Spring-Boot- operation -Redis, three scenarios full resolution!

Redis appear before our wide variety of caching framework, with Redis, caching scheme basically unified, before about Redis, Song Ge has a series of tutorials, not known Redis small partners can refer to this tutorial:

Many use the Java operating Redis program, Jedis is the more popular a program, in addition to Jedis, there are many other solutions, as follows:

In addition to these programs, there is also the use of a considerable number of programs that Spring Data Redis.

In the traditional SSM, the need to develop themselves to configure the Spring Data Redis, this configuration is more complicated, configure the three main things: the connection pool, connection information and key and value serialization scheme.

In Spring Boot, the default integrated Redis is Spring Data Redis, the default underlying connection pool using lettuce, developers can modify for your own familiar, such as Jedis.

Spring Data Redis Redis provides a very convenient for the operation template RedisTemplate. Spring Data This is a good thing, then the next we look at the specific usage Spring Boot in the Spring Data Redis.

Option One: Spring Data Redis

Create Project

Create a project, dependent on the introduction of Redis:

Once created, you need to manually introduced commos-pool2 dependent, so the final complete dependence pom.xml as follows:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

Here mainly introduces the Spring Data Redis + connection pool.

Redis configuration information

Next, the configuration information Redis information includes two aspects, one of the basic information Redis, the other is connected to the pool information:

spring.redis.database=0
spring.redis.password=123
spring.redis.port=6379
spring.redis.host=192.168.66.128
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms

Automatic Configuration

When the developer in the project to introduce the Spring Data Redis, and equipped with basic information about the Redis, at this time, automated configuration will take effect.

We can be discerned from Spring Boot in Redis automated configuration class:

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
        @Bean
        @ConditionalOnMissingBean(name = "redisTemplate")
        public RedisTemplate<Object, Object> redisTemplate(
                        RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
                RedisTemplate<Object, Object> template = new RedisTemplate<>();
                template.setConnectionFactory(redisConnectionFactory);
                return template;
        }
        @Bean
        @ConditionalOnMissingBean
        public StringRedisTemplate stringRedisTemplate(
                        RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
                StringRedisTemplate template = new StringRedisTemplate();
                template.setConnectionFactory(redisConnectionFactory);
                return template;
        }
}

这个自动化配置类很好理解:

  1. 首先标记这个是一个配置类,同时该配置在 RedisOperations 存在的情况下才会生效(即项目中引入了 Spring Data Redis)
  2. 然后导入在 application.properties 中配置的属性
  3. 然后再导入连接池信息(如果存在的话)
  4. 最后,提供了两个 Bean ,RedisTemplate 和 StringRedisTemplate ,其中 StringRedisTemplate 是 RedisTemplate 的子类,两个的方法基本一致,不同之处主要体现在操作的数据类型不同,RedisTemplate 中的两个泛型都是 Object ,意味者存储的 key 和 value 都可以是一个对象,而 StringRedisTemplate 的 两个泛型都是 String ,意味者 StringRedisTemplate 的 key 和 value 都只能是字符串。如果开发者没有提供相关的 Bean ,这两个配置就会生效,否则不会生效。

使用

接下来,可以直接在 Service 中注入 StringRedisTemplate 或者 RedisTemplate 来使用:

@Service
public class HelloService {
    @Autowired
    RedisTemplate redisTemplate;
    public void hello() {
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("k1", "v1");
        Object k1 = ops.get("k1");
        System.out.println(k1);
    }
}

Redis 中的数据操作,大体上来说,可以分为两种:

  1. 针对 key 的操作,相关的方法就在 RedisTemplate 中
  2. 针对具体数据类型的操作,相关的方法需要首先获取对应的数据类型,获取相应数据类型的操作方法是 opsForXXX

调用该方法就可以将数据存储到 Redis 中去了,如下:

k1 前面的字符是由于使用了 RedisTemplate 导致的,RedisTemplate 对 key 进行序列化之后的结果。

RedisTemplate 中,key 默认的序列化方案是 JdkSerializationRedisSerializer 。

而在 StringRedisTemplate 中,key 默认的序列化方案是 StringRedisSerializer ,因此,如果使用 StringRedisTemplate ,默认情况下 key 前面不会有前缀。

不过开发者也可以自行修改 RedisTemplate 中的序列化方案,如下:

@Service
public class HelloService {
    @Autowired
    RedisTemplate redisTemplate;
    public void hello() {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("k1", "v1");
        Object k1 = ops.get("k1");
        System.out.println(k1);
    }
}

当然也可以直接使用 StringRedisTemplate:

@Service
public class HelloService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    public void hello2() {
        ValueOperations ops = stringRedisTemplate.opsForValue();
        ops.set("k2", "v2");
        Object k1 = ops.get("k2");
        System.out.println(k1);
    }
}

另外需要注意 ,Spring Boot 的自动化配置,只能配置单机的 Redis ,如果是 Redis 集群,则所有的东西都需要自己手动配置,关于如何操作 Redis 集群,松哥以后再来和大家分享。

方案二:Spring Cache

通过 Spring Cache 的形式来操作 Redis,Spring Cache 统一了缓存江湖的门面,这种方案,松哥之前有过一篇专门的文章介绍,小伙伴可以移步这里:Spring Boot中,Redis缓存还能这么用!

方案三:回归原始时代

第三种方案,就是直接使用 Jedis 或者 其他的客户端工具来操作 Redis ,这种方案在 Spring Boot 中也是支持的,虽然操作麻烦,但是支持,这种操作松哥之前也有介绍的文章,因此这里就不再赘述了,可以参考 Jedis 使用

总结

Spring Boot 中,Redis 的操作,这里松哥给大家总结了三种方案,实际上前两个使用广泛一些,直接使用 Jedis 还是比较少,基本上 Spring Boot 中没见过有人直接这么搞。

好了,本文就说到这里,有问题欢迎留言讨论。

关注公众号牧码小子,专注于 Spring Boot+微服务,定期视频教程分享,关注后回复 Java ,领取松哥为你精心准备的 Java 干货!

Guess you like

Origin www.cnblogs.com/lenve/p/10965667.html