Caching Query ResultsPage edited by Andrus AdamchikChanges (29)
Full ContentCayenne provides a way to cache query results, avoiding unneeded database trips for the frequently used queries. Caching strategy is configured per query. A strategy can be set via API or in CayenneModeler. Possible strategies, as defined in the org.apache.cayenne.query.QueryCacheStrategy enum are the following:
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). API for Result CachingWhen creating queries in the code, users may set a desired cache strategy per query. Below we will create a query and set its caching policy to LOCAL_CACHE:
SelectQuery query = new SelectQuery(Artist.class);// set local cache strategy, meaning the cache will be stored in the ObjectContext // and not shared between different contexts query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE); ObjectContext context = ... // assume this exists // if there was no cache at this point, the query will hit the database, // and will store the result in the cache List objects = context.performQuery(query); Now if we rerun the same query (or create a new instance of the query with the same set of parameters), we'll get cached result, which will be much faster than fetching it from DB. .bq The point about 2 separate queries reusing the same cache entry is worth repeating. A cache key for each query is automatically generated by Cayenne based on the type of the query and its parameters (such as qualifier, ordering, etc.). So a query itself does not need to be cached by the user code for future reuse. New queries can be created as needed. // creating a new query, same as the previous one SelectQuery query1 = new SelectQuery(Artist.class); // this will hit the local cache List objects1 = context.performQuery(query1); Or if we want to refresh the cache, but still keep caching the result after that: query1.setCachePolicy(QueryCacheStrategy.LOCAL_CACHE_REFRESH); List objects2 = context.performQuery(query1); The example above shows caching with SelectQuery, but it works exactly the same way for SQLTemplate and ProcedureQuery. Similarly SHARED_CACHE and SHARED_CACHE_REFRESH cache policies create cache shared by all ObjectContexts that work on top of a given DataDomain. Queries Mapped in CayenneModelerThe easiest way to set up caching is by creating a named query in CayenneModeler with the appropriate caching type. Then it can be executed via DataContext: List objects1 = context.performQuery("MyQuery", false); The second "false" parameter above indicated that if possible, cached result should be used. Now if we want to force refresh, it can be changed to true (for just this invocation - this does not affect the underlying saved query) List objects2 = context.performQuery("MyQuery", true); Note that parameterized named queries will still work correctly with the cache. We've already mentioned that the users must ensure that two queries must have different names if they fetch logically different data. This is NOT the case with queries stored in the DataMap. If you run the same named query with different sets of parameters, Cayenne will internally generate unique cache keys for each distinct parameter set. Map parameters = Collections.singletonMap("key", "value1"); List objects1 = context.performQuery("MyQuery", parameters, false); Now if we run the same query with a different set of parameters, Cayenne will do the right thing and create a separate entry in the cache: Map parameters = Collections.singletonMap("key", "value2"); List objects2 = context.performQuery("MyQuery", parameters, false);
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Cayenne Documentation > Caching Query Results confluence
- [CONF] Apache Cayenne Documentation > Caching Query Res... confluence
- [CONF] Apache Cayenne Documentation > Caching Query Res... confluence
- [CONF] Apache Cayenne Documentation > Caching Query Res... confluence
- [CONF] Apache Cayenne Documentation > Caching Query Res... confluence
- [CONF] Apache Cayenne Documentation > Caching Query Res... confluence
