JCS 是JAVA 中緩衝的一種實現,支援將資料緩衝到記憶體或硬碟中,支援設定緩衝對象的有效時間長度。使用時需要用到以下幾個jar包:
commons-collections-2.1.1.jar
commons-lang.2.3.jar
commons-logging-1.0.4.jar
concurrent-1.3.2.jar
jcs-1.3.jar
slf4j-api.jar
定義實現了Serializable 介面的儲存物件類:
public class UserInfo implements Serializable{<br /> private String username;<br /> private String domain;</p><p> public UserInfo(String name){<br /> this.username = name;<br /> }</p><p> public UserInfo(String name,String domain){<br /> this.username= name;<br /> this.domain =domain;<br /> }<br /> }
定義儲存類:
public class UserManager{<br /> private JCS jcscache;<br /> private final String NAME_SPACE="userinfo";</p><p> private static class UserManagerContainer{<br /> private static UserManager instance = new UserManager();<br /> }</p><p> public static UserManager getInstance(){<br /> return UserManagerContainer.instance<br /> }</p><p> private UserManager(){<br /> try{ </p><p> jcscache= JCS.getInstance(NAME_SPACE);</p><p> }<br /> catch(CacheException e){<br /> }<br /> }</p><p> public UserInfo get(String key){<br /> return (UserInfo) jcscache.get(key);<br /> }</p><p> pubilc void put(String key,UserInfo info,boolean isoverride){<br /> try{<br /> if(isoverride){<br /> jcscache.put(key,info);<br /> }<br /> else{<br /> jcscache.putSafe(key,info);<br /> }<br /> }<br /> catch(CacheException e){</p><p> }<br /> }<br />}
定義設定檔:cache.ccf
jcs.default=DC<br />jcs.defaultcacheattributes=org.engine.CompositeCacheAttributes<br />jcs.defaultcacheattributes.MaxObjects=500000<br />jcs.defaultcacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache<br />jcs.defaultcacheattributes.UseMemoryShrinker=true<br />jcs.defaultcacheattributes.MaxMemoryIdleTimeSeconds=1200<br />jcs.defaultcacheattributes.ShrinkerIntervalSeconds=30<br />jcs.defaultcacheattributes.MaxSpoolPerRun=500<br />jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes<br />jcs.default.elementattributes.IsEternal=false<br />jcs.auxiliary.DC=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory<br />jcs.auxiliary.DC.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttribute<br />jcs.auxiliary.DC.attributes.DiskPatch=d:/memory