cache策略(一)基类

cache策略(一)基类
这个设计策略参考了很多同事的代码
cache的配置文件applicationContext-cache.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="cacheManager"
class="com.sillycat.easyview.plugin.cache.base.CacheManagerImpl">
<property name="cacheProviders">
<map>
<entry key="OSCACHE">
<ref local="osCacheProvider" />
</entry>
<entry key="HASHMAPCACHE">
<ref local="hashMapCacheProvider" />
</entry>
<entry key="MEMCACHE">
<ref local="memCacheProvider" />
</entry>
</map>
</property>
</bean>

<!-- 定义 oscache 的提供类 -->
<bean id="osCacheProvider"
class="com.sillycat.easyview.plugin.cache.oscache.OsCacheProvider" init-method="start"/>

<!-- 定义 hashMapcache 的提供类 -->
<bean id="hashMapCacheProvider"
class="com.sillycat.easyview.plugin.cache.hashmap.HashMapCacheProvider" init-method="start"/>

<!-- 定义 memcached 的提供类 -->
<bean id="memCacheProvider"
class="com.sillycat.easyview.plugin.cache.memcached.MemCacheProvider" init-method="start"/>
</beans>

其中比较重要的是CacheManagerImpl.java,它可以选择用不同的实现去做缓存,
以后如果要新加入一个cache的类型,也只是增加一个provider而已
CacheManagerImpl.java如下:
package com.sillycat.easyview.plugin.cache.base;

import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sillycat.easyview.plugin.cache.CacheManager;
import com.sillycat.easyview.plugin.commons.exceptions.CacheException;

/**
* 用某种策略新建一个CACHE,具体的CACHE建立使用具体的CACHE PROVIDE BUILDER类。
*/
public final class CacheManagerImpl implements CacheManager {
private static final Log log = LogFactory.getLog(CacheManagerImpl.class);

// 不同种CACHE提供的CACHE实始化实现��ͬ��CACHE�ṩ��CACHEʵʼ��ʵ��
private Map cacheProviders;

private CacheManagerImpl() {
}

// 通常的CACHE提供以下四种的CACHE策略,性能以 READ_ONLY 为最高���
public static final String READ_ONLY = "READ_ONLY";
public static final String READ_WRITE = "READ_WRITE"; // read-write
public static final String NONSTRICT_READ_WRITE = "NONSTRICT_READ_WRITE"; // nonstrict-read-write
public static final String TRANSACTIONAL = "TRANSACTIONAL"; // transactional

public BaseCache createNewCache(final String concurrencyStrategy,
String cacheProvider, String regionName)
throws CacheException {
BaseCache cacheStractegy = null;

// 创建cache策略类
if (concurrencyStrategy.equals(READ_ONLY)) {
cacheStractegy = new ReadOnlyCache();
}
// 创建实际的cache
CacheProvider provider = (CacheProvider) cacheProviders
.get(cacheProvider);
Cache cache = provider.buildCache(regionName);

cacheStractegy.setCache(cache);
return cacheStractegy;
}

/**
* 设置cache提供者列表
*/
public Map getCacheProviders() {
return cacheProviders;
}

public void setCacheProviders(Map cacheProviders) {
this.cacheProviders = cacheProviders;
}
}

使用时只需要选择不同的provider就可以了,测试用例CacheManagerTest.java如下:
package com.sillycat.easyview.plugin.cache;

import com.sillycat.easyview.core.model.User;
import com.sillycat.easyview.plugin.cache.base.BaseCache;
import com.sillycat.easyview.plugin.cache.base.Cache;
import com.sillycat.easyview.plugin.cache.base.CacheManagerImpl;
import com.sillycat.easyview.plugin.commons.base.ServiceTestBase;
import com.sillycat.easyview.plugin.commons.exceptions.CacheException;

public class CacheManagerTest extends ServiceTestBase {

private CacheManager cacheManager;

protected void setUp() throws Exception {
super.setUp();
cacheManager = (CacheManager) appContext.getBean("cacheManager");
}

protected void tearDown() throws Exception {
super.tearDown();

}

public void testDumy() {
assertTrue(true);
}

public void testOSCache() {
BaseCache baseCache = null;
try {
baseCache = cacheManager.createNewCache(CacheManagerImpl.READ_ONLY,
"OSCACHE", "www.sillycat.com");
} catch (CacheException e) {
e.printStackTrace();
}
User u = new User();
u.setId(new Integer("1234"));
u.setLogonName("sillycat");
u.setEmail("magic_dreamer@126.com");

Cache cache = baseCache.getCache();
try {
cache.put(u.getId(), u);
User t = (User) cache.get(u.getId());
System.out.println(t.getLogonName());
System.out.println(t.getEmail());
} catch (CacheException e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值