Ehcache结合Spring

本章介绍Ehcache结合Spring实现服务层的缓存,其他还有数据层缓存和页面缓存。

添加依赖


  com.googlecode.ehcache-spring-annotations
  ehcache-spring-annotations 
  1.2.0 

spring配置文件

  
  
    
      
      
      
      
          
          
      
      
      
      
      
      
      
      
      
      
      
          
        
      
      
          
      
  

在Service层注解

import java.util.Map;  
import java.util.concurrent.ConcurrentHashMap;  
import org.springframework.cache.annotation.CacheEvict;  
import org.springframework.cache.annotation.Cacheable;  
import org.springframework.stereotype.Service;  
@Service  
public class UserService {  
    private Map usersData = new ConcurrentHashMap();  
      
    public UserService(){  
        System.out.println("用户数据初始化..开始");  
        usersData.put("1", "test1");  
        usersData.put("2", "test2");  
        System.out.println("用户数据初始化..完毕");  
    }  
      
    //将查询到的数据缓存到movieFindCache中,并使用方法名称加上参数中的userNo作为缓存的key  
    //通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的key,从而不会影响其它缓存值  
    @Cacheable(value="movieFindCache", key="'get'+#userNo")  
    public String get(String userNo){  
        System.out.println("数据库中查到此用户号[" + userNo + "]对应的用户名为[" + usersData.get(userNo) + "]");  
        return usersData.get(userNo);  
    }  
      
    @CacheEvict(value="movieFindCache", key="'get'+#userNo")  
    public void update(String userNo){  
        System.out.println("移除缓存中此用户号[" + userNo + "]对应的用户名[" + usersData.get(userNo) + "]的缓存");  
    }  
      
    //allEntries为true表示清除value中的全部缓存,默认为false  
    @CacheEvict(value="movieFindCache", allEntries=true)  
    public void removeAll(){  
        System.out.println("移除缓存中的所有数据");  
    }  
} 

Controller层

import javax.annotation.Resource;  
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import com.jadyer.service.UserService;  

@Controller  
@RequestMapping("cacheTest")  
public class UserController {  
    @Resource  
    private UserService userService;  
      
    @RequestMapping(value="/get/{userNo}", method=RequestMethod.GET)  
    public String get(@PathVariable String userNo, Model model){  
        String username = userService.get(userNo);  
        model.addAttribute("username", username);  
        return "getUser";  
    }  
      
    @RequestMapping(value="/update/{userNo}", method=RequestMethod.GET)  
    public String update(@PathVariable String userNo, Model model){  
        userService.update(userNo);  
        model.addAttribute("userNo", userNo);  
        return "updateUser";  
    }  
      
    @RequestMapping(value="/removeAll", method=RequestMethod.GET)  
    public String removeAll(){  
        userService.removeAll();  
        return "removeAllUser";  
    }  
}  

要点:
在spring配置文件:
可使用ecache, 用ecache.xml构建cacheManagerFactory,然后把factory注入实现cacheManager。
也可以使用Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的cacheManager(该功能是从Spring3.1开始提供的)。
在service层:
然后使用@Cacheable等注解对缓存进行操作,这些注解都是org.springframework.cache提供的。


参考

  • http://blog.csdn.net/jadyer/article/details/12257865
  • http://www.codingpedia.org/ama/spring-caching-with-ehcache/
  • http://www.captaindebug.com/2012/09/spring-31-caching-and-config.html#.V6IB7Jyriko
  • http://haohaoxuexi.iteye.com/blog/2123030

你可能感兴趣的:(Ehcache结合Spring)