You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Francesco Fuzio <fr...@tiscali.it> on 2006/09/25 14:41:05 UTC

Caching query results

Hi!

For performance issues we have the need to cache some query result (i.e 
a list of cayenne data objects);
As far as I have understood it's completely up to the developer to 
determine if a previously cached result is still logically valid or not 
(maybe this will change in Cayenne 3.0?).
The developer has to implement too, from scratch, a queries invalidation 
mechanism.

In http://cwiki.apache.org/CAYDOC/caching-query-results.html is stated that:

<<It is important to understand that caching of *result lists* is done 
independently from caching of *individual DataObjects and DataRows*. 
Therefore the API is different as well. Also cached results lists _are 
not synchronized across VMs (even the shared cache)._>>_

_So it seems that if my application is deployed in a cluster I have to 
implement a custom _distributed_ invalidation mechanism.
_
_So my questions are:

1) Are my conclusions correct or not?
2) In case I'm right do u have any "pattern"/best practice/suggestion to 
implement the afore mentioned mechanism in a fast , efficient way?

Thank you ,


Francesco.
_

_





Re: Caching query results

Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi Francesco,

I see. Essentially DataObject -> Query set association would be an  
analogue of 3.0 cache groups (where group name == entity name).

Actually in-memory checking procedure the way you describe it may not  
be as bad as I originally thought. Matching object state before and  
after commit against all cached queries for a given entity is  
proportional to the number of cached queries (including different  
parameter permutations of the same query), not the number of objects  
in the results. So Cayenne will not turn itself in a DB engine :-)

If that works for you and you are willing to contribute it to Apache  
once it is polished, we may include it as a pluggable strategy...  
possibly a QueryCache decorator...

http://svn.apache.org/repos/asf/incubator/cayenne/main/trunk/core/ 
cayenne-jdk1.4/src/main/java/org/apache/cayenne/cache/QueryCache.java

Andrus


On Sep 26, 2006, at 9:14 AM, Francesco Fuzio wrote:

> Hi Andrus,
>
> first of all thank you for the prompt support and your suggestions.
>
> As you correctly guessed I was talking about Cayenne 1.2.1
>
> I was thinking about this automatic (but based on custom  
> configuration) "invalidation algorithm":
>
> Configure somewhere a logic association DataObject--->"QueryData"[]  
> (ex Paintings DataObject has to be associated to the queries  
> [Select * from Paintings where year = 1300 | Select * from Artists,  
> Paintings where Paintings.year >1200 | Select * from Artists,  
> Paintings where Paintings.year >1200 order by Paintings.name]
>
> The "QueryData" object shoud contain, separately, information about  
> the "expression" (i.e. the "where" part ) and about ordering.
> "Select * from Artists, Paintings where Paintings.year >1200 order  
> by Paintings.name" ---> { Paintings.year >1200 | order by  
> Paintings.name }
>
> If I modify (or create or delete) a DataObject I have to check the  
> ante and post modification version of the single DataObject against  
> the associated QueryData's
> We could do this exploiting the Objects filtering capabilities  
> Expression (or optionally using third parties utilities <commons- 
> bean-utils??>) :
>
> Expression filter = Expression.fromString("Paintings.year >1200");
> filter.filterObjects(objects);
>
> As a result we would have two Sets of Queries : those matching  
> before the modification and those matching after the modification.
> For sure we have to invalidate all the query results that are not  
> in the intersection of the two sets.
>
> For the queries in the intersection:
>
> a)If they have NO ordering (Order by clause , paging limitation  
> etc) they are still valid
>
> b)If they have ordering: if ordering is on one of the modified  
> DataObject field, we have to invalidate the query result, otherwise  
> the query result is still valid.
>
> Of course this solution can lead to high computational resources  
> use, dependent on the number of queries it has to check.
> But, for example in the project I am collaborating to, the Db is  
> the "under pressure"/bottleneck system and the Middleware has much  
> less load.
> In such a situation "moving" load from the Db to the Mw is a  
> benefit for the Application as a whole.
>
> For "basic" queries  (I made some tests) I think the algorithm  
> should work. Of course more systematic test cases should be  
> performed to completely validate the algorithm and/or find its  
> limitation.
> Anyway I wanted to share it with you hoping it can be useful or can  
> be of some "inspiration" for a proper/more correct solution.
>
>
> Francesco.

Re: Caching query results

Posted by Francesco Fuzio <fr...@tiscali.it>.
Hi Andrus,

first of all thank you for the prompt support and your suggestions.

As you correctly guessed I was talking about Cayenne 1.2.1

I was thinking about this automatic (but based on custom configuration) 
"invalidation algorithm":

Configure somewhere a logic association DataObject--->"QueryData"[] (ex 
Paintings DataObject has to be associated to the queries [Select * from 
Paintings where year = 1300 | Select * from Artists, Paintings where 
Paintings.year >1200 | Select * from Artists, Paintings where 
Paintings.year >1200 order by Paintings.name]

The "QueryData" object shoud contain, separately, information about the 
"expression" (i.e. the "where" part ) and about ordering.
"Select * from Artists, Paintings where Paintings.year >1200 order by 
Paintings.name" ---> { Paintings.year >1200 | order by Paintings.name }

If I modify (or create or delete) a DataObject I have to check the ante 
and post modification version of the single DataObject against the 
associated QueryData's
We could do this exploiting the Objects filtering capabilities 
Expression (or optionally using third parties utilities 
<commons-bean-utils??>) :

Expression filter = Expression.fromString("Paintings.year >1200");
filter.filterObjects(objects);

As a result we would have two Sets of Queries : those matching before 
the modification and those matching after the modification.
For sure we have to invalidate all the query results that are not in the 
intersection of the two sets.

For the queries in the intersection:

a)If they have NO ordering (Order by clause , paging limitation etc) 
they are still valid

b)If they have ordering: if ordering is on one of the modified 
DataObject field, we have to invalidate the query result, otherwise the 
query result is still valid.

Of course this solution can lead to high computational resources use, 
dependent on the number of queries it has to check.
But, for example in the project I am collaborating to, the Db is the 
"under pressure"/bottleneck system and the Middleware has much less load.
In such a situation "moving" load from the Db to the Mw is a benefit for 
the Application as a whole.

For "basic" queries  (I made some tests) I think the algorithm should 
work. Of course more systematic test cases should be performed to 
completely validate the algorithm and/or find its limitation.
Anyway I wanted to share it with you hoping it can be useful or can be 
of some "inspiration" for a proper/more correct solution.


Francesco.




Andrus Adamchik wrote:
> Hi Francesco,
>
>
> On Sep 25, 2006, at 10:56 AM, Francesco Fuzio wrote:
>> Thank you for the answers: I'm definitely looking forward to trying 
>> the 3.0 cool features you mentioned.
>>
>> As for 2.1 (since for us is important to keep data updated without 
>> relying on expiration timing) I was thinking about this approach (for 
>> a clustered environment)
>
> That would be version 1.2.*, right?
>
>> 1) Enable Cayenne Replicated Shared Object Cache
>> 2) Disable Cayenne Query (i.e list ) Cache
>> 3) Use a Caching framework supporting automatic distributed 
>> refresh/invalidation policy (e.g Oscahe or Ehcache) to save query 
>> results as list of ObjectId's.
>> 4) In case of Query "Cache Hit" use the cached ObjectId's to retrieve 
>> the associated DataObjects via the DataContext [ public Persistent 
>> <http://incubator.apache.org/cayenne/1_2/api/cayenne/org/objectstyle/cayenne/Persistent.html> 
>> *localObject*(ObjectId 
>> <http://incubator.apache.org/cayenne/1_2/api/cayenne/org/objectstyle/cayenne/ObjectId.html> 
>> id, Persistent 
>> <http://incubator.apache.org/cayenne/1_2/api/cayenne/org/objectstyle/cayenne/Persistent.html> 
>> prototype)]
>>
>> What do you think, is this approach reasonable? Will it work?
>
> This should work (you'll just use your own cache as a front end to the 
> DataContext query API), and should provide a clean path to the future 
> 3.0 migration. You'll need to consider a few things though:
>
> A. Query cache key generation. In 1.2 this is based on Query name 
> which is pretty dumb and barely usable; in 3.0 SelectQuery and 
> SQLTemplate are smart enough to build the cache key based on their 
> state. You may copy some of that code.
>
>
> B. Invalidation Strategies. That's a tricky one....
>
> I couldn't come up with a well-performing generic solution (I tried, 
> see CAY-577). Consider that events that may cause automatic 
> invalidation are object deletion, insertion and updating (update can 
> affect the ordering and also whether an object still matches the query 
> condition). So *every* commit can potentially invalidate any number of 
> cached lists for a given entity.
>
> The trick is to create an efficient algorithm to invalidate just the 
> right cache entries and avoid invalidating the entire entity cache. 
> Manually scanning and rearranging all lists on every commit is of 
> course very inefficient.
>
> So in 3.0 we added "cache group" notion so that users could categorize 
> queries based on some criteria and then invalidate the whole category 
> of cache entries. (Cache group notion is supported by OSCache by the 
> way). Here is an example.... Consider a "BlogPost" entity. All queries 
> that fetch a date range of BlogPosts can be arbitrarily divided into 
> "old_posts" and "new_posts" categories. So once a user 
> updates/deletes/removes a BlogPost, a code can check the date of this 
> post and invalidate either "old_posts" or "new_posts".
>
> This is just one solution that we came up with. Not automatic, but 
> fairly simple and efficient. You can come up with your own strategies. 
> If you can think of a better generic algorithm for invalidation, 
> please share.
>
> Andrus
>
>
>
> __________ NOD32 1.1767 (20060921) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.nod32.com
>
>
>



Re: Caching query results

Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi Francesco,


On Sep 25, 2006, at 10:56 AM, Francesco Fuzio wrote:
> Thank you for the answers: I'm definitely looking forward to trying  
> the 3.0 cool features you mentioned.
>
> As for 2.1 (since for us is important to keep data updated without  
> relying on expiration timing) I was thinking about this approach  
> (for a clustered environment)

That would be version 1.2.*, right?

> 1) Enable Cayenne Replicated Shared Object Cache
> 2) Disable Cayenne Query (i.e list ) Cache
> 3) Use a Caching framework supporting automatic distributed refresh/ 
> invalidation policy (e.g Oscahe or Ehcache) to save query results  
> as list of ObjectId's.
> 4) In case of Query "Cache Hit" use the cached ObjectId's to  
> retrieve the associated DataObjects via the DataContext [ public  
> Persistent <http://incubator.apache.org/cayenne/1_2/api/cayenne/org/ 
> objectstyle/cayenne/Persistent.html> *localObject*(ObjectId <http:// 
> incubator.apache.org/cayenne/1_2/api/cayenne/org/objectstyle/ 
> cayenne/ObjectId.html> id, Persistent <http://incubator.apache.org/ 
> cayenne/1_2/api/cayenne/org/objectstyle/cayenne/Persistent.html>  
> prototype)]
>
> What do you think, is this approach reasonable? Will it work?

This should work (you'll just use your own cache as a front end to  
the DataContext query API), and should provide a clean path to the  
future 3.0 migration. You'll need to consider a few things though:

A. Query cache key generation. In 1.2 this is based on Query name  
which is pretty dumb and barely usable; in 3.0 SelectQuery and  
SQLTemplate are smart enough to build the cache key based on their  
state. You may copy some of that code.


B. Invalidation Strategies. That's a tricky one....

I couldn't come up with a well-performing generic solution (I tried,  
see CAY-577). Consider that events that may cause automatic  
invalidation are object deletion, insertion and updating (update can  
affect the ordering and also whether an object still matches the  
query condition). So *every* commit can potentially invalidate any  
number of cached lists for a given entity.

The trick is to create an efficient algorithm to invalidate just the  
right cache entries and avoid invalidating the entire entity cache.  
Manually scanning and rearranging all lists on every commit is of  
course very inefficient.

So in 3.0 we added "cache group" notion so that users could  
categorize queries based on some criteria and then invalidate the  
whole category of cache entries. (Cache group notion is supported by  
OSCache by the way). Here is an example.... Consider a "BlogPost"  
entity. All queries that fetch a date range of BlogPosts can be  
arbitrarily divided into "old_posts" and "new_posts" categories. So  
once a user updates/deletes/removes a BlogPost, a code can check the  
date of this post and invalidate either "old_posts" or "new_posts".

This is just one solution that we came up with. Not automatic, but  
fairly simple and efficient. You can come up with your own  
strategies. If you can think of a better generic algorithm for  
invalidation, please share.

Andrus



Re: Caching query results

Posted by Francesco Fuzio <fr...@tiscali.it>.
Thank you for the answers: I'm definitely looking forward to trying the 
3.0 cool features you mentioned.

As for 2.1 (since for us is important to keep data updated without 
relying on expiration timing) I was thinking about this approach (for a 
clustered environment)

1) Enable Cayenne Replicated Shared Object Cache
2) Disable Cayenne Query (i.e list ) Cache
3) Use a Caching framework supporting automatic distributed 
refresh/invalidation policy (e.g Oscahe or Ehcache) to save query 
results as list of ObjectId's.
4) In case of Query "Cache Hit" use the cached ObjectId's to retrieve 
the associated DataObjects via the DataContext [ public Persistent 
<http://incubator.apache.org/cayenne/1_2/api/cayenne/org/objectstyle/cayenne/Persistent.html> 
*localObject*(ObjectId 
<http://incubator.apache.org/cayenne/1_2/api/cayenne/org/objectstyle/cayenne/ObjectId.html> id, 
Persistent 
<http://incubator.apache.org/cayenne/1_2/api/cayenne/org/objectstyle/cayenne/Persistent.html> prototype)]

What do you think, is this approach reasonable? Will it work?

Thanks,

Francesco.



Andrus Adamchik wrote:
> Until 3.0 invalidating query lists was totally up to the developer.
>
> In 3.0 we added three things - RefreshQuery (mentioned by Ari), "query 
> cache group" concept, and pluggable QueryCache. This makes clustering 
> and refresh policy management much simpler. I am running this latest 
> code (3.0 trunk essentially) in pre-production environment on a 
> cluster using OSQueryCache and it work extremely well.
>
> I am going to document it better at some point (maybe even write an 
> article). For now please post on the list if you have more questions 
> about that. Essentially I do something like this:
>
> dataDomain.setQueryCacheFactory(new OSQueryCacheFactory());
>
> and then configure "oscache.properties" with group refresh policies 
> (see OSQueryCache javadocs and OSCache framework documentation for 
> details).
>
> Once you have this set up, you can build in more bells and whistles if 
> you wish. For instance I implemented the following extensions on a 
> customer project:
>
> * a web console for interactive cache group invalidation on the cluster.
> * hooks to invalidate certain groups when certain entities are 
> modified by the user via another new 3.0 feature - callbacks (I am 
> going to write an overview of callbacks soon).
>
> Andrus
>
>
> On Sep 25, 2006, at 9:34 AM, Aristedes Maniatis wrote:
>>
>> On 25/09/2006, at 10:41 PM, Francesco Fuzio wrote:
>>
>>> <<It is important to understand that caching of *result lists* is 
>>> done independently from caching of *individual DataObjects and 
>>> DataRows*. Therefore the API is different as well. Also cached 
>>> results lists _are not synchronized across VMs (even the shared 
>>> cache)._>>_
>>>
>>> _So it seems that if my application is deployed in a cluster I have 
>>> to implement a custom _distributed_ invalidation mechanism.
>>
>> It depends on how important it is that the data is up to date. One 
>> solution might be to add functionality to Cayenne which invalidates 
>> objects in the cache after a certain period of time (for instance 1 
>> hour). It is now possible to invalidate objects one at time 
>> (http://cwiki.apache.org/confluence/display/CAYDOC/RefreshQuery)
>>
>> We are examining the same problem in our application (not a web 
>> cluster, but a client/server application). For now we switched off 
>> caching, but we need to think through the issues. One solution is to 
>> implement a distributing messaging system to send notifications 
>> between clients - I think Andrus was looking at XMPP at one time 
>> (http://www.xmpp.org/summary.html) for this. Certainly it would be 
>> handy if a messaging system was built into Cayenne for this. It would 
>> also be useful for things like pessimistic locking.
>>
>>
>> Ari Maniatis
>>
>>
>>
>> -------------------------->
>> ish
>> http://www.ish.com.au
>> Level 1, 30 Wilson Street Newtown 2042 Australia
>> phone +61 2 9550 5001   fax +61 2 9550 4001
>> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>>
>>
>
>
> __________ NOD32 1.1767 (20060921) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.nod32.com
>
>
>


Re: Caching query results

Posted by Andrus Adamchik <an...@objectstyle.org>.
I think the answer is still no, there's no better docs yet. sorry.

Andrus


On Aug 24, 2008, at 7:00 PM, Borut Bolčina wrote:
> Hi,
>
> there is still no info/examples about OSCache usage at
> http://cayenne.apache.org/doc/caching-and-fresh-data.html. Is there  
> anything
> in the oven?
>
> Cheers,
> Borut
>
> 2006/9/25 Andrus Adamchik <an...@objectstyle.org>
>
>> Until 3.0 invalidating query lists was totally up to the developer.
>>
>> In 3.0 we added three things - RefreshQuery (mentioned by Ari),  
>> "query
>> cache group" concept, and pluggable QueryCache. This makes  
>> clustering and
>> refresh policy management much simpler. I am running this latest  
>> code (3.0
>> trunk essentially) in pre-production environment on a cluster using
>> OSQueryCache and it work extremely well.
>>
>> I am going to document it better at some point (maybe even write an
>> article). For now please post on the list if you have more  
>> questions about
>> that. Essentially I do something like this:
>>
>> dataDomain.setQueryCacheFactory(new OSQueryCacheFactory());
>>
>> and then configure "oscache.properties" with group refresh policies  
>> (see
>> OSQueryCache javadocs and OSCache framework documentation for  
>> details).
>>
>>


Re: Caching query results

Posted by Borut Bolčina <bo...@gmail.com>.
Hi,

there is still no info/examples about OSCache usage at
http://cayenne.apache.org/doc/caching-and-fresh-data.html. Is there anything
in the oven?

Cheers,
Borut

2006/9/25 Andrus Adamchik <an...@objectstyle.org>

> Until 3.0 invalidating query lists was totally up to the developer.
>
> In 3.0 we added three things - RefreshQuery (mentioned by Ari), "query
> cache group" concept, and pluggable QueryCache. This makes clustering and
> refresh policy management much simpler. I am running this latest code (3.0
> trunk essentially) in pre-production environment on a cluster using
> OSQueryCache and it work extremely well.
>
> I am going to document it better at some point (maybe even write an
> article). For now please post on the list if you have more questions about
> that. Essentially I do something like this:
>
> dataDomain.setQueryCacheFactory(new OSQueryCacheFactory());
>
> and then configure "oscache.properties" with group refresh policies (see
> OSQueryCache javadocs and OSCache framework documentation for details).
>
>

Re: Caching query results

Posted by Borut Bolčina <bo...@gmail.com>.
Hi,

there is still no info/examples about OSCache usage at
http://cayenne.apache.org/doc/caching-and-fresh-data.html. Is there anything
in the oven?

Cheers,
Borut

2006/9/25 Andrus Adamchik <an...@objectstyle.org>

>
> In 3.0 we added three things - RefreshQuery (mentioned by Ari), "query
> cache group" concept, and pluggable QueryCache. This makes clustering and
> refresh policy management much simpler. I am running this latest code (3.0
> trunk essentially) in pre-production environment on a cluster using
> OSQueryCache and it work extremely well.
>
> I am going to document it better at some point (maybe even write an
> article). For now please post on the list if you have more questions about
> that. Essentially I do something like this:
>
> dataDomain.setQueryCacheFactory(new OSQueryCacheFactory());
>
> and then configure "oscache.properties" with group refresh policies (see
> OSQueryCache javadocs and OSCache framework documentation for details).
>
>

Re: Caching query results

Posted by Andrus Adamchik <an...@objectstyle.org>.
Until 3.0 invalidating query lists was totally up to the developer.

In 3.0 we added three things - RefreshQuery (mentioned by Ari),  
"query cache group" concept, and pluggable QueryCache. This makes  
clustering and refresh policy management much simpler. I am running  
this latest code (3.0 trunk essentially) in pre-production  
environment on a cluster using OSQueryCache and it work extremely well.

I am going to document it better at some point (maybe even write an  
article). For now please post on the list if you have more questions  
about that. Essentially I do something like this:

dataDomain.setQueryCacheFactory(new OSQueryCacheFactory());

and then configure "oscache.properties" with group refresh policies  
(see OSQueryCache javadocs and OSCache framework documentation for  
details).

Once you have this set up, you can build in more bells and whistles  
if you wish. For instance I implemented the following extensions on a  
customer project:

* a web console for interactive cache group invalidation on the cluster.
* hooks to invalidate certain groups when certain entities are  
modified by the user via another new 3.0 feature - callbacks (I am  
going to write an overview of callbacks soon).

Andrus


On Sep 25, 2006, at 9:34 AM, Aristedes Maniatis wrote:
>
> On 25/09/2006, at 10:41 PM, Francesco Fuzio wrote:
>
>> <<It is important to understand that caching of *result lists* is  
>> done independently from caching of *individual DataObjects and  
>> DataRows*. Therefore the API is different as well. Also cached  
>> results lists _are not synchronized across VMs (even the shared  
>> cache)._>>_
>>
>> _So it seems that if my application is deployed in a cluster I  
>> have to implement a custom _distributed_ invalidation mechanism.
>
> It depends on how important it is that the data is up to date. One  
> solution might be to add functionality to Cayenne which invalidates  
> objects in the cache after a certain period of time (for instance 1  
> hour). It is now possible to invalidate objects one at time (http:// 
> cwiki.apache.org/confluence/display/CAYDOC/RefreshQuery)
>
> We are examining the same problem in our application (not a web  
> cluster, but a client/server application). For now we switched off  
> caching, but we need to think through the issues. One solution is  
> to implement a distributing messaging system to send notifications  
> between clients - I think Andrus was looking at XMPP at one time  
> (http://www.xmpp.org/summary.html) for this. Certainly it would be  
> handy if a messaging system was built into Cayenne for this. It  
> would also be useful for things like pessimistic locking.
>
>
> Ari Maniatis
>
>
>
> -------------------------->
> ish
> http://www.ish.com.au
> Level 1, 30 Wilson Street Newtown 2042 Australia
> phone +61 2 9550 5001   fax +61 2 9550 4001
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>
>


Re: Caching query results

Posted by Aristedes Maniatis <ar...@ish.com.au>.
On 25/09/2006, at 10:41 PM, Francesco Fuzio wrote:

> <<It is important to understand that caching of *result lists* is  
> done independently from caching of *individual DataObjects and  
> DataRows*. Therefore the API is different as well. Also cached  
> results lists _are not synchronized across VMs (even the shared  
> cache)._>>_
>
> _So it seems that if my application is deployed in a cluster I have  
> to implement a custom _distributed_ invalidation mechanism.

It depends on how important it is that the data is up to date. One  
solution might be to add functionality to Cayenne which invalidates  
objects in the cache after a certain period of time (for instance 1  
hour). It is now possible to invalidate objects one at time (http:// 
cwiki.apache.org/confluence/display/CAYDOC/RefreshQuery)

We are examining the same problem in our application (not a web  
cluster, but a client/server application). For now we switched off  
caching, but we need to think through the issues. One solution is to  
implement a distributing messaging system to send notifications  
between clients - I think Andrus was looking at XMPP at one time  
(http://www.xmpp.org/summary.html) for this. Certainly it would be  
handy if a messaging system was built into Cayenne for this. It would  
also be useful for things like pessimistic locking.


Ari Maniatis



-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001   fax +61 2 9550 4001
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A