Print Page | Close Window

How to disable caching? And more info on how caching works?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3108
Printed Date: 13-May-2026 at 11:36am


Topic: How to disable caching? And more info on how caching works?
Posted By: katit
Subject: How to disable caching? And more info on how caching works?
Date Posted: 23-Nov-2011 at 3:31pm

I have this code in many places. Is there any more compact way to get manager that doesn't do caching?

var queryStrategy = new QueryStrategy(
                FetchStrategy.DataSourceOnly,
                MergeStrategy.PreserveChanges,
                QueryInversionMode.Manual,
                TransactionSettings.Default,
                CacheQueryOptions.Default);

            this.entityManager =
                new IDATTApplicationEntities(modelService.ApplicationEntityManager)
                    {
                        DefaultQueryStrategy = queryStrategy
                    };

 
Question #2:
 
I have "messageService" inside my app and I poll for new messages every X minutes. I take timestamp of last message and use it for query parameter. So, for example if latest message was created at 10pm - in 10 minutes I will query again to check if there was messages created after 10pm. IdeaBlade would cache this query but since there was no messages - I will retry in 10 minutes and get nothing because query goes directly to cache. Is that scenario good example when cache need to be turned off?



Replies:
Posted By: DenisK
Date Posted: 23-Nov-2011 at 7:02pm
Hi katit;

Question #1:

I don't think it's possible to really turn off caching of query results inside the EntityManager. The best you can do is to direct the execution of your query against the data source. I tested your queryStrategy above just to be sure and my query results were still cached. Having said that, the more compact way to achieve the same effect as this particular queryStrategy is to use QueryStrategy.DataSourceOnly.

Question #2:

If you set the query's strategy to QueryStrategy.Normal, query execution will first check the cache and then the data source. You also have the option to specify the QueryStrategy for each query with the With extension method. For example: manager.Entities.With(QueryStrategy.Normal) or manager.Entities.With(QueryStrategy.DataSourceOnly) to force query execution against the data source.

For more info on how caching works, you can see  http://drc.ideablade.com/xwiki/bin/view/Documentation/query-cache - http://drc.ideablade.com/xwiki/bin/view/Documentation/query-cache .

Hope this helps.


Posted By: katit
Date Posted: 23-Nov-2011 at 7:12pm
#1 - I'm confused now. I do use that code everywhere and indeed it doesn't cache anything - every time I query - I hit database. Are you sure it's not caching for you?
I think FetchStrategy does that


Posted By: DenisK
Date Posted: 23-Nov-2011 at 7:26pm
Yes. This is the test that I did.

      var mgr = new DomainModelEntityManager();
      var queryStrategy = new QueryStrategy(
                FetchStrategy.DataSourceOnly,
                MergeStrategy.PreserveChanges,
                QueryInversionMode.Manual,
                TransactionSettings.Default,
                CacheQueryOptions.Default);
      mgr.DefaultQueryStrategy = queryStrategy;
      mgr.Orders.Include("OrderDetails.Product").Execute();
      //_em1.Orders.Execute();
      Assert.IsTrue(mgr.FindEntities<Order>(EntityState.AllButDetached).Any());
      Assert.IsTrue(mgr.FindEntities<OrderDetail>(EntityState.AllButDetached).Any());
      Assert.IsTrue(mgr.FindEntities<Product>(EntityState.AllButDetached).Any());

mgr.FindEntities is a method that retrieves entities only in the cache.

Everytime you query, it hits the database because that's what you have specified for the FetchStrategy. But the entity results that comes back from the database are still cached inside the EntityManager. So entity caching still occurs but because you're telling the query to always go to the data source, it will never check the cache.


Posted By: katit
Date Posted: 23-Nov-2011 at 7:35pm
Glad I asked then. Need to re-read all documentation on caching. It makes sense.
 
The more I look at it - in my application I want NO cache by default and MAYBE I will consider it for some pieces. So far everything I did - didn't require caching.


Posted By: katit
Date Posted: 12-Jan-2012 at 6:14pm
Ok, now I know how to hit data store every time.
Is it possible to disable caching at all? Do I understand correctly that caching uses RAM? I don't really need that feature


Posted By: DenisK
Date Posted: 12-Jan-2012 at 7:31pm
Yes, caching is an in-memory representation of the entities you just fetch from the data source so it does use some memory resources. 

If you want to clear your cache, you can

Clear the query cache explicitly by calling EntityManager.QueryCache Clear method.

or

Clear the query cache implicitly by removing any entity from the entity cache.

or

Clear the EntityManager's cache by calling EntityManager.Clear(). Only do this after you've finished working with your entities and saved them to the database.





Print Page | Close Window