JBoss.org Community Documentation
As previously mentioned, unlike a traditional cache system, POJO Cache preserves object identity. This allows for any type of object relationship available in the Java language to be transparently handled.
During the mapping process, all object references are checked to see if they are already stored in the cache. If already stored, instead of duplicating the data, a reference to the original object is written in the cache. All referenced objects are reference counted, so they will be removed once they are no longer referenced.
To look at one example, let's say that multiple
Person
s ("joe" and "mary")
objects can own the same
Address
(e.g., a household). The following diagram is a graphical representation of the pysical cache data.
As can be seen, the "San Jose" address is only stored once.
In the following code snippet, we show programmatically the object sharing example.
import org.jboss.cache.pojo.PojoCache; import org.jboss.cache.pojo.PojoCacheFactory; import org.jboss.test.cache.test.standAloneAop.Person; import org.jboss.test.cache.test.standAloneAop.Address; String configFile = "META-INF/replSync-service.xml"; PojoCache cache = PojoCacheFactory.createCache(configFile); // This will start PojoCache automatically Person joe = new Person(); // instantiate a Person object named joe joe.setName("Joe Black"); joe.setAge(41); Person mary = new Person(); // instantiate a Person object named mary mary.setName("Mary White"); mary.setAge(30); Address addr = new Address(); // instantiate a Address object named addr addr.setCity("Sunnyvale"); addr.setStreet("123 Albert Ave"); addr.setZip(94086); joe.setAddress(addr); // set the address reference mary.setAddress(addr); // set the address reference cache.attach("pojo/joe", joe); // add aop sanctioned object (and sub-objects) into cache. cache.attach("pojo/mary", mary); // add aop sanctioned object (and sub-objects) into cache. Address joeAddr = joe.getAddress(); Address maryAddr = mary.getAddress(); // joeAddr and maryAddr should be the same instance cache.detach("pojo/joe"); maryAddr = mary.getAddress(); // Should still have the address.
If
joe
is removed from
the cache,
mary
should still have reference the same
Address
object in the cache store.
To further illustrate this relationship management, let's examine the Java code under a replicated
environment. Imagine two separate cache instances in the cluster now (cache1
and cache2
). On the first cache instance, both joe
and mary
are attached as above. Then, the application fails over to cache2.
Here is the code
snippet for cache2
(assume the objects were already attached):
/** * Code snippet on cache2 during fail-over */ import org.jboss.cache.PropertyConfigurator; import org.jboss.cache.pojo.PojoCache; import org.jboss.test.cache.test.standAloneAop.Person; import org.jboss.test.cache.test.standAloneAop.Address; String configFile = "META-INF/replSync-service.xml"; PojoCache cache2 = PojoCacheFactory.createCache(configFile); // This will start PojoCache automatically Person joe = cache2.find("pojo/joe"); // retrieve the POJO reference. Person mary = cache2.find("pojo/mary"); // retrieve the POJO reference. Address joeAddr = joe.getAddress(); Address maryAddr = mary.getAddress(); // joeAddr and maryAddr should be the same instance!!! maryAddr = mary.getAddress().setZip(95123); int zip = joeAddr.getAddress().getZip(); // Should be 95123 as well instead of 94086!