`
hanqunfeng
  • 浏览: 1543106 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring Cache注解+Memcached

阅读更多

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();
		}
	}

	

}

 

1
3
分享到:
评论
2 楼 guan_hua 2015-11-19  
为什么我这样配制的报错了,

警告: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in URL [file:/E:/JavaDeveloperWorkspace/work1/redis_demo/target/classes/spring/spring-cache.xml]: Cannot create inner bean 'com.gh.redis_demo.utils.cache.memcached.MemCache#3d8314f0' of type [com.gh.redis_demo.utils.cache.memcached.MemCache] while setting bean property 'caches' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.gh.redis_demo.utils.cache.memcached.MemCache#3d8314f0' defined in URL [file:/E:/JavaDeveloperWorkspace/work1/redis_demo/target/classes/spring/spring-cache.xml]: Cannot resolve reference to bean 'defaultCache' while setting bean property 'cache'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultCache': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.google.code.ssm.aop.CacheBase com.google.code.ssm.CacheFactory.cacheBase; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.google.code.ssm.aop.CacheBase] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:276)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:278)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
1 楼 tuspark 2015-04-25  
文章讲解的感觉不太详细,不如这篇文章: Spring中@Cacheable的用法

相关推荐

    (SSM框架)memcached整合Spring基于Cache注解.

    在SSM框架中引入Memcached并基于Spring的Cache注解进行整合,可以实现高效、分布式的数据缓存,提升系统性能。下面将详细阐述这一过程中的关键知识点。 1. **Memcached介绍**: Memcached是一款高性能、分布式的...

    SSM与memcached整合项目Spring Cache

    在本项目中,我们主要探讨的是如何将Spring Cache与memcached进行整合,以提升应用程序的性能和效率。Spring Cache是Spring框架的一部分,它提供了一种抽象的缓存管理机制,可以方便地集成到各种缓存解决方案中,如...

    spring-boot-tomcat-memcached:Spring Boot Tomcat + Memcached 示例

    - Spring Cache抽象提供了基于注解的缓存支持,如`@Cacheable`、`@CacheEvict`、`@CachePut`和`@Caching`,它们分别用于缓存方法结果、清除缓存、更新缓存以及组合缓存操作。 - `@Cacheable`注解用于缓存方法的...

    Spring整合memcached完整项目代码

    本文将深入探讨如何将Spring与Memcached整合,以及如何使用Spring Cache注解实现缓存功能。 首先,我们需要理解Spring Cache的基本概念。Spring Cache是一个抽象层,它允许我们在不依赖特定缓存实现的情况下使用...

    springcache-1.1.0.zip

    SpringCache是基于注解驱动的,允许开发者通过在方法上添加`@Cacheable`、`@CacheEvict`等注解来启用缓存功能。`@Cacheable`用于缓存方法的返回结果,`@CacheEvict`则用于清除缓存。此外,`@Caching`注解可以用于...

    Spring memcached 源码

    Spring Memcached 是一个用于在Spring应用中集成Memcached缓存服务的框架。Memcached是一种分布式内存对象缓存系统,常用于提高网站数据读取的速度,因为它可以将数据存储在内存中,避免了频繁的数据库查询。现在...

    SSM框架Spring memcached整合基于注解形式

    Spring框架提供了对memcached的支持,主要通过`spring-cache`模块实现。首先,我们需要在项目的`pom.xml`文件中添加`spring-context-support`和`spymemcached`库的依赖。`spymemcached`是Java客户端库,用于连接和...

    使用spring aop对web 应用数据进行memcached缓存

    - 使用`@Cacheable`、`@CacheEvict`、`@CachePut`等Spring Cache注解可以方便地管理缓存操作。 - `@Cacheable`注解用于标记方法,表示其结果应该被缓存。如果缓存中存在对应键的结果,则直接返回,否则执行方法并...

    spring调用memcached client for java

    &lt;bean id="cacheManager" class="org.springframework.cache.memcached.MemcachedCacheManager"&gt; &lt;property name="memcachedClient" ref="memcachedClient" /&gt; ``` 3. **启用缓存注解**:在Spring配置中开启...

    spring整合memcached

    5. **使用Memcached**:在业务代码中,你可以通过@Autowired注解注入MemcachedTemplate,并使用它进行数据的缓存操作。例如: ```java @Autowired private MemcachedTemplate, Object&gt; memcachedTemplate; ...

    Memcached spring 的集成

    - 在代码中,通过Spring的缓存注解,可以自动地将数据存入和取出Memcached。在实际应用中,可以通过设置不同的缓存策略,如基于方法的缓存、基于类的缓存等,以优化性能。 7. **性能优化**: - 缓存大小:根据...

    Memcached与Spring、Mybatis集成.doc

    - 启用了AspectJ代理机制,这对于使用simple-spring-memcached中的注解非常关键。 - 定义了一个名为`defaultMemcachedClient`的Bean,它是simple-spring-memcached中的核心组件之一,负责创建和管理Memcached客户端...

    spring memcached

    ### Spring 3.1 对 Memcached 的集成与应用 #### 一、Spring 集成缓存概述 从 Spring 3.1 开始,Spring 框架正式引入了对缓存的支持,使得开发者能够更加方便地管理和使用缓存技术。这一特性极大地简化了缓存的...

    memcached集成Spring安装部署文档

    **Memcached与Spring集成安装部署指南** Memcached是一款高性能、分布式的内存对象缓存系统,常用于减轻数据库负载,提高Web应用性能。Spring框架则是一个广泛使用的Java企业级应用开发框架,提供了丰富的功能和...

    memcached在SSH中的配置

    标题“memcached在SSH中的配置”指的是如何在SSH(Spring Security、Struts2和Hibernate)这个Java Web开发框架组合中集成和配置缓存系统memcached。memcached是一个高性能、分布式内存对象缓存系统,常用于加速动态...

    arduino-testsuite-2.9.zip

    4. **Spring与Memcached集成**:通过Spring Cache的Memcached模块,开发者可以在Spring应用中透明地使用Memcached服务。配置完成后,应用可以通过注解或编程方式将方法结果缓存到Memcached,减少对后端数据库的依赖...

    simple-spring-memcached统一缓存的使用实例[整理].pdf

    《Simple-Spring-Memcached统一缓存的使用详解》 在Java应用中,尤其是在中大型项目中,有效地管理和使用缓存对于提升系统性能至关重要。Simple-Spring-Memcached(SSM)是一个流行的缓存框架,它整合了Spring框架...

    JetCache是一个基于Java的缓存系统封装提供统一的API和注解来简化缓存的使用

    JetCache 支持多种缓存实现,包括本地缓存(如 Guava Cache)和分布式缓存(如 Redis、Memcached),并且提供了丰富的配置选项,可以根据实际需求进行定制。 **核心特性** 1. **统一的 API 和注解**:JetCache ...

    spring boot 实践学习案例,与其它组件整合

    - Spring Boot 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - springboot-templates - Spring Boot 模板,包括thymeleaf、freemarker、jsp、表单校验 等。 - ...

Global site tag (gtag.js) - Google Analytics