java ehcache list_java缓存ehcache ---- spring aop

在Spring中配置

Java代码

class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

classpath:ehcache.xmlvalue>

property>

bean>

class="org.springframework.cache.ehcache.EhCacheFactoryBean">

value="×××.cache.LOCAL_CACHE" />

bean>

class="×××.core.cache.CacheService" init-method="init" destroy-method="destory">

bean>

class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

classpath:ehcache.xmlvalue>

property>

bean>

class="org.springframework.cache.ehcache.EhCacheFactoryBean">

value="×××.cache.LOCAL_CACHE" />

bean>

class="×××.core.cache.CacheService" init-method="init" destroy-method="destory">

bean>

在properties文件中配置${cache.servers} ${cache.cacheServerWeights} ${cache.cluster}

具体工具类的代码

Java代码

/**

* @author Marc

*

*/

public class CacheService {

private Log logger = LogFactory.getLog(getClass());

private Cache localCache;

String cacheServerList;

String cacheServerWeights;

boolean cacheCluster = false;

int initialConnections = 10;

int minSpareConnections = 5;

int maxSpareConnections = 50;

long maxIdleTime = 1000 * 60 * 30; // 30 minutes

long maxBusyTime = 1000 * 60 * 5; // 5 minutes

long maintThreadSleep = 1000 * 5; // 5 seconds

int socketTimeOut = 1000 * 3; // 3 seconds to block on reads

int socketConnectTO = 1000 * 3; // 3 seconds to block on initial

// connections. If 0, then will use blocking

// connect (default)

boolean failover = false; // turn off auto-failover in event of server

// down

boolean nagleAlg = false; // turn off Nagle's algorithm on all sockets in

// pool

MemCachedClient mc;

public CacheService(){

mc = new MemCachedClient();

mc.setCompressEnable(false);

}

/**

* 放入

*

*/

public void put(String key, Object obj) {

Assert.hasText(key);

Assert.notNull(obj);

Assert.notNull(localCache);

if (this.cacheCluster) {

mc.set(key, obj);

} else {

Element element = new Element(key, (Serializable) obj);

localCache.put(element);

}

}

/**

* 删除

*/

public void remove(String key){

Assert.hasText(key);

Assert.notNull(localCache);

if (this.cacheCluster) {

mc.delete(key);

}else{

localCache.remove(key);

}

}

/**

* 得到

*/

public Object get(String key) {

Assert.hasText(key);

Assert.notNull(localCache);

Object rt = null;

if (this.cacheCluster) {

rt = mc.get(key);

} else {

Element element = null;

try {

element = localCache.get(key);

} catch (CacheException cacheException) {

throw new DataRetrievalFailureException("Cache failure: "

+ cacheException.getMessage());

}

if(element != null)

rt = element.getValue();

}

return rt;

}

/**

* 判断是否存在

*

*/

public boolean exist(String key){

Assert.hasText(key);

Assert.notNull(localCache);

if (this.cacheCluster) {

return mc.keyExists(key);

}else{

return this.localCache.isKeyInCache(key);

}

}

private void init() {

if (this.cacheCluster) {

String[] serverlist = cacheServerList.split(",");

Integer[] weights = this.split(cacheServerWeights);

// initialize the pool for memcache servers

SockIOPool pool = SockIOPool.getInstance();

pool.setServers(serverlist);

pool.setWeights(weights);

pool.setInitConn(initialConnections);

pool.setMinConn(minSpareConnections);

pool.setMaxConn(maxSpareConnections);

pool.setMaxIdle(maxIdleTime);

pool.setMaxBusyTime(maxBusyTime);

pool.setMaintSleep(maintThreadSleep);

pool.setSocketTO(socketTimeOut);

pool.setSocketConnectTO(socketConnectTO);

pool.setNagle(nagleAlg);

pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);

pool.initialize();

logger.info("初始化memcached pool!");

}

}

private void destory() {

if (this.cacheCluster) {

SockIOPool.getInstance().shutDown();

}

}

}

然后实现函数的AOP拦截类,用来在函数执行前返回缓存内容

Java代码

public class CachingInterceptor implements MethodInterceptor {

private CacheService cacheService;

private String cacheKey;

public void setCacheKey(String cacheKey) {

this.cacheKey = cacheKey;

}

public void setCacheService(CacheService cacheService) {

this.cacheService = cacheService;

}

public Object invoke(MethodInvocation invocation) throws Throwable {

Object result = cacheService.get(cacheKey);

//如果函数返回结果不在Cache中,执行函数并将结果放入Cache

if (result == null) {

result = invocation.proceed();

cacheService.put(cacheKey,result);

}

return result;

}

}

public class CachingInterceptor implements MethodInterceptor {

private CacheService cacheService;

private String cacheKey;

public void setCacheKey(String cacheKey) {

this.cacheKey = cacheKey;

}

public void setCacheService(CacheService cacheService) {

this.cacheService = cacheService;

}

public Object invoke(MethodInvocation invocation) throws Throwable {

Object result = cacheService.get(cacheKey);

//如果函数返回结果不在Cache中,执行函数并将结果放入Cache

if (result == null) {

result = invocation.proceed();

cacheService.put(cacheKey,result);

}

return result;

}

}

Spring的AOP配置如下:

Java代码

pointcut="execution(* ×××.PoiService.getOne(..))"

advice-ref="PoiServiceCachingAdvice" />

class="×××.core.cache.CachingInterceptor">

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值