SpringBoot2.x使用redis(Lettuce)和RedisTemplate

为什么使用Lettuce:

Spring-data-redis提供了在spring应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis,  JRedis, and RJC)进行了高度封装

RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。

特征https://spring.io/projects/spring-data-redis

  • 连接包是跨多个Redis驱动程序(LettuceJedis)的底层抽象。

  • RedisTemplate提供了用于执行各种Redis操作,异常转换和序列化支持的高级抽象。

  • Pubsub支持(例如,消息驱动的POJO的MessageListenerContainer)。

  • Redis SentinelRedis Cluster支持。

  • JDK,String,JSON和Spring Object / XML映射序列化器

  • Redis之上的JDK Collection实现。

  • 原子计数器支持类。

  • 排序和流水线功能。

  • Spring 3.1缓存抽象的Redis 实现

Spring Data Redis(SDR)框架通过Spring出色的基础架构支持消除了与存储库交互所需的冗余任务和样板代码,从而简化了编写使用Redis键值存储库的Spring应用程序的过程。 

redis序列化存储的问题:

编写缓存配置类RedisConfig用于调优缓存默认配置,RedisTemplate<String, Object>的类型兼容性更高

大家可以看到在redisTemplate()这个方法中用JacksonJsonRedisSerializer更换掉了Redis默认的序列化方    式:JdkSerializationRedisSerializer

spring-data-redis中序列化类有以下几个:

GenericToStringSerializer可以将任何对象泛化为字符创并序列化

Jackson2JsonRedisSerializer序列化Object对象为json字符创(与JacksonJsonRedisSerializer相同)    

JdkSerializationRedisSerializer序列化java 对象 StringRedisSerializer:简单的字符串序列化

JdkSerializationRedisSerializer序列化被序列化对象必须实现Serializable接口,被序列化除属性内容还有其他内容,长度长且不易阅读,默认就是采用这种序列化方式

存储内容如下:

"\xac\xed\x00\x05sr\x00!com.oreilly.springdata.redis.User\xb1\x1c

\n\xcd\xed%\xd8\x02\x00\x02I\x00\x03ageL\x00\buserNamet\x00\x12Ljava/lang/String;xp\x00\x00\x00\ x14t\x00\x05user1"

JacksonJsonRedisSerializer序列化,被序列化对象不需要实现Serializable接口,被序列化的结果清晰,容易阅读,而且存储字节少,速度快

存储内容如下:

"{"userName":"guoweixin","age":20}"

StringRedisSerializer序列化

一般如果keyvalue都是string字符串的话,就是用这个就可以了

项目目录:

 

一、pom文件,引入依赖

        <!--Spring Data Redis 默认是lettuce客户端-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--redis依赖pool连接池这个jar包-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.7.0</version>
        </dependency>

配置文件:application.yml

server:
   port: 8889

#redis
spring:
  redis:
    port: 6379
    password: Xxf159357!_
    host: 182.92.236.141
    lettuce:
      pool:
        max-active: 100 # 连接池最大连接数(使用负值表示没有限制) 太小可能出现connection.PoolExcelption
        max-idle: 8 # 连接池中的最大空闲连接
        min-idle: 0 # 连接池中的最小空闲连接
        max-wait: 1000 # 连接池最大阻塞等待时间(使用负值表示没有限制)
      shutdown-timeout: 100	# 关闭超时时间
#log 出现<font color=red>CONDITIONS EVALUATION REPORT</font>
#<font color=red>java.lang.IllegalStateException: Failed to load ApplicationContext</font>释放下面代码
#logging.level.org.springframework.boot.autoconfigure: error
#logging:
  #level: info

 二、RedisConfig类

package com.xxf.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
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.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.lang.reflect.Method;

/**
 * @Author:xuefengxia
 * @Date:2020/6/18
 * @Description:
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    /**
     * 自定义缓存key的生成策略。默认的生成策略是看不懂的(乱码内容)
     * 通过Spring 的依赖注入特性进行自定义的配置注入并且此类是一个配置类可以更多程度的自定义配置
     * @return
     */
    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {

            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

    /**
     * 缓存配置管理器
     */
    @Bean
    public CacheManager cacheManager(LettuceConnectionFactory factory) {
        //以锁写入的方式创建RedisCacheWriter对象
        RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(factory);
        //创建默认缓存配置对象
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        RedisCacheManager cacheManager = new RedisCacheManager(writer, config);
        return cacheManager;
    }


    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer 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);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // 在使用注解@Bean返回RedisTemplate的时候,同时配置hashKey与hashValue的序列化方式。
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // value 序 列 化 方 式 采 用 jackson
        //使用它操作普通字符串,会出现Could not read JSON  template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

三、实例RedisServImpl

package com.xxf.service;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author:xuefeng
 * @Date:2020/6/18
 * @Description:
 */
@Service
@Log
public class RedisServiceImpl {

@Autowired
private RedisTemplate<String,Object> redisTemplate;


    /**
     *普通缓存放入
     *@param key 键
     *@return true成功 false失败
     */
    public String getString(String key) {
        if(redisTemplate.hasKey(key)) {
            log.info("Redis中查询");
            redisTemplate.opsForValue().set("jsonStr","{'userName':'xxf','age':20}");
            System.out.println(redisTemplate.opsForValue().get(key));
            return (String) redisTemplate.opsForValue().get(key);
        }else{
            String val="xxf";
            redisTemplate.opsForValue().set(key, val);
            log.info("数据库中查询的");
            return	val;
        }
    }

    /**
     *普通缓存放入
     *@param key 键
     *@param value 值
     *@param expireTime 超时时间(秒)
     *@return true成功 false失败
     */
    public Boolean set(String key, Object value, int expireTime) {
        try {
            redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

四、测试类

package com.xxf.lettuce;

import com.xxf.service.RedisServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @Author:xuefeng
 * @Date:2020/6/18
 * @Description:
 */
@SpringBootTest
class TestRedisTemplate {
    @Autowired
    private RedisServiceImpl redisService;
    @Test
    public void testGetKey() {
        redisService.getString("jsonStr");
    }
    @Test
    public void testSetKey() {
        redisService.set("xxf","黎明的光与影",5000);
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章