In "Struts2 development, use of custom tags-automatic conversion of encoding into chinese meanings", we found that encoding conversion was slow and the page was displayed as slowly as it was during the curtain, the problem is that the background processing service obtains the encoding ID and queries the Chinese meaning of the corresponding encoding in the database. This operation is required for each ID. Of course, the data presentation is slow, therefore, further optimization may be required, so the cache mechanism is immediately considered. Cache is used to optimize data acquisition efficiency. The ehcache cache is used, and the ehcache. jar support package needs to be introduced. It mainly optimizes the code at the database operation layer. 1. Define the cache tool class EhcacheUtil. java: package lsxy. report. tool; import net. sf. ehcache. cacheException; import net. sf. ehcache. cacheManager; public class EhcacheUtil {private EhcacheUtil () {} private static CacheManager cacheManager = null; static {try {cacheManager = CacheManager. create ();} catch (CacheException e) {e. printStackTrace () ;}} public static CacheManager getCacheManager () {return cacheManager;} 2. Optimization after ehcache is introduced to the database operation layer code: public class ReportCodeDao {private BasicDB db = new BasicDB (); /*** obtain the corresponding name based on codeId ** @ param cid * @ return * @ throws CacheException * @ throws IllegalStateException */public String getCodeDesc (String cid) throws Exception {String codedesc = ""; // obtain CacheManager cacheManager = EhcacheUtil. getCacheManager (); // use the colorcache configured in the configuration file to create the cache Cache = CacheManager. getCache ("codeCache"); // check whether the cache contains the cid cache Element = cache. get (cid); // if it does not exist, query if (element = null) from the database) {String SQL = "select codedesc from TBL_REPORT_CODEDESC where cid = '" + cid + "'"; // System. out. println ("query from database! "+ SQL); ResultSet rs = db.exe cuteQuery (SQL); try {if (rs. next () codedesc = rs. getString ("codedesc");} catch (SQLException e) {e. printStackTrace ();} finally {db. closed ();} cache. put (new Element (cid, codedesc);} else {// If yes, load // System from the cache. out. println ("load from cache! "); Codedesc = element. getValue (). toString ();} // System. out. println ("codedesc:" + codedesc); return codedesc;} 3. cache configuration file ehcache. xml <ehcache> <diskStore path = "java. io. codeFile "/> <defaultCache maxElementsInMemory =" 10000 "eternal =" false "timeToIdleSeconds =" 120 "timeToLiveSeconds =" 120 "overflowToDisk =" true "/> <cache name =" codeCache" maxElementsInMemory = "10000" eternal = "true" timeToIdleSeconds = "300 "TimeToLiveSeconds =" 600 "overflowToDisk =" true "/> </ehcache> <! -- The following attributes are required for defaultCache. For details: maxElementsInMemory: sets the maximum value of the objects created in memory; eternal: sets whether the objects in memory reside permanently. If it is true, the object will never die. timeToIdleSeconds: sets the pause time before an element dies, that is, the maximum time interval between two accesses before an element dies. This is only valid when the element is not permanently resident. If the value is 0, it means that the element can pause for an infinite period of time. TimeToLiveSeconds: sets the survival time before an element dies, that is, the maximum time interval between an element's construction and extinction, which is only valid when the element is not permanently resident. OverflowToDisk: sets whether the elements can be written to the disk when the cache in the memory reaches the maxElementsInMemory limit. -->