码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Cache Demo

时间:2016-04-03 00:24:28      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

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

}

 

Spring Cache Demo

标签:

原文地址:http://www.cnblogs.com/zouhao510/p/5348608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!