您的位置:首页 > 编程语言 > Java开发

Spring Cache Demo

2016-04-02 23:35 531 查看
1,配置cacheManager,applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-4.0.xsd"> 
<context:annotation-config />

<context:component-scan base-package="com.zouhao.spring.cache" />

<cache:annotation-driven/>

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="default"/>
</bean>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="baseCache"/>
</bean>
</set>
</property>
</bean>
</beans>


2,测试实现Controller

package com.zouhao.spring.cache.controller;
import javax.xml.ws.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zouhao.spring.cache.entity.CacheObject;
import com.zouhao.spring.cache.service.SpringCacheService;

@Controller
@RequestMapping("/cache")
public class SpringCacheController {

@Autowired
private SpringCacheService springCacheService;

@ResponseBody
@RequestMapping(value = "/getCache", method= RequestMethod.GET)
private String getCacheById(@RequestParam("id") Integer id){

return JSON.toJSONString(springCacheService.getCacheObject(id));
}

@ResponseBody
@RequestMapping(value = "/removeCache", method= RequestMethod.GET)
private String clearCacheById(@RequestParam("id") Integer id){

springCacheService.clearCacheObjectById(id);
return "clear success";
}

@ResponseBody
@RequestMapping(value = "/updateCache", method= RequestMethod.GET)
private String updateCacheById(@RequestParam("id") Integer id){

springCacheService.updateCacheObject(id);
return "update success";
}

}


3,测试实现Service

package com.zouhao.spring.cache.service;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.zouhao.spring.cache.entity.CacheObject;

@Service
public class SpringCacheService {

//将查询结果通过id作为key缓存
@Cacheable(key="#id",value="baseCache")
public CacheObject getCacheObject(Integer id) {
System.out.println(String.format("query CacheObject from DB by id:%d",id));
CacheObject obj = newCacheObject(id);
return obj;
}

// 更新 accountCache 缓存
@CachePut(value="baseCache",key="#id")
public CacheObject updateCacheObject(Integer id) {
System.out.println(String.format("update the CacheObject by id:%d", id));
return updateCacheObjectFromDB(id);
}

//清空缓存
@CacheEvict(value="baseCache",key="#id")
public void clearCacheObjectById(Integer id) {
}

private CacheObject updateCacheObjectFromDB(Integer id) {
System.out.println(String.format("get CacheObject from cache by id:%d ", id));
CacheObject obj = getCacheObject(id);
System.out.println(String.format("set CacheObject name field from cache"));
obj.setName("xxxxxxxxxxxxxxxxxxxx");
return obj;
}

public CacheObject newCacheObject(Integer id){

CacheObject obj = new CacheObject();
obj.setId(id);
obj.setName("test");
obj.setPass("pass");
return obj;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: