当前位置:首页 > 开发 > 编程语言 > Spring > 正文

Spring Cache注解+Memcached

发表于: 2015-01-12   作者:hanqunfeng   来源:转载   浏览:
摘要: Spring3.1 Cache注解  依赖jar包: <!-- simple-spring-memcached --> <dependency> <groupId>com.google.code.simple-spring-memcached</groupId> <artifactId>simple-s

Spring3.1 Cache注解 

依赖jar包:

<!-- simple-spring-memcached -->
		<dependency>
			<groupId>com.google.code.simple-spring-memcached</groupId>
			<artifactId>simple-spring-memcached</artifactId>
			<version>3.5.0</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.simple-spring-memcached</groupId>
			<artifactId>xmemcached-provider</artifactId>
			<version>3.5.0</version>
		</dependency>
		<dependency>
			<groupId>com.googlecode.xmemcached</groupId>
			<artifactId>xmemcached</artifactId>
			<version>2.0.0</version>
		</dependency>

 

applicationContext-cache-memcached.xml

<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
	<cache:annotation-driven cache-manager="cacheManager" />
	
	<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
	<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
		<property name="caches">
			<set>
				<bean class="org.cpframework.core.cache.memcached.MemcachedCache">
					<property name="cache" ref="defaultCache"/>
				</bean>
				<bean class="org.cpframework.core.cache.memcached.MemcachedCache">
					<property name="cache" ref="commonCache"/>
				</bean>
			</set>
		</property>
	</bean>


	<!-- memcached client defaultCache -->
	<bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
		<property name="cacheClientFactory">
			<bean
				class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
		</property>
		<property name="addressProvider">
			<bean class="com.google.code.ssm.config.DefaultAddressProvider">
				<property name="address" value="192.168.xx.xx:11211" />
			</bean>
		</property>
		<property name="configuration">
			<bean class="com.google.code.ssm.providers.CacheConfiguration">
				<property name="consistentHashing" value="true" />
			</bean>
		</property>
		<property name="cacheName" value="default"/>
		
	</bean>
	<!-- memcached client commonCache -->
	<bean name="commonCache" class="com.google.code.ssm.CacheFactory">
		<property name="cacheClientFactory">
			<bean
				class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
		</property>
		<property name="addressProvider">
			<bean class="com.google.code.ssm.config.DefaultAddressProvider">
				<property name="address" value="192.168.xx.xx:11211" />
			</bean>
		</property>
		<property name="configuration">
			<bean class="com.google.code.ssm.providers.CacheConfiguration">
				<property name="consistentHashing" value="true" />
			</bean>
		</property>
		<property name="cacheName" value="commonCache"/>
		
	</bean>

 

MemcachedCache.java

package org.cpframework.core.cache.memcached;

import java.util.concurrent.TimeoutException;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;

import com.google.code.ssm.api.format.SerializationType;
import com.google.code.ssm.providers.CacheException;



public class MemcachedCache implements Cache{
	
	private com.google.code.ssm.Cache cache;
	
	public com.google.code.ssm.Cache getCache() {
		return cache;
	}

	public void setCache(com.google.code.ssm.Cache cache) {
		this.cache = cache;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return this.cache.getName();
	}

	@Override
	public Object getNativeCache() {
		// TODO Auto-generated method stub
		return this.cache;
	}

	@Override
	public ValueWrapper get(Object key) {
		// TODO Auto-generated method stub
		Object object = null;
		try {
			object = this.cache.get((String)key, SerializationType.JAVA);
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CacheException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (object != null ? new SimpleValueWrapper(object) : null);
	}

	@Override
	public void put(Object key, Object value) {
		// TODO Auto-generated method stub
		try {
			this.cache.set((String)key, 86400, value, SerializationType.JAVA);
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CacheException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void evict(Object key) {
		// TODO Auto-generated method stub
		try {
			this.cache.delete((String)key);
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CacheException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		try {
			this.cache.flush();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CacheException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	

}

 

Spring Cache注解+Memcached

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号