spring boot 2 使用RedisTemplate操作redis存取对象时,需要先进行序列化操作

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory; import java.time.Duration;
import java.util.HashMap;
import java.util.Map; /**
* Redis缓存配置类
*
* @author lee
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport { //缓存管理器
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
//user信息缓存配置
RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)).disableCachingNullValues().prefixKeysWith("user");
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("user", userCacheConfiguration);
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig(); //设置默认超过期时间是30秒
defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
//初始化RedisCacheManager
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
return cacheManager;
}
}

包装RedisTemplate,把常用操作写一个redis工具类

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service
public class RedisService { @Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate; public <T> T getJson2Object(String key, Class<T> clazz) { return JSON.parseObject(stringRedisTemplate.opsForValue().get(key), clazz);
} public <T> void setObject(String key, T clazz) {
redisTemplate.opsForValue().set(key, clazz);
} /**
* 设置对象,含有效期,单位为秒
*/
public <T> void setObject(String key, T clazz, long timeout) {
redisTemplate.opsForValue().set(key, clazz, timeout, TimeUnit.SECONDS);
} /**
* 设置对象,含有效期,单位可以自定义
*/
public <T> void setObject(String key, T clazz, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, clazz, timeout, unit); } @SuppressWarnings("unchecked")
public <T> T getObject(String key, Class<T> clazz) { return (T) redisTemplate.opsForValue().get(key); } public String getString(String key) {
return stringRedisTemplate.opsForValue().get(key);
} public void setString(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
}

spring boot 2.0.4 Redis缓存配置的更多相关文章

  1. Spring Boot不同版本整合Redis的配置

    1. Spring Boot为1.4及其他低版本 1.1 POM.XML配置 <!--引入 spring-boot-starter-redis(1.4版本前)--> <depende ...

  2. 搞懂分布式技术14:Spring Boot使用注解集成Redis缓存

    本文内容参考网络,侵删 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutor ...

  3. spring boot 2.0 ribbon 负载均衡配置

    1.pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId ...

  4. spring boot 2.0+ 错误页面配置

    如果访问了错误的路径,或者后台报错 如果没有一个统一的页面! 或者说页面上展示一堆报错信息,既影响美观,又对用户不友好! 那么如何配置? 定义 ErrorPageConfig,配置错误状态与对应访问路 ...

  5. Spring Boot 2.0 迁移指南

    ![img](https://mmbiz.qpic.cn/mmbiz_jpg/1flHOHZw6Rs7yEJ6ItV43JZMS7AJWoMSZtxicnG0iaE0AvpUHI8oM7lxz1rRs ...

  6. Spring Boot 2.x 整合 Redis最佳实践

    一.前言 在前面的几篇文章中简单的总结了一下Redis相关的知识.本章主要讲解一下 Spring Boot 2.0 整合 Redis.Jedis 和 Lettuce 是 Java 操作 Redis 的 ...

  7. Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...

  8. Spring Boot 2.0 升级指南

    Spring Boot 2.0 升级指南 前言 Spring Boot已经发布2.0有5个月多,多了很多新特性,一些坑也慢慢被填上,最近有空,就把项目中Spring Boot 版本做了升级,顺便整理下 ...

  9. spring boot 2.0.0 + shiro + redis实现前后端分离的项目

    简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大 ...

随机推荐

  1. bootstrap顶部导航遮挡下面内容的解决办法

    使用bootstrap设置顶部导航,并将导航栏固定,代码如下: <nav class="navbar navbar-expand-lg navbar-light bg-light fi ...

  2. Django-Form组件之字段

    Form类 创建Form类时,主要涉及到 [字段] 和 [插件],字段用于对用户请求数据的验证,插件用于自动生成HTML; 1.Django内置字段如下: 1 2 3 4 5 6 7 8 9 10 1 ...

  3. Kettle解决方案: 第三章 安装和配置

  4. JavaBean找到项目下的文件这里是\WEB-INF\classes\下面的属性文件

    String baseinfo="/com/Bean/DBhelp.properties"; filename=getClass().getClassLoader().getRes ...

  5. kafka reset offset 手工重置offset

    1.场景 a)有时消费端逻辑修改,需要重复消费数据,需要将offset设置到指定位置. 2.实现 kafka版本:0.11.* KIP-122: Add Reset Consumer Group Of ...

  6. keepalived+nginx实现niginx高可用,宕机自动重启

    nginx作为http服务器,在集群中 用于接受客户单发送过来的请求,并且根据配置的策略将请求 转发给具体的哪台服务器 如果在nginx服务器使用轮询策略处理客户端的请求,出现了tomcat 宕机的情 ...

  7. C# Directory.Exists() 文件存在但返回一直为false

    备注:这是一个低级错误,起始真正的原因不是访问权限的问题. 真正的原因是:这个程序要读取远程电脑上共享文件夹里的文件,但是没有远程访问代码,导致找不到相关的目录.所以才报错! 查询一个文件,但程序突然 ...

  8. JSONObject 转List 强制类型转换错误

    JSONArray arr=(JSONArray)map.getOrDefault("data","");List<DHD> data=JSONOb ...

  9. 2018.7.3 lnmp一键安装包无人值守版本 php7.2 + nginx1.14.0 + mariadb5.5 + centos7.1(1503) 环境搭建 + Thinkphp5.1.7 配置

    给自己练习用的,整个过程追求一个简单粗暴,没有配置虚拟主机,现在记录一下过程. 1. 进入到lnmp解压缩后的文件夹conf/rewrite,把thinkphp.conf复制一份到/usr/local ...

  10. Qt之QTreeWidget入门

    QTreeWidget的一些基本操作 1.insertTopLevelItems,insertTopLevelItem用来添加顶层的item QTreeWidget *treeWidget = new ...