资源简介
spring + redis使用@Cacheable,@CachePut,@CacheEvict详细步骤
代码片段和文件信息
package com.susq.work;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.objectInputStream;
import java.io.objectOutputStream;
/**
* @author susq
* @since 2018-01-05-10:33
*/
@Slf4j
public class MyRedisCache implements Cache {
private RedisTemplateject> redisTemplate;
private String name;
public RedisTemplateject> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplateject> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public object getNativeCache() {
return this.redisTemplate;
}
@Override
public ValueWrapper get(object key) {
System.out.println(“get key“);
final String keyf = key.toString();
object object = null;
object = redisTemplate.execute(new RedisCallbackject>() {
public object doInRedis(RedisConnection connection) throws DataAccessException {
byte[] key = keyf.getBytes();
byte[] value = connection.get(key);
if (value == null) {
return null;
}
return toobject(value);
}
});
return (object != null ? new SimpleValueWrapper(object) : null);
}
@Override
public void put(object key object value) {
System.out.println(“put key“);
final String keyf = key.toString();
final object valuef = value;
final long liveTime = 86400;
redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keyb = keyf.getBytes();
byte[] valueb = toByteArray(valuef);
connection.set(keyb valueb);
if (liveTime > 0) {
connection.expire(keyb liveTime);
}
return 1L;
}
});
}
private byte[] toByteArray(object obj) {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
objectOutputStream oos = new objectOutputStream(bos);
oos.writeobject(obj);
oos.flush();
bytes = bos.toByteArray()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-05-18 22:15 redis-second\
目录 0 2018-05-18 22:15 redis-second\.idea\
目录 0 2018-05-17 20:30 redis-second\.idea\artifacts\
文件 315 2018-05-17 15:28 redis-second\.idea\artifacts\redis_second_war.xm
文件 3415 2018-05-17 20:30 redis-second\.idea\artifacts\redis_second_war_exploded.xm
文件 561 2018-05-17 15:18 redis-second\.idea\checkst
文件 636 2018-05-17 15:45 redis-second\.idea\compiler.xm
文件 172 2018-05-17 15:28 redis-second\.idea\encodings.xm
目录 0 2018-05-17 15:19 redis-second\.idea\inspectionProfiles\
目录 0 2018-05-17 20:30 redis-second\.idea\libraries\
文件 514 2018-05-17 15:28 redis-second\.idea\libraries\Maven__aopalliance_aopalliance_1_0.xm
文件 448 2018-05-17 15:28 redis-second\.idea\libraries\Maven__cglib_cglib_3_1.xm
文件 568 2018-05-17 15:28 redis-second\.idea\libraries\Maven__ch_qos_logback_logback_classic_1_1_7.xm
文件 547 2018-05-17 15:28 redis-second\.idea\libraries\Maven__ch_qos_logback_logback_core_1_1_7.xm
文件 558 2018-05-17 15:28 redis-second\.idea\libraries\Maven__commons_logging_commons_logging_1_2.xm
文件 644 2018-05-17 15:28 redis-second\.idea\libraries\Maven__com_fasterxm
文件 595 2018-05-17 15:28 redis-second\.idea\libraries\Maven__com_fasterxm
文件 623 2018-05-17 15:28 redis-second\.idea\libraries\Maven__com_fasterxm
文件 578 2018-05-17 15:28 redis-second\.idea\libraries\Maven__javax_servlet_javax_servlet_api_3_1_0.xm
文件 455 2018-05-17 15:28 redis-second\.idea\libraries\Maven__junit_junit_4_12.xm
文件 570 2018-05-17 15:28 redis-second\.idea\libraries\Maven__org_apache_commons_commons_pool2_2_4_2.xm
文件 532 2018-05-17 15:28 redis-second\.idea\libraries\Maven__org_hamcrest_hamcrest_core_1_3.xm
文件 500 2018-05-17 19:53 redis-second\.idea\libraries\Maven__org_mybatis_mybatis_3_2_5.xm
文件 549 2018-05-17 19:53 redis-second\.idea\libraries\Maven__org_mybatis_mybatis_spring_1_2_2.xm
文件 458 2018-05-17 15:28 redis-second\.idea\libraries\Maven__org_ow2_asm_asm_4_2.xm
文件 524 2018-05-17 15:28 redis-second\.idea\libraries\Maven__org_projectlombok_lombok_1_16_8.xm
文件 513 2018-05-17 20:30 redis-second\.idea\libraries\Maven__org_slf4j_slf4j_api_1_7_21.xm
文件 678 2018-05-17 15:28 redis-second\.idea\libraries\Maven__org_springfr
文件 609 2018-05-17 15:28 redis-second\.idea\libraries\Maven__org_springfr
文件 623 2018-05-17 15:28 redis-second\.idea\libraries\Maven__org_springfr
文件 637 2018-05-17 15:28 redis-second\.idea\libraries\Maven__org_springfr
............此处省略76个文件信息
- 上一篇:单相桥式可控整流电路的设计
- 下一篇:双目矫正及视差图的计算
相关资源
- spring-qutarz_demo
- 约束文件.zip
- Spring Boot -- 整合Websocket
- Spring5高级编程(第5版) 中文pdf
-
LNCS的st
yle文件endnote - sharding-jdbc-demo.zip
- SSM搭建分库分表源码可直接运行带数
- SSM到Spring Boot
- Spring Boot 2.0 + Thymeleaf模板+简单增删改
- SSM外文文献和翻译(毕设论文精品)
- vert.x结合springboot开发mqtt服务,真实可
- springboot+springcloud教程视频.txt
- Spring相关的外文文献和翻译(含出处
- scrapy_redis-0.6.8-py2.py3-none-any.whl
-
Spring fr
amework Fundamentals - Apache Dbutils +springmvc 框架demo代码
- jsf+spring+hibernate整合demo
- springboothttps
- springboot实现Excel文件导入与导出功能
- springboot + redis实现session共享
- 最简单的用户登录与注册系统 spring
- springboot+mybatis+shiro整合demo
- SpringBoot集成app微信支付.rar
- 基于SpringBoot+Mybatis商品信息管理系统
- 蚂蚁课堂第四期结业项目SpringCloud构建
- Spring-Boot实战第4版随书源码
- tp框架封装redis读写分离类.rar
- redis-3.3.0.gem
-
spring-code-ba
sed - springBoot简单后台增删改查
评论
共有 条评论