Jedis, Redission, Lettuce operation Redis, common Redis operation methods
Jedis
- Definition: Jedis is the official Java connection development tool recommended by Redis.
Basic use
Maven dependency
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
Instructions
Jedis jedis = new Jedis("localhost",6379);
jedis.set("one", "one");
return jedis.get("one");
JedisPool initialization
@Bean
public JedisPool getJedisPool(){
RedisProperties redisProperties=new RedisProperties();
GenericObjectPoolConfig jedisPoolConfig=new JedisPoolConfig();
// 资源池中的最大连接数
jedisPoolConfig.setMaxTotal(10);
// 资源池允许的最大空闲连接数
jedisPoolConfig.setMaxIdle(10);
// 资源池确保的最少空闲连接数
jedisPoolConfig.setMinIdle(0);
// 当资源池连接用尽后,调用者的最大等待时间(单位为毫秒)。
jedisPoolConfig.setMaxWaitMillis(1000);
JedisPool jedisPool=new JedisPool(jedisPoolConfig,redisProperties.getHost(),redisProperties.getPort());
try{
Jedis resource = jedisPool.getResource();
logger.info("==========JedisPool配置成功!");
return jedisPool;
}catch (Exception e){
logger.info("==========JedisPool配置失败!");
return null;
}
}
Redission
- Definition: On the Netty framework based on NIO, Redisson makes full use of a series of advantages provided by Redis key-value database. Based on the common interfaces in the Java utility toolkit, it provides users with a series of commonly used distributed features. Tools. As a toolkit for coordinating single-machine multi-threaded concurrent programs, it has obtained the ability to coordinate distributed multi-machine multi-threaded concurrent systems, greatly reducing the difficulty of designing and developing large-scale distributed systems. At the same time, the combination of various characteristic distributed services further simplifies the collaboration between programs in a distributed environment.
Basic use
Maven dependency
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.5.0</version>
</dependency>
Redisson configuration
@Configuration
public class RedissionConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
private static Logger logger = Logger.getLogger(String.valueOf(RedissionConfig.class));
/**
* 单机模式配置
*/
@Bean
public RedissonClient redissonClient() {
try {
RedisProperties redisProperties = new RedisProperties();
Config config =new Config();
config.useSingleServer().setAddress("redis://"+host+":"+port);
RedissonClient redissonClient = Redisson.create(config);
logger.info("==========Redisson配置完成!");
return redissonClient;
} catch (Exception e) {
logger.info("==========Redisson配置失败!" + e.getMessage());
return null;
}
}
}
Instructions
There are many ways to operate Redis in the RedissonClient interface
public interface RedissonClient {
RBinaryStream getBinaryStream(String var1);
<V> RGeo<V> getGeo(String var1);
<V> RGeo<V> getGeo(String var1, Codec var2);
<V> RSetCache<V> getSetCache(String var1);
<V> RSetCache<V> getSetCache(String var1, Codec var2);
<K, V> RMapCache<K, V> getMapCache(String var1, Codec var2);
<K, V> RMapCache<K, V> getMapCache(String var1, Codec var2, MapOptions<K, V> var3);
<K, V> RMapCache<K, V> getMapCache(String var1);
<K, V> RMapCache<K, V> getMapCache(String var1, MapOptions<K, V> var2);
<V> RBucket<V> getBucket(String var1);
}
Lettuce
- Definition: Lettuce is a scalable thread-safe [Redis] client. Multiple threads can share the same RedisConnection. It uses the excellent [netty]NIO framework to efficiently manage multiple connections.
Basic use
Maven dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
applicaiton.yml placement
server:
port: 8888
spring:
redis:
host: localhost
port: 6379
lettuce: # 这里标明使用lettuce配置
pool:
max-active: 8 #连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms #连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 5 #连接池中的最大空闲连接
min-idle: 0 #连接池中的最小空闲连接
timeout: 10000ms #连接超时时间(毫秒)
RedisConfig配置RedisTemplate
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
/**
* RedisTemplate配置
*/
@Bean
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory connectionFactory){
RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());//key序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());//value序列化
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
use
@RequestMapping("/test1")
public void addlist1() {
redisTemplate.opsForValue().set("one", "one");
redisTemplate.opsForSet().add("test:set2", "1");
redisTemplate.opsForSet().add("test:set2", "2");
redisTemplate.opsForSet().add("test:set1", "3");
redisTemplate.opsForSet().add("test:set1", "3");
redisTemplate.opsForHash().put("hash1", "name1", "lms1");
redisTemplate.opsForHash().put("hash1", "name2", "lms2");
redisTemplate.opsForHash().put("hash1", "name3", "lms3");
redisTemplate.opsForList().leftPush("two", "two", "two1");
}
For the complete code, see https://gitee.com/Marlon_Brando/JavaTest/tree/master/configtest