You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by cghersi <cr...@abodata.com> on 2012/05/23 14:07:48 UTC

Cayenne cache strategy

Hi all,

I'm trying to use the query cache of Cayenne 3.0.

My code snippet for the typical query is:

private List query(Expression qualifier) {
  DataContext context = config.getDomain("MyDomain").createDataContext();
  SelectQuery select = new SelectQuery(MyClass.class, qualifier);
  select.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
  return context.performQuery(select);
}

With this code, if I call my query() method in the following way List b is
empty:
Expression qualifier = ... //my filters
List a = query(qualifier);

DataContext context = config.getDomain("MyDomain").createDataContext();
//this is another context with respect to the one used in query()
MyClass newObj = context.newObject(MyClass .class);
// set props of newObj
context.commitChanges();

List b = query(qualifier); //the same qualifier resulting in List a

The problem seems related to the fact that committing the changes regarding
the newly created object doesn't result in a refresh of the Cayenne cache,
and so the very same result retrieved for List a is returned also for List
b.

Is there any setting or configuration that I need to enable?
Is there any command that I need to issue in order to invalidate the
preiovus results of the cache in this example?

Thank you very much
Best
cghersi



--
View this message in context: http://cayenne.195.n3.nabble.com/Cayenne-cache-strategy-tp4008506.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: Cayenne cache strategy

Posted by Andrus Adamchik <an...@objectstyle.org>.
> The problem seems related to the fact that committing the changes regarding
> the newly created object doesn't result in a refresh of the Cayenne cache,
> and so the very same result retrieved for List a is returned also for List
> b.

Correct. There's no good generic algorithm to efficiently detect which cached lists should be flushed on commit, so yes, commits have no effect on query cache by default.

> Is there any setting or configuration that I need to enable?
> Is there any command that I need to issue in order to invalidate the
> preiovus results of the cache in this example?

I would recommend using "cache groups" to categorize queries - query.setCacheGroups("somegroup"). Then you can setup your own cache invalidation calling queryCache.removeGroup(..) when certain types of objects are committed.

In Cayenne 3.1 we went a step further, and created @CacheGroups annotations and CacheInvalidationFilter that allow to do the above declaratively. E.g.:

@CacheGroups("abc")
class Artist extends _Artist {}

The above declaration would ensure that all cached queries that used "abc" cache group are flushed when any Artist object is committed.

Andrus


On May 23, 2012, at 8:07 AM, cghersi wrote:
> Hi all,
> 
> I'm trying to use the query cache of Cayenne 3.0.
> 
> My code snippet for the typical query is:
> 
> private List query(Expression qualifier) {
>  DataContext context = config.getDomain("MyDomain").createDataContext();
>  SelectQuery select = new SelectQuery(MyClass.class, qualifier);
>  select.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
>  return context.performQuery(select);
> }
> 
> With this code, if I call my query() method in the following way List b is
> empty:
> Expression qualifier = ... //my filters
> List a = query(qualifier);
> 
> DataContext context = config.getDomain("MyDomain").createDataContext();
> //this is another context with respect to the one used in query()
> MyClass newObj = context.newObject(MyClass .class);
> // set props of newObj
> context.commitChanges();
> 
> List b = query(qualifier); //the same qualifier resulting in List a
> 
> The problem seems related to the fact that committing the changes regarding
> the newly created object doesn't result in a refresh of the Cayenne cache,
> and so the very same result retrieved for List a is returned also for List
> b.
> 
> Is there any setting or configuration that I need to enable?
> Is there any command that I need to issue in order to invalidate the
> preiovus results of the cache in this example?
> 
> Thank you very much
> Best
> cghersi
> 
> 
> 
> --
> View this message in context: http://cayenne.195.n3.nabble.com/Cayenne-cache-strategy-tp4008506.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.
>