New Posts New Posts RSS Feed: How to disable caching? And more info on how caching works?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

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

 Post Reply Post Reply
Author
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Topic: How to disable caching? And more info on how caching works?
    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?
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.

Hope this helps.
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.


Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down