You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Bob Schellink <sa...@gmail.com> on 2010/05/25 14:47:00 UTC

Removing Cayenne cache groups

Hi all,

I'm trying to add Cayenne caching for a web project I'm working on. A new DataContext is created for
each request so I'm looking at adding a shared cache for entities that doesn't change often. However
if one of those entities does get updated, I want to refresh the cache.

To start off with I'm trying to test with a small standalone application, following the
documentation here:

http://cayenne.apache.org/doc/query-result-caching.html

My test is fairly similar expect that I specify SHARED_CACHE instead of LOCAL_CACHE:

  SelectQuery query = new SelectQuery(Artist.class);
  query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
  query.setCacheGroups("artists");

I'm also using the default LRUMap.

>From my test I can see that the query data is cached, but when I update the entity, invoke
removeGroup and execute my query a second time, the database is not hit, so I assume the cached data
is returned.

Before going further I just wanted to check whether removeGroup does in fact work on shared caches?
Chances are that I've misconfigured the caching.

Kind regards

Bob

Re: Removing Cayenne cache groups

Posted by Bryan Lewis <jb...@gmail.com>.
Okay, CAY-1436.


On Thu, May 27, 2010 at 11:04 AM, Andrus Adamchik <an...@objectstyle.org>wrote:

>
> On May 27, 2010, at 5:48 PM, Bryan Lewis wrote:
>
>  I couldn't get OSCache to work at first.  I stepped through the code and
>> learned that the queryCacheFactory isn't created unless the
>> dataContextFactory is also specified.  (In
>> DataDomain.initWithProperties().)  I don't see the logic to that... why
>> would those two settings be interdependent?  It was easy to work around by
>> specifying a no-op DataContextFactory.
>>
>
> Whatever that logic was, it makes no sense to me now. Maybe that's the
> cause of the issue with shared cache as well? Apreciate if you could log a
> bug report please.
>
> Andrus
>

Re: Removing Cayenne cache groups

Posted by Andrus Adamchik <an...@objectstyle.org>.
On May 27, 2010, at 5:48 PM, Bryan Lewis wrote:

> I couldn't get OSCache to work at first.  I stepped through the code  
> and
> learned that the queryCacheFactory isn't created unless the
> dataContextFactory is also specified.  (In
> DataDomain.initWithProperties().)  I don't see the logic to that...  
> why
> would those two settings be interdependent?  It was easy to work  
> around by
> specifying a no-op DataContextFactory.

Whatever that logic was, it makes no sense to me now. Maybe that's the  
cause of the issue with shared cache as well? Apreciate if you could  
log a bug report please.

Andrus

Re: Removing Cayenne cache groups

Posted by Joe Baldwin <jf...@earthlink.net>.
If you have access to the ObjectContext with which you originally registered the cache group, (which I personally get via a call to the BaseContext), then that should be your DataContext instance.


On May 29, 2010, at 12:22 AM, Bob Schellink wrote:

> Sorry for my late reply.
> 
> On 28/05/2010 00:48, Bryan Lewis wrote:
>> This email made me aware of that cool new removeGroup and OSCache features.
>> Thanks.  Two little howevers...
>> 
>> In my testing, it worked only with LOCAL_CACHE, as the original poster
>> said.  Not a big deal for me; SHARED_CACHE is rarely necessary.  The same
>> was true with OSCache.
> 
> 
> Did some more testing (and reading Gary Jarrel's thread[1]) and it turns out I've misunderstood
> LOCAL vs SHARED caching. I now understand that:
> 
> - LOCAL_CACHE is cached in a QueryCache at the ObjectContext level
> - SHARED_CACHE is cached in a QueryCache at the DataDomain level
> - These two cached are independent e.g. invoking removeGroups on the ObjectContext QueryCache won't
> clear the DataDomain QueryCache.
> 
> The Java example in the doco (http://cayenne.apache.org/doc30/query-result-caching.html) shows how
> to clear the cache in a post-commit callback, but it targets the ObjectContext cache, in other
> words, LOCAL_CACHE.
> 
> In order to clear a group from the SHARED_CACHE one needs to lookup the DataDomain's QueryCache and
> invoke removeGroup on that cache. The following seems to work:
> 
> void onCommit() {
>   QueryCache cache = customer.getDataContext().getParentDataDomain().getQueryCache();
>   if(isModern()) {
>      cache.removeGroup("modern");
>   }
>   else {
>      cache.removeGroup("classic");
>   }
> }
> 
> That said, DataObject.getDataContext is deprecated. What is the recommended way to lookup the
> DataDomain from inside a DataObject callback? Configuration.getSharedConfiguration().getDomain()?
> 
> Kind regards
> 
> Bob
> 
> [1]: http://markmail.org/message/5qgrpbs2hjz7qxbx


Re: Removing Cayenne cache groups

Posted by Gary Jarrel <ga...@gmail.com>.
On Sat, May 29, 2010 at 2:22 PM, Bob Schellink <sa...@gmail.com> wrote:

>
> That said, DataObject.getDataContext is deprecated. What is the recommended way to lookup the
> DataDomain from inside a DataObject callback? Configuration.getSharedConfiguration().getDomain()?
>
> Kind regards
>
> Bob
>

I tend to call the DataObjects getObjectContext() method and cast that
to DataContext and then access the caches from there.

i.e. ((DataContext)getObjectContext()).getQueryCache() //for local cache

Cheers,

Gary

Re: Removing Cayenne cache groups

Posted by Bob Schellink <sa...@gmail.com>.
Sorry for my late reply.

On 28/05/2010 00:48, Bryan Lewis wrote:
> This email made me aware of that cool new removeGroup and OSCache features.
> Thanks.  Two little howevers...
> 
> In my testing, it worked only with LOCAL_CACHE, as the original poster
> said.  Not a big deal for me; SHARED_CACHE is rarely necessary.  The same
> was true with OSCache.


Did some more testing (and reading Gary Jarrel's thread[1]) and it turns out I've misunderstood
LOCAL vs SHARED caching. I now understand that:

- LOCAL_CACHE is cached in a QueryCache at the ObjectContext level
- SHARED_CACHE is cached in a QueryCache at the DataDomain level
- These two cached are independent e.g. invoking removeGroups on the ObjectContext QueryCache won't
clear the DataDomain QueryCache.

The Java example in the doco (http://cayenne.apache.org/doc30/query-result-caching.html) shows how
to clear the cache in a post-commit callback, but it targets the ObjectContext cache, in other
words, LOCAL_CACHE.

In order to clear a group from the SHARED_CACHE one needs to lookup the DataDomain's QueryCache and
invoke removeGroup on that cache. The following seems to work:

void onCommit() {
   QueryCache cache = customer.getDataContext().getParentDataDomain().getQueryCache();
   if(isModern()) {
      cache.removeGroup("modern");
   }
   else {
      cache.removeGroup("classic");
   }
}

That said, DataObject.getDataContext is deprecated. What is the recommended way to lookup the
DataDomain from inside a DataObject callback? Configuration.getSharedConfiguration().getDomain()?

Kind regards

Bob

[1]: http://markmail.org/message/5qgrpbs2hjz7qxbx

Re: Removing Cayenne cache groups

Posted by Bryan Lewis <jb...@gmail.com>.
This email made me aware of that cool new removeGroup and OSCache features.
Thanks.  Two little howevers...

In my testing, it worked only with LOCAL_CACHE, as the original poster
said.  Not a big deal for me; SHARED_CACHE is rarely necessary.  The same
was true with OSCache.

I couldn't get OSCache to work at first.  I stepped through the code and
learned that the queryCacheFactory isn't created unless the
dataContextFactory is also specified.  (In
DataDomain.initWithProperties().)  I don't see the logic to that... why
would those two settings be interdependent?  It was easy to work around by
specifying a no-op DataContextFactory.


On Wed, May 26, 2010 at 9:13 AM, Andrus Adamchik <an...@objectstyle.org>wrote:

> Odd. MapQueryCacheFactory supports 'removeGroup' method (although it is not
> very efficient compared to say OSQueryCacheFactory). Can you possibly run
> this in debugger to check the state of the cache?
>
> Andrus
>
>
>
>
> On May 25, 2010, at 3:47 PM, Bob Schellink wrote:
>
>  Hi all,
>>
>> I'm trying to add Cayenne caching for a web project I'm working on. A new
>> DataContext is created for
>> each request so I'm looking at adding a shared cache for entities that
>> doesn't change often. However
>> if one of those entities does get updated, I want to refresh the cache.
>>
>> To start off with I'm trying to test with a small standalone application,
>> following the
>> documentation here:
>>
>> http://cayenne.apache.org/doc/query-result-caching.html
>>
>> My test is fairly similar expect that I specify SHARED_CACHE instead of
>> LOCAL_CACHE:
>>
>>  SelectQuery query = new SelectQuery(Artist.class);
>>  query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
>>  query.setCacheGroups("artists");
>>
>> I'm also using the default LRUMap.
>>
>> From my test I can see that the query data is cached, but when I update
>> the entity, invoke
>> removeGroup and execute my query a second time, the database is not hit,
>> so I assume the cached data
>> is returned.
>>
>> Before going further I just wanted to check whether removeGroup does in
>> fact work on shared caches?
>> Chances are that I've misconfigured the caching.
>>
>> Kind regards
>>
>> Bob
>>
>>
>

Re: Removing Cayenne cache groups

Posted by Andrus Adamchik <an...@objectstyle.org>.
Odd. MapQueryCacheFactory supports 'removeGroup' method (although it  
is not very efficient compared to say OSQueryCacheFactory). Can you  
possibly run this in debugger to check the state of the cache?

Andrus



On May 25, 2010, at 3:47 PM, Bob Schellink wrote:

> Hi all,
>
> I'm trying to add Cayenne caching for a web project I'm working on.  
> A new DataContext is created for
> each request so I'm looking at adding a shared cache for entities  
> that doesn't change often. However
> if one of those entities does get updated, I want to refresh the  
> cache.
>
> To start off with I'm trying to test with a small standalone  
> application, following the
> documentation here:
>
> http://cayenne.apache.org/doc/query-result-caching.html
>
> My test is fairly similar expect that I specify SHARED_CACHE instead  
> of LOCAL_CACHE:
>
>  SelectQuery query = new SelectQuery(Artist.class);
>  query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
>  query.setCacheGroups("artists");
>
> I'm also using the default LRUMap.
>
> From my test I can see that the query data is cached, but when I  
> update the entity, invoke
> removeGroup and execute my query a second time, the database is not  
> hit, so I assume the cached data
> is returned.
>
> Before going further I just wanted to check whether removeGroup does  
> in fact work on shared caches?
> Chances are that I've misconfigured the caching.
>
> Kind regards
>
> Bob
>