本文主要是介绍spring缓存机制-@CachePut的用法(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
spring是一个轻量级开源框架,以IoC(Inverse of Control:控制反转)和AOP(Aspect Oriented Programming:面向切面编程)为内核,兼具功能强大的的原生展现层spring mvc,原生持久层spring jdbc和原生业务层等技术,并且以海纳百川的胸怀整合了开源世界里众多
著名的第三方框架和类库,已经逐渐成为世界上使用最多的JavaEE企业级应用开源框架.
在使用spring缓存技术过程中,有时候我们既希望方法被调用,同时又希望结果被缓存.例如记录日志,调用接口等,
在这种情况下需要用到@CachePut 注释.
1.通过maven构建项目,直接看pom.xml文件(略:参考相关博客)
2.创建service,dao和model,并将spring缓存加在service层,直接看代码UserService,UserServiceImpl,UserDao,UserDaoImpl和User.(略:参考相关博客)
在UserServiceImpl代码中通过@CachePut既保证方法被调用,同时又保证结果被缓存.
<span style="font-size:12px;">package com.ilucky.spring.cache.service.impl;import java.util.Map;import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;import com.ilucky.spring.cache.dao.UserDao;
import com.ilucky.spring.cache.model.User;
import com.ilucky.spring.cache.service.UserService;/*** @author IluckySi* @date 20140613*/
public class UserServiceImpl implements UserService {private UserDao userDao;/*** JVM加载spring配置文件时, 通过set方法注入本类的依赖.* @param userDao*/public void setUserDao(UserDao userDao) {this.userDao = userDao;}/*** 对数据进行增删改时清空缓存, 查询时使用缓存, 其中value为缓存区,* allEntries表示清空缓存中所有的数据, 使用#"user.id + '方法名'"做为key.*/@Override@CacheEvict(value = "data", key = "#id + 'add'") public void add(User user) {System.out.println("UserService: method- add(User user)" );userDao.add(user);}@Override@CacheEvict(value = "data", key = "#id + 'delete'") public void delete(String id) {System.out.println("UserService: method-delete(String id)" );userDao.delete(id);}@Override@CacheEvict(value = "data", key = "#user.id+ 'update'") public void update(User user) {System.out.println("UserService: method-update(User user)" );userDao.update(user);}@Override@Cacheable(value = "data", condition = "#id.length() > 5") public User find(String id) {System.out.println("UserService: method-find(String id)" );return userDao.find(id);}@Override//@CachePut(value = "data") //@Cacheable(value = "data") @Cacheable(value = "data") public Map<String, User> getAll() {System.out.println("UserService: method-getAll()" );return userDao.getAll();}
}
</span>
3.创建spring配置文件,配置spring自己的缓存管理器和bean之间的依赖关系,直接看代码spring-cahce.xml.(略:参考相关博客)4.最后通过测试类测试spring缓存机制是否生效,直接看代码MainTest.
<span style="font-size:12px;">package com.ilucky.spring.cache;import java.util.Map;
import java.util.Map.Entry;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ilucky.spring.cache.model.User;
import com.ilucky.spring.cache.service.UserService;/*** @author IluckySi* @date 20140614* 测试spring缓存机制是否生效.*/
public class MainTest {public static void main(String[] args) {//加载ClassPath路径下的spring配置文件, 并获取service bean.ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-cache.xml"); UserService userService = context.getBean("userService", UserService.class); System.out.println("service bean: " + userService);//添加三个用户, 然后查询并输出.userService.add(new User("1", "Ilucky1", "pwd1"));userService.add(new User("2", "Ilucky2", "pwd2"));userService.add(new User("3", "Ilucky3", "pwd3"));Map<String, User> map = userService.getAll();for(Entry<String, User> entry : map.entrySet()) {System.out.println(entry.getValue());}//验证@Cacheable和CachePut的区别, CachePut既保证方法被调用,同时保证结果被缓存.Map<String, User> map2 = userService.getAll();for(Entry<String, User> entry : map2.entrySet()) {System.out.println(entry.getValue());}}
}
/**
spring缓存分为两种:方法和数据,如果方法被清除了,需要重新从数据库获取数据放入缓存,另外只有方法加入了缓存才可以获取缓存中的数据.
@Cacheable(value = "data")
public Map<String, User> getAll() {
输出结果:
service bean: com.ilucky.spring.cache.service.impl.UserServiceImpl@17ec9f7
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1@CachePut(value = "data")
service bean: com.ilucky.spring.cache.service.impl.UserServiceImpl@fd918a
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
*/
</span>
在测试代码中通过ClassPathXmlApplicationContext类加载spring配置文件,因为此maven项目是jar项目,并且在maven中约定资源放在src/main/resources目录,所以spring配置文件需要放在src/main/resources目录下.
注意:spring缓存分为两种:方法和数据,如果方法被清除了,需要重新从数据库获取数据放入缓存,另外只有方法加入了缓存才可以获取缓存中的数据.
这篇关于spring缓存机制-@CachePut的用法(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!