JCS is a subproject of the turbine project of Jakarta. It is a compound buffer tool. It can buffer objects to memory and hard disk. The expiration time of the buffered object is set. JCs can also be used to build a buffer distributed architecture for high-performance applications. Objects that require frequent access and consume resources each time they are accessed can be temporarily stored in the buffer zone to improve service performance. JCS is a good buffer tool. The buffer tool significantly improves the performance of read Operations much more than write operations.
I. Understand the three core concepts of Cache
Elements: JCS is an object cache that can place some objects or "elements" and access them through keys, much like a hashtable. As you can imagine, JCS is a collection of HashTables that can be obtained by name.
Regions: Each HashTables is called "region", and each region is independent of other regions configurations. For example, you can have a region called a city, which caches city objects that are regularly changed. You can define a region called a product and cache regularly changed product data. The regionelements of products that can be configured with ease of Change expire faster than the region of the city.
Auxiliaries: auxiliaries is a plug-in option available for region. The core auxiliaries are indexeddisk cache, tcplateral cache, and remotecache server. For example, the disk cache allows you to swap cache objects to hard when the memory reaches the threshold.
Ii. Download JCs
You can download JCs from the JCS official website and view JCs related documents.
3. Obtain the jar package on which JCs depends
JCs has two necessary jar, namely jcs-1.3.jar and concurrent. jar, both of which can be downloaded from the official website. In addition, we 'd better download the jar packages supported by log4j, because it is so convenient for us to print some logs.
4. Configure JCs
Create the cache. CCF configuration file in the src directory and copy the following content to the file.
# Default cache Region JCs. Default = JCs. Default. cacheattributes = org. Apache. JCs. Engine. compositecacheattributes JCs. Default. cacheattributes. maxobjects = 1000 JCs. Default. cacheattributes. memorycachename = org. Apache. JCs. Engine. Memory. LRU. lrumemorycache |
Note that do not leave spaces after the equal sign "="; otherwise, the cache. CCF will not recognize it.
V. Start programming with JCs
5.1 create a cachewrapper class and use it to add, retrieve, and remove objects.
package com.favccxx.favjcs;import org.apache.jcs.JCS;import org.apache.jcs.access.exception.CacheException;import org.apache.jcs.engine.CacheElement;public class CacheWrapper { public JCS jcsCache = null; public CacheWrapper(JCS cache){ this.jcsCache = cache; } public void put(String key , Object value){ try{ jcsCache.put(key, value); }catch(CacheException e){ e.printStackTrace(); } } public Object get(String key){ CacheElement cacheElement = (CacheElement) jcsCache.getCacheElement(key); if (null != cacheElement) { Object object = cacheElement.val; return object; } return null; } }
5.2 create a cachefactory class and use the factory to manage cache objects to initialize cache and clear cache.
Package COM. favccxx. favjcs; import Java. util. hashmap; import Java. util. map; import Org. apache. JCs. JCs; import Org. apache. JCs. access. exception. cacheexception; import Org. apache. log4j. logger; public class cachefactory {Private Static logger = logger. getlogger (cachefactory. class); Private Static Map <string, cachewrapper> hashmapwrapper = new hashmap <string, cachewrapper> ();/*** get a cache named cachename Object; if not, return NULL * @ Param cachename * @ return */public static cachewrapper getcachewrapper (string cachename) {logger. debug ("Get cachewrapper, The cachename is:" + cachename); Return hashmapwrapper. get (cachename);}/*** clear all the caches */public static void revoke AchE () {object [] cachearray = hashmapwrapper. keyset (). toarray (); For (INT I = 0, L = cachearray. length; I <L; I ++) {try {string cachename = cachea Rray [I]. tostring (); logger. debug ("the cache is below to clear, and the name is:" + cachename); cachewrapper = hashmapwrapper. get (cachename); cachewrapper. jcscache. clear ();} catch (cacheexception e) {logger. debug ("Clear cache error! "); E. printstacktrace () ;}}/ *** gets a cache object named cachename. If it does not exist, create a new cache object * @ Param cachename * @ return */Private Static cachewrapper createcachewrapper (string cachename) {JCs cache = NULL; try {cache = JCs. getinstance (cachename); return New cachewrapper (cache);} catch (cacheexception e) {return NULL ;}} /*** create a cache object * @ Param cachename */Private Static void createhashmapwrapper (string cachename) {hashmapwrapper. put (cachename, createcachewrapper (cachename);}/*** initialize cache object */public static void initcache () {logger. debug ("by START initcache method, we create all the cache object"); createhashmapwrapper ("coolboycache"); // createhashmapwrapper ("beautifulgirl ");}}
5.3 create a test class testcache and test the cache object just now.
package com.favccxx.favjcs.web;import com.favccxx.favjcs.CacheFactory;public class TestCache { /** * @param args */ public static void main(String[] args) { CacheFactory.initCache(); CacheFactory.getCacheWrapper("coolBoy"); }}