本项目使用jar包:

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  • 在application.properties中加入redis的配置信息

    #redis
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    # 数据库连接超时时间,2.0 中该参数的类型为Duration,这里在配置的时候需要指明单位
    spring.redis.timeout=60s
    # 连接池配置,2.0中直接使用jedis或者lettuce配置连接池
    # 最大活跃连接数,负数为不限制
    spring.redis.lettuce.pool.max-active=500
    # 等待可用连接的最大时间,负数为不限制
    spring.redis.lettuce.pool.max-wait=-1ms
    # 最大空闲连接数
    spring.redis.lettuce.pool.max-idle=100
    # 最小空闲连接数
    spring.redis.lettuce.pool.min-idle=20
  • 新增RedisConfig.java文件,其中注释掉的两行代码不能打开。打开的话会使value使序例化,序例化了以后会跟后面要使用的@Cacheable不一致,导致取不到值
    package com.chenyingjun.springboot2.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer; /**
    * RedisConfig工具类配置
    *
    * @author chenyingjun
    * @since 1.0
    * @version 2018年8月15日 chenyingjun
    */
    @Configuration
    @EnableCaching
    public class RedisConfig { /**
    * RedisTemplate配置
    */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
    // 设置序列化
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
    Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    // 配置redisTemplate
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(lettuceConnectionFactory);
    RedisSerializer<?> stringSerializer = new StringRedisSerializer();
    // // key序列化
    redisTemplate.setKeySerializer(stringSerializer);
    // value序列化
    // redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    // Hash key序列化
    redisTemplate.setHashKeySerializer(stringSerializer);
    // Hash value序列化
    // redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
    } }
  • 在service层中加入@Cacheable(key="'userId_'+#id", value="user")来缓存信息,这样println只会打印一次
        @Cacheable(key="'userId_'+#id", value="user")
    public SystemUserVo info(String id) {
    System.out.println("-------------------------------------------------------");
    SystemUserVo user = systemUserMapper.info(id);
    return user;
    }
  • 缓存后数据如下图
  • 使用RedisTemplate和StringRedisTemplate来获取缓存中的值,其中obj1也已经获取到了redis中的数据。效果如下

springboot2 redis的更多相关文章

  1. Redis事件通知示例

    1. redis如同zk一样,提供了事件监听(或者说是回调机制), 下面是redis的配置说明: ############################# EVENT NOTIFICATION ## ...

  2. (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)

    一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...

  3. springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间

    springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...

  4. SpringBoot2.X + SpringCache + redis解决乱码问题

    环境:SpringBoot2.X + SpringCache + Redis Spring boot默认使用的是SimpleCacheConfiguration,使用ConcurrentMapCach ...

  5. springboot2.x版本整合redis(单机/集群)(使用lettuce)

    在springboot1.x系列中,其中使用的是jedis,但是到了springboot2.x其中使用的是Lettuce. 此处springboot2.x,所以使用的是Lettuce.关于jedis跟 ...

  6. SpringBoot2.x整合Redis实战 4节课

    1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download          2.新手 ...

  7. SpringBoot2.0+Mybatis+PageHelper+Redis实现缓存

    1.在maven引入相关的依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactI ...

  8. Springboot2.x+shiro+redis(Lettuce)整合填坑

    主要记录关键和有坑的地方 前提: 1.SpringBoot+shiro已经集成完毕,如果没有集成,先查阅之前的Springboot2.0 集成shiro权限管理 2.redis已经安装完成 3.red ...

  9. SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理

    一.Redis简介 Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elastic ...

随机推荐

  1. Ubuntu 更改屏幕分辨率

    安装完Ubuntu后发现分辨率不合适,平时习惯了看小一点的文字,所以搜了一下修改屏幕分辨率的命令,具体操作如下: 1.先用 xrandr 命令查看一下当前系统支持的分辨率 wayde@wayde-Al ...

  2. K8s-Pod控制器

      在K8s-Pod文档中我们创建的Pod是非托管的Pod,因为Pod被设计为用后就弃的对象,如果Pod正常关闭,K8s会将该Pod清除,它没有自愈的能力.Pod控制器是用来保持Pod状态的一种对象资 ...

  3. 正则表达式过滤html标签

    1.说明:需要使用非贪婪模式 2.示例 过滤所有span标签: var newContent = Regex.Replace(htmlContent, "<span.*?>.*? ...

  4. 步步为营-93-MVC+EF简单实例

    1:创建MVC项目 2:添加EF数据(这里选择DataBaseFirst模式) 3:添加控制器 3.1.1 创建列表页面 3.1.2 html页面 @using MvcApplication1 @{ ...

  5. JAVA追加写入文本文件

    public void method1() { FileWriter fw = null; try { //如果文件存在,则追加内容:如果文件不存在,则创建文件 File f=new File(&qu ...

  6. python练习册0004题

    在任意一个英文文档中,统计单词出现的次数, 分析: 本题不是很难,单词通常以空格隔开,但是有些单词后面跟一些特殊符号,只需把这些特殊符号替换掉就可以了, 代码一 import re file_name ...

  7. proc/net/dev实时网速统计实例

    https://blog.csdn.net/dosthing/article/details/80384541 http://www.l99.com/EditText_view.action?text ...

  8. HTML5语音输入方法

    谷歌的网站是时逛时新啊,今天在他们首页发现了HTML5的新玩法——语音搜索.可惜的是只有webkit核心的浏览器才能使用.用法很简单只需要在input添加属性 x-webkit-speech 即可,例 ...

  9. OpenGL搭建环境-VS2012【OpenGL】

    在学习cocos2dx时,其底层就用到了OpenGL,因此,想要能够精通cocos2dx,还是需要了解OpenGL的. 本着这个目的,开始搭建OpenGL的环境,并进行学习. OpenGL环境的搭建相 ...

  10. redis 配置文件配置

    redis的配置和使用 redis的配置的分段的 配置段: 基本配置项 网络配置项 持久化相关配置 复制相关的配置 安全相关配置 Limit相关的配置 SlowLog相关的配置 INCLUDES Ad ...