9.1. Local cache with CacheLoader
This demo shows a local PojoCache with a CacheLoader. We will insert a POJO into the cache, and see that the POJO is transparently saved using the CacheLoader.
To run this, you have to modify jboss-cache/output/etc/META-INF/oodb-service.xml : change CacheLoaderConfig to point to a valid directory (create it if it doesn't yet exist):
<attribute name="CacheLoaderConfig">
location=c:\\tmp\\oodb
</attribute>
Then start the beanshell and source oodb.bsh into it. Note that oodb.bsh already contains code to create and retrieve POJO from the cache. So remember to comment them out if you decide to create the Person instance yourself.
bela@laptop /cygdrive/c/jboss-cache
$ ./runShellDemo.sh
BeanShell 1.3.0 - by Pat Niemeyer (pat@pat.net)
bsh % sourceRelative("oodb.bsh");
interceptor chain is:
class org.jboss.cache.interceptors.CallInterceptor
class org.jboss.cache.interceptors.CacheLoaderInterceptor
class org.jboss.cache.interceptors.TransactionInterceptor
<null>
bsh %
Next, create an instance of Person, and set its address and other fields:
bsh % p=new Person();
<name=null, age=0, hobbies=, address=null, skills=null, languages=null>
bsh % p.age=3;
<3>
bsh % p.name="Michelle";
<Michelle>
bsh % addr=new Address();
<street=null, city=null, zip=0>
bsh % addr.city="San Jose";
<San Jose>
bsh % addr.zip=95124;
<95124>
bsh % addr.street="1704 Almond Blossom Lane";
<1704 Almond Blossom Lane>
bsh % p.setAddress(addr);
bsh % tree.putObject("/person/me", p);
bsh % p;
<name=Michelle, age=3, hobbies=, address=street=1704 Almond Blossom Lane,
city=San Jose, zip=95124, skills=null, languages=null>
bsh %
The Person object with all of its fields and subobjects is now saved. Let's kill beanshell and restart it. At this point, because the instance of Person we created was given the name "p", we can retrieve it again:
bela@laptop /cygdrive/c/jboss-cache
$ ./runShellDemo.sh
BeanShell 1.3.0 - by Pat Niemeyer (pat@pat.net)
bsh % sourceRelative("oodb.bsh");
interceptor chain is:
class org.jboss.cache.interceptors.CallInterceptor
class org.jboss.cache.interceptors.CacheLoaderInterceptor
class org.jboss.cache.interceptors.TransactionInterceptor
<null>
bsh % tree;
</>
bsh % p=tree.getObject("/person/me");
<name=Michelle, age=3, hobbies=, address=street=1704 Almond Blossom Lane,
city=San Jose, zip=95124, skills=null, languages=null>
bsh % tree;
</p
/address
>
bsh %
The interesting thing here is that the cache was initially empty ("/"). Only when we loaded "p", did it get populated (lazy loading). You can see that the values of "p" are loaded from the datastore where they were previously saved.