Setting up the cache layer is quite complicated topic. Because it was already described in user guide in [03] we will provide only a few basic examples.

In the previous sections we learned how to use the VoidCache. Similarly we could use the single-threaded variants of the other two cache implementations provided - the ArcCacheST and LruCacheST caches:

db = new CachedDatabase(
  DriverManager::getDriver("IopcOracle10g").getDatabase("XE"), 
  new HashArcCacheST());
db = new CachedDatabase(
  DriverManager::getDriver("IopcOracle10g").getDatabase("XE"), 
  new HashLruCacheST());

To take advantage of the cache/strategy selector facade and asynchronous cache maintenance provided by CacheKeeper we must first associate the ComposedCache (no other cache) with the connections. Then we will define cache and strategy selection rules (see the section called “The cache layer”) on each connection and start the maintainer thread:

db = new CachedDatabase(
  DriverManager::getDriver("IopcOracle10g").getDatabase("XE"), 
  new ComposedCache());
conn = (CachedConnection*)db->getConnection(
  "username=iopc;password=iopc;autocommit=false");
CacheKeeper cacheKeeper(-15000, 100, 200);
cacheKeeper.setCache(conn, TypeDesc<Employee>::getType(), new HashArcCache());
cacheKeeper.setCache(conn, new HashLruCache());
cacheKeeper.setStrategy(conn, Strategy::lazy);
cacheKeeper.runMaintainer();

In the example, we associated the HashArcCache (multithreaded variant) with the Employee class. Instances of this class manipulated on the conn connection will use this cache. All other persistent objects will use the default HashLruCache. The lazy strategy will be used for objects of all types. For other strategy types and their descriptions see [03]. CacheKeeper constructor allows to specify maximum cache capacities and other parameters that influence the cache maintenance.