Taglib utilisable dans les JSP (cache par fragments)
Permet de bloquer les accès jusqu’à la fin d’une maj
Servlet Filter pour le cache
Utilisation avec fail-over
1234567891011121314151617181920
StringmyKey="myKey";StringmyValue;intmyRefreshPeriod=1000;try{// Get from the cachemyValue=(String)admin.getFromCache(myKey,myRefreshPeriod);}catch(NeedsRefreshExceptionnre){try{// Get the value (probably from the database)myValue="This is the content retrieved.";// Store in the cacheadmin.putInCache(myKey,myValue);}catch(Exceptionex){// We have the current content if we want fail-over.myValue=(String)nre.getCacheContent();// It is essential that cancelUpdate is called if the// cached content is not rebuiltadmin.cancelUpdate(myKey);}}
Utilisation sans fail-over
123456789101112131415161718192021
StringmyKey="myKey";StringmyValue;intmyRefreshPeriod=1000;try{// Get from the cachemyValue=(String)admin.getFromCache(myKey,myRefreshPeriod);}catch(NeedsRefreshExceptionnre){try{// Get the value (probably from the database)myValue="This is the content retrieved.";// Store in the cacheadmin.putInCache(myKey,myValue);updated=true;}finally{if(!updated){// It is essential that cancelUpdate is called if the// cached content could not be rebuiltadmin.cancelUpdate(myKey);}}}
publicclassTestServletextendsHttpServlet{privatestaticLoggerlogger=Logger.getLogger(TestServlet.class.getName());privateCacheManagermanager=null;privateCachecache=null;publicvoidinit()throwsServletException{// Configuration de log4J.PropertyConfigurator.configure(getServletContext().getRealPath("WEB-INF/log4j.properties"));// Creating the cache manager from configuration filetry{manager=CacheManager.create(getServletContext().getRealPath("WEB-INF/ehCache.xml"));cache=manager.getCache("sampleCache1");}catch(Exceptione){logger.error("Unable to load EHCACHE configuration file");}}publicvoiddestroy(){}protectedvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{// Creation du writer de sortie.PrintWriterout=response.getWriter();// Creation d'un objetObjectmyObject=newObject();// Mise en cache de cet elementElementelement=newElement("testID",myObject);cache.put(element);out.println("Objet mis en cache`<br/>`");// Lecture du cache pour extraire l'objettry{element=cache.get("testID");}catch(Exceptione){}ObjectmySecondObject=element.getValue();}}
publicclassPersonCacheextendsGenericEntity{ObjectCachecache;publicPersonEntity(CacheFactorycacheFactory){cache=cacheFactory.createCache("Person");// * Other initialization here}publicPersonget(longkey){LongcacheKey=newLong(key);Personperson=(Person)cache.get(cachekey);if(person==null){// * Get the object from the database if(person!=null){// Put it in the cachecache.put(cacheKey,person);}}returnperson;}publicvoidinsert(Personperson){// * Do database insert// Add new object to cacheLongcacheKey=newLong(person.getKey());cache.put(cacheKey,person);}publicvoidupdate(Personperson){// * Do database update// Remove object from cache// (This causes all caches in the group to be notified)LongcacheKey=newLong(person.getKey());cache.clear(cacheKey);// Re-add the object to the cachecache.put(cacheKey,person);}publicvoiddelete(longkey){// * Do database delete// Remove object from cache// (This causes all caches in the group to be notified)LongcacheKey=newLong(key);cache.clear(cacheKey);}}
Permet de cacher une arborescence d’objets “TreeCache”
Fonctionne avec Hibernate
Utilisation du cache POJO
12345678
ArrayListlist=newArrayList();list.add("first");cache.putObject("/list/test",list);// Put the list under the aop cachelist.add("second");// Won't work since AOP intercepts the dynamic proxy not the original POJO.ArrayListmyList=(List)cache.getObject("/list/test");// we are getting a dynamic proxy insteadmyList.add("second");// it works nowmyList.add("third");myList.remove("third");