同步处理数据
删除操作
先删除缓存
单个数据缓存,
String key=“use”+id;
redisTempalte.delete(key);删除redis中缓存的单个对象
删除集合的对象
List list=(List)redisTemplate.opsForValue().get(“user:all”);
//判断集合不为空 查找user对象去除
if(list!=null){
Iterator lt=list.
whilt(it.hasNext()){
User user=it.next();
if(user.getId().equals(id)){
it.remove();
break;
}
}
设置有效期
redisTemplate。expire(key,10,TimeMut.)
Session 一个用户一个session 耗费tomcat资源 服务器就会影响
redis数据库可以存储多个数据
增删改查数据进行修改
70%用户的数据就行查询
用户数据进行存储,数据先提交在缓存,然后在提交到服务器,可以增加用户体验度
使用缓存进行数据增删改查
服务器启动就加载数据,将数据加载到缓存
初始化方法
单列模式创建
任务调度
扫描task任务组件任务 启动服务器立即加载数据
spring-task.xml
<!-- 组件启动就自动加载 -->
<context:component-scan base-package="com.tracy.task"></context:component-scan>
默认为单例模式
@Component // 容器启动就将数据加载到缓存
public class LoadUserBean {
@Autowired
private UserDao userDao;
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@PostConstruct //等价于Bean组件的init-method 方法
public void init() {
List<User> list=userDao.findAll();
//将整个集合存入缓存
redisTemplate.opsForValue().set("user:all", list);
//设置过期时间
redisTemplate.expire("user:all", 10, TimeUnit.MINUTES);
//将单个对象存入到缓存
for(User user:list) {
String key="user"+user.getId();
redisTemplate.opsForValue().set(key, user);
}
}
}
定时频率调度任务
quartz 开源项目 周期性调度 指定时间调用 缓存,简单基本任务调度
使用事务调度
Spring支持的事务调度
导入spring-support.jar
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<!-- 组件启动就自动加载 -->
<context:component-scan base-package="com.tracy.task"></context:component-scan>
<!-- 开启任务调度 -->
<task:annotation-driven/>
</beans>
@Component
public class TaskBean {
@Scheduled(cron = "1 * * * * ?")
public void taskTest() {
System.out.println("执行的时间为"+new Date());
}
}
@Scheduled(cron = "0/3 * * * * ?")
3秒进行一次调度