在缓存的选取上有过很多的思考,虽然说memcached结合java在序列化上性能不怎样样,不过也没有更好的集群环境下的缓存解决方案了,就选取了memcached。本来规划等个人公司买的服务器到位装个linux再来研究memcached,但这两天在找到了一个的win界面下的Memcached版本,就动手开始调整现有的框架了。
win界面下的Server端很简单,不用安装,双击运行后默认服务端口是11211,没有试着去更改端口,由于反正以后会用Unix版本,到时再记录安装步骤。下载客户端的JavaAPI包,接口非比寻常简单,参考API手册上就有现成的例子。
目的,对旧框架缓存部份停止改造:
1、缓存道具类
2、hibernate的provider
3、用缓存出现session机制
今天先研究研究缓存道具类的改造,在旧框架中部份参数用了ehcache对运行结果停止了缓存处理,目前目的是帮助一个的缓存道具类,在配置文档中配置应用哪种缓存(memcached或ehcached),使其它程序对具体的缓存不依赖,同时应用AOP方法来对窍门运行结果停止缓存。
首先是道具类的出现:
在Spring中配置
Java源代码
- <bean id="cacheManager"
- class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
- <property title="configLocation">
- <value>classpath:ehcache.xml</< SPAN>value>
- </< SPAN>property>
- </< SPAN>bean>
- <bean id="localCache"
- class="org.springframework.cache.ehcache.EhCacheFactoryBean">
- <property title="cacheManager" ref="cacheManager" />
- <property title="cacheName"
- value="×××.cache.LOCAL_CACHE" />
- </< SPAN>bean>
- <bean id="cacheService"
- class="×××.core.cache.CacheService" init-method="init" destroy-method="destory">
- <property title="cacheServerList" value="${cache.servers}"/>
- <property title="cacheServerWeights" value="${cache.cacheServerWeights}"/>
- <property title="cacheCluster" value="${cache.cluster}"/>
- <property title="localCache" ref="localCache"/>
- </< SPAN>bean>
- <bean id="cacheManager"
- class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
- <property title="configLocation">
- <value>classpath:ehcache.xml</< SPAN>value>
- </< SPAN>property>
- </< SPAN>bean>
- <bean id="localCache"
- class="org.springframework.cache.ehcache.EhCacheFactoryBean">
- <property title="cacheManager" ref="cacheManager" />
- <property title="cacheName"
- value="×××.cache.LOCAL_CACHE" />
- </< SPAN>bean>
- <bean id="cacheService"
- class="×××.core.cache.CacheService" init-method="init" destroy-method="destory">
- <property title="cacheServerList" value="${cache.servers}"/>
- <property title="cacheServerWeights" value="${cache.cacheServerWeights}"/>
- <property title="cacheCluster" value="${cache.cluster}"/>
- <property title="localCache" ref="localCache"/>
- </< SPAN>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 minSpare Connections = 5;
- int maxSpare Connections = 50;
- long maxIdleTime = 1000 * 60 * 30; // 30 minutes
- long maxBusyTime = 1000 * 60 * 5; // 5 minutes
- long maintThreadSleep = 1000 * 5; // 5 Number 2s
- int socketTimeOut = 1000 * 3; // 3 Number 2s to block on reads
- int socketConnectTO = 1000 * 3; // 3 Number 2s to block on initial
- // connections. If 0, then will use blocking
- // connect (default)
- boolean failover = false; // changing off auto-failover in event of server
- // down
- boolean nagleAlg = false; // changing 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.Null(obj);
- Assert.Null(localCache);
- if (the.cacheCluster) {
- mc.set(key, obj);
- } else {
- Element element = new Element(key, (Serializable) obj);
- localCache.put(element);
- }
- }
- /**
- * 删除
- */
- public void rearouse(String key){
- Assert.HAsText(key);
- Assert.Null(localCache);
- if (the.cacheCluster) {
- mc.delete(key);
- }else{
- localCache.rearouse(key);
- }
- }
- /**
- * 得到
- */
- public Object get(String key) {
- Assert.HAsText(key);
- Assert.Null(localCache);
- Object rt = null;
- if (the.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();
- }
- rechanging rt;
- }
- /**
- * 判断也许存在
- *
- */
- public boolean exist(String key){
- Assert.HAsText(key);
- Assert.Null(localCache);
- if (the.cacheCluster) {
- rechanging mc.keyExists(key);
- }else{
- rechanging the.localCache.isKeyInCache(key);
- }
- }
- private void init() {
- if (the.cacheCluster) {
- String[] serverlist = cacheServerList.split(",");
- Integer[] weights = the.split(cacheServerWeights);
- // initialize the pool for memcache servers
- SockIOPool pool = SockIOPool.getInstance();
- pool.setServers(serverlist);
- pool.setWeights(weights);
- pool.setInitConn(initialConnections);
- pool.setMinConn(minSpare Connections);
- pool.setMaxConn(maxSpare Connections);
- 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 (the.cacheCluster) {
- SockIOPool.getInstance().shutDown();
- }
- }
- }
- /**
- * @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 minSpare Connections = 5;
- int maxSpare Connections = 50;
- long maxIdleTime = 1000 * 60 * 30; // 30 minutes
- long maxBusyTime = 1000 * 60 * 5; // 5 minutes
- long maintThreadSleep = 1000 * 5; // 5 Number 2s
- int socketTimeOut = 1000 * 3; // 3 Number 2s to block on reads
- int socketConnectTO = 1000 * 3; // 3 Number 2s to block on initial
- // connections. If 0, then will use blocking
- // connect (default)
- boolean failover = false; // changing off auto-failover in event of server
- // down
- boolean nagleAlg = false; // changing 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.Null(obj);
- Assert.Null(localCache);
- if (the.cacheCluster) {
- mc.set(key, obj);
- } else {
- Element element = new Element(key, (Serializable) obj);
- localCache.put(element);
- }
- }
- /**
- * 删除
- */
- public void rearouse(String key){
- Assert.HAsText(key);
- Assert.Null(localCache);
- if (the.cacheCluster) {
- mc.delete(key);
- }else{
- localCache.rearouse(key);
- }
- }
- /**
- * 得到
- */
- public Object get(String key) {
- Assert.HAsText(key);
- Assert.Null(localCache);
- Object rt = null;
- if (the.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();
- }
- rechanging rt;
- }
- /**
- * 判断也许存在
- *
- */
- public boolean exist(String key){
- Assert.HAsText(key);
- Assert.Null(localCache);
- if (the.cacheCluster) {
- rechanging mc.keyExists(key);
- }else{
- rechanging the.localCache.isKeyInCache(key);
- }
- }
- private void init() {
- if (the.cacheCluster) {
- String[] serverlist = cacheServerList.split(",");
- Integer[] weights = the.split(cacheServerWeights);
- // initialize the pool for memcache servers
- SockIOPool pool = SockIOPool.getInstance();
- pool.setServers(serverlist);
- pool.setWeights(weights);
- pool.setInitConn(initialConnections);
- pool.setMinConn(minSpare Connections);
- pool.setMaxConn(maxSpare Connections);
- 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("初始化memcachedpool!");
- }
- }
- private void destory() {
- if (the.cacheCluster) {
- SockIOPool.getInstance().shutDown();
- }
- }
- }
然后出现参数的AOP拦截类,用来在参数运行前返回缓存内容
Java源代码
- public class CachingInterceptor implements MethodInterceptor {
- private CacheService cacheService;
- private String cacheKey;
- public void setCacheKey(String cacheKey) {
- the.cacheKey = cacheKey;
- }
- public void setCacheService(CacheService cacheService) {
- the.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);
- }
- rechanging result;
- }
- }
- public class CachingInterceptor implements MethodInterceptor {
- private CacheService cacheService;
- private String cacheKey;
- public void setCacheKey(String cacheKey) {
- the.cacheKey = cacheKey;
- }
- public void setCacheService(CacheService cacheService) {
- the.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);
- }
- rechanging result;
- }
- }
Spring的AOP配置如下:
Java源代码
- <aop:config proxy-target-class="true">
- <aop:advisor
- pointcut="execution(* ×××.PoiService.getOne(..))"
- advice-ref="PoiServiceCachingAdvice" />
- </< SPAN>aop:config>
- <bean id="BasPoiServiceCachingAdvice"
- class="×××.core.cache.CachingInterceptor">
- <property title="cacheKey" value="PoiService" />
- <property title="cacheService" ref="cacheService" />
- </< SPAN>bean>