SpringBoot2.0之Redis-lettuce连接

什么是lettuce
LettuceJedis 的都是连接Redis Server的客户端程序。Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例增加物理连接。Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

导入依赖和添加配置

<!-- lettuce pool 缓存连接池 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
  <!-- spring boot redis 缓存引入 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
#  添加配置

spring:
  redis:
#    reids的连接ip
    host: 114.67.224.231
# Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
    database: 0
# 连接超时时间(毫秒)
    timeout: 10000ms
#  springboot2.0后默认使用lettuce连接redis,底层使用的是netty框架做支撑
    lettuce:
      pool:
# 连接池中的最小空闲连接 默认 0
        min-idle: 0
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
        max-wait: -1ms
# 连接池最大连接数(使用负值表示没有限制) 默认 8
        max-active: 8
# 连接池中的最大空闲连接 默认 8
        max-idle: 8

自定义Template

package com.ocean.springcloud.oceanspringboot2.test.configuration;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * @author 季超
 * @create 2018-10-25 10:46
 * @desc
 **/
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisCacheAutoConfiguration {

    @Bean
    public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

测试

package com.ocean.springcloud.oceanspringboot2.test;

import com.ocean.springcloud.oceanspringboot2.test.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ApplicationTests {

	@Resource
	private StringRedisTemplate stringRedisTemplate;

	@Resource
	private RedisTemplate<String, Serializable> redisCacheTemplate;

	@Test
	public void redisTest(){
		// TODO 测试线程安全
		ExecutorService executorService = Executors.newFixedThreadPool(1000);
		IntStream.range(0, 1000).forEach(i ->
				executorService.execute(() -> stringRedisTemplate.opsForValue().increment("kk", 1))
		);
		stringRedisTemplate.opsForValue().set("k1", "v1");
		final String k1 = stringRedisTemplate.opsForValue().get("k1");
		log.info("[字符缓存结果] - [{}]", k1);
		// TODO 以下只演示整合,具体Redis命令可以参考官方文档,Spring Data Redis 只是改了个名字而已,Redis支持的命令它都支持
		String key = "battcn:user:1";
		redisCacheTemplate.opsForValue().set(key, new User(1L, "u1", "pa"));
		// TODO 对应 String(字符串)
		final User user = (User) redisCacheTemplate.opsForValue().get(key);
		log.info("[对象缓存结果] - [{}]", user);

	}

}

是Redis其它类型所对应的操作方式

  • opsForValue: 对应 String(字符串)
  • opsForZSet: 对应 ZSet(有序集合)
  • opsForHash: 对应 Hash(哈希)
  • opsForList: 对应 List(列表)
  • opsForSet: 对应 Set(集合)
  • opsForGeo: 对应 GEO(地理位置

猜你喜欢

转载自blog.csdn.net/m0_37779570/article/details/83377808