redis.clients
jedis
4.3.1
@RestController
public class TestController {
@GetMapping("jedis")
public String redis() {
Jedis jedis = new Jedis("192.168.2.17", 6379);
jedis.set("k1", "v1");
return "";
}
}
org.springframework.boot
spring-boot-starter-parent
2.6.10
io.lettuce
lettuce-core
6.2.1.RELEASE
StringBuffer sb = new StringBuffer();
sb.append("123456");
RedisURI uri = RedisURI
.builder()
.redis("192.168.2.17")
.withPort(6379)
.withPassword(sb)
.build();
//创建连接客户端
RedisClient redisClient = RedisClient.create(uri);
StatefulRedisConnection connection = redisClient.connect();
RedisCommands commands = connection.sync();
commands.set("k2", "v2");
Object k2 = commands.get("k2");
connection.close();
redisClient.shutdown();
org.springframework.boot
spring-boot-starter-data-jdbc
org.apache.commons
commons-pool2
application.properties
spring.redis.host=192.168.2.17
spring.redis.port=6379
spring.redis.password=123456
spring.redis.lettuce.pool.max-active=8
spring.redis.1ettuce.pool.max-wait=-1ms
spring.redis.1ettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
@Configuration
public class RedisConfig {
/**
* *redis序列化的工具定置类,下面这个请一定开启配置
* *127.0.0.1:6379> keys *
* *1) “ord:102” 序列化过
* *2)“\xaclxedlxeelx05tixeelaord:102” 野生,没有序列化过
* *this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法
* *this.redisTemplate.opsForList();// 提供了操作List类型的所有方法
* *this.redisTemplate.opsForset(); //提供了操作set类型的所有方法
* *this.redisTemplate.opsForHash(); //提供了操作hash类型的所有方认
* *this.redisTemplate.opsForZSet(); //提供了操作zset类型的所有方法
* param LettuceConnectionFactory
* return
*/
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
// 设置key序列化方式string
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 设置value的序列化方式json,使用GenericJackson2JsonRedisSerializer替换默认序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
@RestController
public class TestController {
public TestController(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
private RedisTemplate redisTemplate;
@GetMapping("jedis")
public String redis() {
this.redisTemplate.opsForValue().set("k3", "v3");
Object k3 = this.redisTemplate.opsForValue().get("k3");
return "k2" + k3;
}
}
1.判断是否有key所对应的值,有则返回true,没有则返回false
redisTemplate.hasKey(key)
2.有则取出key值所对应的值
redisTemplate.opsForValue().get(key)
3.删除单个key值
redisTemplate.delete(key)
4.批量删除key
redisTemplate.delete(keys) //其中keys:Collection
keys
5.将当前传入的key值序列化为byte[]类型
redisTemplate.dump(key)
6.设置过期时间
public Boolean expire(String key, long timeout, TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
public Boolean expireAt(String key, Date date) {
return redisTemplate.expireAt(key, date);
}
7.查找匹配的key值,返回一个Set集合类型
public Set getPatternKey(String pattern) {
return redisTemplate.keys(pattern);
}
8.修改redis中key的名称
public void renameKey(String oldKey, String newKey) {
redisTemplate.rename(oldKey, newKey);
}
9.返回传入key所存储的值的类型
public DataType getKeyType(String key) {
return redisTemplate.type(key);
}
10.返回当前key所对应的剩余过期时间
public Long getExpire(String key) {
return redisTemplate.getExpire(key);
}
11.返回剩余过期时间并且指定时间单位
public Long getExpire(String key, TimeUnit unit) {
return redisTemplate.getExpire(key, unit);
}
12.
6.1 语法
eval script numkeys [key1 key2] [value1 value2]
String lua = "if redis.call('get',KEYS[1]) == ARGV[1] then " +
"return redis.call('del',KEYS[1]) " +
"else " +
"return 0 " +
"end";
this.stringRedisTemplate.execute(new DefaultRedisScript<>(lua, Long.class), Arrays.asList(key),"123466");
6.2 基础的
eval "return 'hello lua'" 0
6.3 Set
eval "redis.call('set','k1','v1') redis.call('expire','k1',30) return redis.call('get','k1')" 0
6.4 参数
eval "return redis.call('mset',KEYS[1],ARGV[1],KEYS[2],ARGV[2])" 2 k1 k2 v1 v2
6.5 条件if
eval "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end" 1 lock 666
6.6