Cayenne API

How to clear Apache Cayenne's cache?

In this post we refer to some simple ways - tricks in order to clear Apache Cayenne's cache. Clearing the cache is essential especially when huge datasets are extracted continuously from the database (and passed into objects).

A nice and easy solution for this is to create a new DataContext before executing queries that will fetch huge amounts of data. You just have to create a new object each time (before the query):

DataContext context = new DataContext.createDataContext();

and then use this for your query:

List queryRows = context.performQuery(selectQuery);

Another way, which is not always very effective but it can be used together with the previous one is to directly clear the data rows from the object store cache.

context.getObjectStore().getDataRowCache().clear();

In heavy duty applications you should always adjust the memory for the java heap using the runtime arguments -Xms128m (mininum memory size set to 128Mb) and -Xmx512m (maximum memory size set to 512 Mb). The values (128 and 512) are just an example (you should test your code and find out the right numbers for these values).

Syndicate content