java cache system_java 缓存框架java caching system使用示例

[Web开发过程中,我们经常会碰到缓存的问题,缓存的合理使用可以使网站性能有大幅度提高(当然需要高的命中率),我这里只是抛砖引玉一下,用OSCache开源框架来实现缓存,

要使用java caching system,需要下面这几个包:jcs.jar,concurrent.jar,commons-logging.jar, commons-lang.jar,commons-collection.jar这几个包,在java工程里面,

首先新建一个使用jcs的配置文件:cache.ccf,文件名不能改。

# DEFAULT CACHE REGION

# sets the default aux value for any non configured caches

jcs.default=

jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes

jcs.default.cacheattributes.MaxObjects=2000

jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache

jcs.default.elementattributes.IsEternal=false

jcs.default.elementattributes.MaxLifeSeconds=3600

jcs.default.elementattributes.IdleTime=1800

jcs.default.elementattributes.IsSpool=true

jcs.default.elementattributes.IsRemote=true

jcs.default.elementattributes.IsLateral=true

# CACHE REGIONS AVAILABLE

# Regions preconfigured for caching

jcs.region.bookCache=

jcs.region.bookCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes

jcs.region.bookCache.cacheattributes.MaxObjects=1200

jcs.region.bookCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache

jcs.region.bookCache.elementattributes.IsEternal=false

jcs.region.bookCache.elementattributes.MaxLifeSeconds=7200

jcs.region.bookCache.elementattributes.IdleTime=1800

jcs.region.bookCache.elementattributes.IsSpool=true

jcs.region.bookCache.elementattributes.IsRemote=true

jcs.region.bookCache.elementattributes.IsLateral=true

# AUXILIARY CACHES AVAILABLE

# Primary Disk Cache -- faster than the rest because of memory key storage

jcs.auxiliary.DC=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory

jcs.auxiliary.DC.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes

jcs.auxiliary.DC.attributes.DiskPath=/usr/opt/bookstore/raf

jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000

jcs.auxiliary.DC.attributes.MaxKeySize=10000

jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000

jcs.auxiliary.DC.attributes.MaxRecycleBinSize=7500

然后新建两个类,

import java.io.Serializable;

import java.util.List;

public class Student implements Serializable{

/**

*

*/

public static final long serialVersionUID = 1L;

public int sno;

public String sname;

public Student()

{}

public int getSno(){

return this.sno;

}

public void setSno(int no){

this.sno=no;

}

public String getSname(){

return this.sname;

}

public void setSname(String name){

this.sname=name;

}

}

import org.apache.jcs.JCS;

public class StuObjManager {

private static StuObjManager instance;

private static int checkedOut=0;

public static JCS stuCache;

private StuObjManager()

{

try

{

stuCache=JCS.getInstance("stuCache");

}

catch(Exception e)

{

e.printStackTrace();

}

}

public static StuObjManager getInstance()

{

synchronized(StuObjManager.class)

{

if(instance==null)

{

instance=new StuObjManager();

}

}

synchronized(instance)

{

instance.checkedOut++;

}

return instance;

}

public Student getStu(int sno)

{

return getStu(sno,true);

}

public Student getStu(int sno,boolean a)

{

Student stu=null;

if(a)

{

stu=(Student)stuCache.get("Stu"+sno);

}

if(stu==null)

{

stu=loadStu(sno);

}

return stu;

}

public Student loadStu(int sno)

{

Student stu=new Student();

stu.sno=sno;

try

{

boolean found=false;

found=true;

if(found)

{

stuCache.put("Stu"+sno, stu);

}

}

catch(Exception e)

{System.out.println("又报错了!");}

return stu;

}

public void storeStu(Student stu)

{

try

{

if(stu.sno!=0)

{

stuCache.remove("Stu"+stu.sno);

}

stuCache.put("Stu"+stu.sno, stu);

}

catch(Exception e)

{}

}

public static void main(String [] args)

{

long last=System.currentTimeMillis();

StuObjManager stu=StuObjManager.getInstance();

for(int i = 0; i < 2000; i++){

stu.loadStu(i);

}

Student stu1=stu.getStu(8,true);

System.out.println(stu1.sno);

System.out.println("所需时间:"+(System.currentTimeMillis()-last));

}

}

运行类StuObjManager里面的main函数,结果如下:

8

所需时间:1250[java缓存框架 说说我用过的: 1.oscache 2.whirlycache 比较: 1.应用场景:   缓存应不应该用硬盘。   缓存的存取策略。 INFO: Size: 176; Questions: 525; Hits: 126;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值