public class PersonEntity extends GenericEntity {
ObjectCache cache;
public PersonEntity(CacheFactory cacheFactory) {
cache = cacheFactory.createCache("Person");
// * Other initialization here
}
...
public Person get(long key) {
Long cacheKey = new Long(key);
Person person = (Person)cache.get(cachekey);
if (person == null) {
// * Get the object from the database
if (person != null) {
// Put it in the cache
cache.put(cacheKey, person);
}
}
return person;
}
...
public void insert(Person person) {
// * Do database insert
// Add new object to cache
Long cacheKey = new Long(person.getKey());
cache.put(cacheKey, person);
}
...
public void update(Person person) {
// * Do database update
// Remove object from cache
// (This causes all caches in the group to be notified)
Long cacheKey = new Long(person.getKey());
cache.clear(cacheKey);
// Re-add the object to the cache
cache.put(cacheKey, person);
}
...
public void delete(long key) {
// * Do database delete
// Remove object from cache
// (This causes all caches in the group to be notified)
Long cacheKey = new Long(key);
cache.clear(cacheKey);
}
...
}
SwarmCache Project Home Page
http://swarmcache.sourceforge.net
SwarmCache Documentation
http://swarmcache.sourceforge.net/documentation.html
Download SwarmCache
http://sourceforge.net/project/showfiles.php?group_id=78637&release_id=193220
Permalink