目录
redis客户端(JAVA)
配置
引入依赖
建立连接
常用命令实现
get/set
exists/del
keys
expire和ttl
type
字符串(String)
mget和mset
getrange和setrange
append
incr和decr
列表(list)
lpush和lrange
rpush
lpop
rpop
blpop
llen
集合(set)
sadd和smembers
sismember
scard
spop
sinter
sinterstore
哈希(hash)
hset和hget
hexists
hdel
hkeys和hvals
hmget和hmset
有序集合(zset)
zadd和zrange
zcard
zrem
zscore
zrank
SpringDataRedis
RedisTemplate工具类
SpringDataRedis快速入门
引入依赖
配置文件
注入RedisTemplate
编写测试
SpringDataRedis的序列化方式
方案一
方案二
构建一个类来测试redis的方法
string
list
编辑
set
hash
zset
Jedis的官网地址
java生态中,封装了RESP协议,在这里使用jedis实现redis客户端
创建一个maven项目,引入jedis依赖
redis.clients
jedis
4.4.2
进行简单配置,把云服务器的端口当成一个本地的端口使用
连接上云服务器上的 redis ,就需要开放 6379 端口:
- 将 java 程序打包成 jar 包,放到 linux 服务器上执行(过于麻烦,不推荐);
- 匹配 ssh 端口转发,把云服务器的 redis 端口,映射到本地主机(推荐).
点击会话,右键点击属性
查看ssh连接是否生效
写出一个类来验证
public class RedisDemo {
public static void main(String[] args) {
//连接到Redis服务器上
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
//redis的各种命令,就都对应到jedis对象的各种方法
String pong=jedis.ping();
System.out.println(pong);
}
}
}
public class RedisDemo {
public static void test1(Jedis jedis){
System.out.println("get和set的使用");
//先清空数据库
jedis.flushAll();
//set
jedis.set("key","111");
jedis.set("key2","222");
//get
String value=jedis.get("key");
System.out.println("value="+value);
}
public static void main(String[] args) {
//连接到Redis服务器上
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test1(jedis);
}
}
}
设置超时时间
public class RedisDemo {
public static void test1(Jedis jedis){
System.out.println("get和set的使用");
//先清空数据库
jedis.flushAll();
//set
jedis.set("key","111");
jedis.set("key2","222");
SetParams params=new SetParams();
params.ex(10);
params.xx();
jedis.set("key","333",params);
//get
String value=jedis.get("key");
System.out.println("value="+value);
}
public static void main(String[] args) {
//连接到Redis服务器上
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test1(jedis);
}
}
}
public class RedisDemo {
public static void test2(Jedis jedis){
System.out.println("exists和del");
jedis.flushAll();
jedis.set("key","111");
jedis.set("key2","222");
boolean result=jedis.exists("key");
System.out.println("result:"+result);
long result2=jedis.del("key");
System.out.println("result2:"+result2);
result=jedis.exists("key");
System.out.println("result:"+result);
}
public static void main(String[] args) {
//连接到Redis服务器上
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test2(jedis);
}
}
}
public class RedisDemo {
public static void test3(Jedis jedis){
System.out.println("keys");
jedis.flushAll();
jedis.set("key","111");
jedis.set("key2","222");
jedis.set("key3","333");
jedis.set("key4","444");
Set keys=jedis.keys("*");
System.out.println(keys);
}
public static void main(String[] args) {
//连接到Redis服务器上
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test3(jedis);
}
}
}
public class RedisDemo {
public static void test4(Jedis jedis){
System.out.println("expire和ttl");
jedis.flushAll();
jedis.set("key","111");
jedis.expire("key",10);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Long time=jedis.ttl("key");
System.out.println("time:"+time);
}
public static void main(String[] args) {
//连接到Redis服务器上
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test4(jedis);
}
}
}
public class RedisDemo {
public static void test5(Jedis jedis){
System.out.println("type");
jedis.flushAll();
jedis.set("key","111");
String type=jedis.type("key");
System.out.println("type:"+type);
jedis.lpush("key2","111","222","333");
type=jedis.type("key2");
System.out.println("type:"+type);
jedis.hset("key3","f1","111");
type=jedis.type("key3");
System.out.println("type:"+type);
jedis.sadd("key4","111","222","333");
type=jedis.type("key4");
System.out.println("type:"+type);
jedis.zadd("key5",10,"zhangsan");
type=jedis.type("key5");
System.out.println("type:"+type);
}
public static void main(String[] args) {
//连接到Redis服务器上
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test5(jedis);
}
}
}
public class RedisDemoString {
public static void test1(Jedis jedis){
System.out.println("mget和mset");
jedis.flushAll();
jedis.mset("key1","111","key2","222","key3","333");
List values=jedis.mget("key1","key2","key3");
System.out.println("values:"+values);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test1(jedis);
}
}
}
public class RedisDemoString {
public static void test2(Jedis jedis){
System.out.println("getrange和setrange");
jedis.flushAll();
jedis.set("key","abjskjdcd");
String result=jedis.getrange("key",2,5);
System.out.println("result:"+result);
jedis.setrange("key",2,"xyz");
String value=jedis.get("key");
System.out.println("value:"+value);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test2(jedis);
}
}
}
public class RedisDemoString {
public static void test3(Jedis jedis){
System.out.println("append");
jedis.flushAll();
jedis.set("key","abcdef");
jedis.append("key","ghij");
String value=jedis.get("key");
System.out.println("value:"+value);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test3(jedis);
}
}
}
public class RedisDemoString {
public static void test4(Jedis jedis){
System.out.println("incr和decr");
jedis.flushAll();
jedis.set("key","100");
long result=jedis.incr("key");
System.out.println("result:"+result);
String value=jedis.get("key");
System.out.println("value:"+value);
result=jedis.decr("key");
System.out.println("result:"+result);
value=jedis.get("key");
System.out.println("value:"+value);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test4(jedis);
}
}
}
public class RedisDemoList {
public static void test1(Jedis jedis){
System.out.println("lpush和lrange");
jedis.flushAll();
jedis.lpush("key","111","222","333");
List result=jedis.lrange("key",0,-1);
System.out.println(result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test1(jedis);
}
}
}
public class RedisDemoList {
public static void test2(Jedis jedis){
System.out.println("rpush");
jedis.flushAll();
jedis.rpush("key","111","222","333");
List result=jedis.lrange("key",0,-1);
System.out.println(result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test2(jedis);
}
}
}
public class RedisDemoList {
public static void test3(Jedis jedis){
System.out.println("lpop");
jedis.flushAll();
jedis.rpush("key","111","222","333");
String result=jedis.lpop("key");
System.out.println("result:"+result);
result=jedis.lpop("key");
System.out.println("result:"+result);
result=jedis.lpop("key");
System.out.println("result:"+result);
result=jedis.lpop("key");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test3(jedis);
}
}
}
public class RedisDemoList {
public static void test4(Jedis jedis){
System.out.println("lpop");
jedis.flushAll();
jedis.rpush("key","111","222","333");
String result=jedis.rpop("key");
System.out.println("result:"+result);
result=jedis.rpop("key");
System.out.println("result:"+result);
result=jedis.rpop("key");
System.out.println("result:"+result);
result=jedis.rpop("key");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test4(jedis);
}
}
}
public class RedisDemoList {
public static void test5(Jedis jedis){
System.out.println("blpop");
jedis.flushAll();
//返回结果是一个“二元组”,一个是从哪个key对应的list中删除的,一个是删除的元素是什么
List results=jedis.blpop(100,"key");
System.out.println("result[0]:"+results.get(0));
System.out.println("result[1]:"+results.get(1));
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test5(jedis);
}
}
}
public class RedisDemoList {
public static void test6(Jedis jedis){
System.out.println("llen");
jedis.flushAll();
jedis.rpush("key","111","222","333");
long len= jedis.llen("key");
System.out.println("len:"+len);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test5(jedis);
}
}
}
public class RedisDemoSet {
public static void test1(Jedis jedis){
System.out.println("sadd和smembers");
jedis.flushAll();
jedis.sadd("key","111","222","333");
Set result=jedis.smembers("key");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test1(jedis);
}
}
}
public class RedisDemoSet {
public static void test2(Jedis jedis){
System.out.println("sismember");
jedis.flushAll();
jedis.sadd("key","111","222","333");
boolean result=jedis.sismember("key","111");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test2(jedis);
}
}
}
public class RedisDemoSet {
public static void test3(Jedis jedis){
System.out.println("scard");
jedis.flushAll();
jedis.sadd("key","111","222","333");
long result=jedis.scard("key");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test3(jedis);
}
}
}
public class RedisDemoSet {
public static void test4(Jedis jedis){
System.out.println("spop");
jedis.flushAll();
jedis.sadd("key","111","222","333","444","555");
String result=jedis.spop("key");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test4(jedis);
}
}
}
public class RedisDemoSet {
//求交集
public static void test5(Jedis jedis){
System.out.println("sinter");
jedis.flushAll();
jedis.sadd("key","111","222","333");
jedis.sadd("key2","111","222","444");
Set result=jedis.sinter("key","key2");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test5(jedis);
}
}
}
public class RedisDemoSet {
public static void test6(Jedis jedis){
System.out.println("sinterstore");
jedis.flushAll();
jedis.sadd("key","111","222","333");
jedis.sadd("key2","111","222","444");
long len=jedis.sinterstore("key3","key","key2");
System.out.println("len:"+len);
Set result=jedis.smembers("key3");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test6(jedis);
}
}
}
public class RedisDemoHash {
public static void test1(Jedis jedis){
System.out.println("hset和hget");
jedis.flushAll();
jedis.hset("key","f1","111");
Map fields=new HashMap<>();
fields.put("f2","222");
fields.put("f3","333");
jedis.hset("key",fields);
String result=jedis.hget("key","f1");
System.out.println("result:"+result);
result=jedis.hget("key","f2");
System.out.println("result:"+result);
result=jedis.hget("key","f10");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test1(jedis);
}
}
}
public class RedisDemoHash {
public static void test2(Jedis jedis){
System.out.println("hexists");
jedis.flushAll();
jedis.hset("key","f1","111");
jedis.hset("key","f2","222");
jedis.hset("key","f3","333");
boolean result=jedis.hexists("key","f1");
System.out.println("result:"+result);
result=jedis.hexists("key","f100");
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test2(jedis);
}
}
}
public class RedisDemoHash {
public static void test3(Jedis jedis){
System.out.println("hdel");
jedis.flushAll();
jedis.hset("key","f1","111");
jedis.hset("key","f2","111");
jedis.hset("key","f3","111");
long result=jedis.hdel("key","f1","f2");
System.out.println("result:"+result);
boolean exists= jedis.hexists("key","f1");
System.out.println("exists:"+exists);
exists= jedis.hexists("key","f2");
System.out.println("exists:"+exists);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test3(jedis);
}
}
}
public class RedisDemoHash {
public static void test3(Jedis jedis){
System.out.println("hkeys和hvals");
jedis.flushAll();
jedis.hset("key","f1","111");
jedis.hset("key","f2","111");
jedis.hset("key","f3","111");
Set fields=jedis.hkeys("key");
List vals=jedis.hvals("key");
System.out.println("fields:"+fields);
System.out.println("vals:"+vals);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test4(jedis);
}
}
}
public class RedisDemoHash {
public static void test5(Jedis jedis){
System.out.println("hmget和hmset");
jedis.flushAll();
Map map=new HashMap<>();
map.put("f1","111");
map.put("f2","222");
map.put("f3","333");
jedis.hmset("key",map);
List values=jedis.hmget("key","f1","f2","f3");
System.out.println("values:"+values);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test5(jedis);
}
}
}
public class RedisDemoZSet {
public static void test1(Jedis jedis){
System.out.println("zadd和zrange");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
Map map=new HashMap<>();
map.put("lisi",20.0);
map.put("wangwu",30.0);
jedis.zadd("key",map);
List members=jedis.zrange("key",0,-1);
System.out.println("members:"+members);
List memberWithScore=jedis.zrangeWithScores("key",0,-1);
System.out.println("memberWithScore:"+memberWithScore);
String member=memberWithScore.get(0).getElement();
double score=memberWithScore.get(0).getScore();
System.out.println("member:"+member+",score:"+score);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test1(jedis);
}
}
}
public class RedisDemoZSet {
public static void test2(Jedis jedis){
System.out.println("zcard");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
jedis.zadd("key",20,"lisi");
jedis.zadd("key",30,"wangwu");
long len=jedis.zcard("key");
System.out.println("len:"+len);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test2(jedis);
}
}
}
public class RedisDemoZSet {
public static void test3(Jedis jedis){
System.out.println("zrem");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
jedis.zadd("key",20,"lisi");
jedis.zadd("key",30,"wangwu");
long n=jedis.zrem("key","zhangsan");
System.out.println("n:"+n);
List result=jedis.zrangeWithScores("key",0,-1);
System.out.println("result:"+result);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test3(jedis);
}
}
}
public class RedisDemoZSet {
public static void test4(Jedis jedis){
System.out.println("score");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
jedis.zadd("key",20,"lisi");
jedis.zadd("key",30,"wangwu");
Double score=jedis.zscore("key","zhangsan");
System.out.println("score:"+score);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test4(jedis);
}
}
}
public class RedisDemoZSet {
public static void test5(Jedis jedis){
System.out.println("zrank");
jedis.flushAll();
jedis.zadd("key",10,"zhangsan");
jedis.zadd("key",20,"lisi");
jedis.zadd("key",30,"wangwu");
Long rank=jedis.zrank("key","zhangsan");
System.out.println("rank:"+rank);
}
public static void main(String[] args) {
JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis=jedisPool.getResource()){
test5(jedis);
}
}
}
SpringDataRedis是Spring中数据操作的模块,包含对各种数据库的集成,其中Redis的集成模块就叫做SpringDataRedis,官网地址:https://spring.io/projects/spring-data-redis
其中封装了各中对Redis的操作,并且将不同数据类型的操作API封装到了不同的类型中
API | 返回值类型 | 说明 |
redisTemplate.opsForValue() | ValueOperations | 操作String类型数据 |
redisTemplate.opsForHash() | HashOperations | 操作Hash类型数据 |
redisTemplate.opsForList() | ListOperations | 操作List类型数据 |
redisTemplate.opsForSet() | SetOperations | 操作Set类型数据 |
redisTemplate.opsForZSet() | ZSetOperations | 操作ZSet类型数据 |
redisTemplate | 通用的命令 |
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
spring:
redis:
host: 127.0.0.1
port: 8888
password:
lettuce:
pool:
max-active: 8 #最大连接
max-idle: 8 #最大空闲连接
min-idle: 0 #最小空闲连接
max-wait: 100 #连接等待时间
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
//写入一条spring数据
redisTemplate.opsForValue().set("name","baekhyun");
//获取spring数据
Object name=redisTemplate.opsForValue().get("name");
System.out.println("name="+name);
RedisTemplate可以接受任意Object作为值写入Redis,只不过写入前会把Object序列化为字节形式,默认是采用JDK序列化,得到的结果如下所示:
这样可读性差而且内存占用较大
因此我们需要自定义RedisTemplate的序列化方式
1、自定义RedisTemplate;
2、修改RedisTemplate的序列化器GenericJackson2JsonRedisSerializer
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
//创建RedisTemplate对象
RedisTemplate template=new RedisTemplate<>();
//设置连接工厂
template.setConnectionFactory(connectionFactory);
//创建JSON序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer=new GenericJackson2JsonRedisSerializer();
//设置key的序列化
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
//设置value的序列化
template.setValueSerializer(jsonRedisSerializer);
template.setHashValueSerializer(jsonRedisSerializer);
//返回
return template;
}
但是我们会发现存储后的数据 对象的类型占用内存大
为了节省空间,并不会使用JSON序列化器来处理value,而是统一使用String序列化器,要求只能存储String类型的key和value。当需要存储java对象时,手动完成对象的序列化和反序列化
1、使用StringRedisTemplate
2、写入Redis时,手动把对象序列化为json
3、读取Redis时,手动把读取到的JSON反序列化为对象
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ObjectMapper mapper=new ObjectMapper();
@Test
void testSaveUser() throws JsonProcessingException {
//创建对象
User user=new User("do",30);
//手动序列化
String json=mapper.writeValueAsString(user);
//写入数据
stringRedisTemplate.opsForValue().set("user:200",json);
//获取数据
String jsonUser=stringRedisTemplate.opsForValue().get("user:200");
//手动反序列化
User user1=mapper.readValue(jsonUser,User.class);
System.out.println("user1"+user1);
}
//后续redis测试的各种方法,都通过这个Controller提供的http接口来触发
@RestController
public class MyController {
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/testString")
@ResponseBody
public String testString(){
redisTemplate.opsForValue().set("key","111");
redisTemplate.opsForValue().set("key","222");
redisTemplate.opsForValue().set("key","333");
String value=redisTemplate.opsForValue().get("key");
System.out.println("value:"+value);
return "ok";
}
}
界面效果
@GetMapping("/testList")
@ResponseBody
public String testList(){
//先清除之前的数据
redisTemplate.execute((RedisConnection connection)->{
connection.flushAll();
return null;
});
redisTemplate.opsForList().leftPush("key","111");
redisTemplate.opsForList().leftPush("key","222");
redisTemplate.opsForList().leftPush("key","333");
String value=redisTemplate.opsForList().rightPop("key");
System.out.println("value:"+value);
return "ok";
}
@GetMapping("/testSet")
@ResponseBody
public String testSet(){
redisTemplate.execute((RedisConnection connection)->{
connection.flushAll();
return null;
});
redisTemplate.opsForSet().add("key","111","222","333");
Set result=redisTemplate.opsForSet().members("key");
System.out.println("result:"+result);
boolean exists=redisTemplate.opsForSet().isMember("key","111");
System.out.println("exists:"+exists);
Long count=redisTemplate.opsForSet().size("key");
System.out.println("count:"+count);
redisTemplate.opsForSet().remove("key","111","222");
result=redisTemplate.opsForSet().members("key");
System.out.println("result:"+result);
return "ok";
}
@GetMapping("/testHash")
@ResponseBody
public String testHash(){
redisTemplate.execute((RedisConnection connection)->{
connection.flushAll();
return null;
});
redisTemplate.opsForHash().put("key","f1","111");
redisTemplate.opsForHash().put("key","f2","222");
redisTemplate.opsForHash().put("key","f3","333");
String value= (String) redisTemplate.opsForHash().get("key","f1");
System.out.println("value:"+value);
Boolean exists=redisTemplate.opsForHash().hasKey("key","f1");
System.out.println("exists:"+exists);
Long size=redisTemplate.opsForHash().delete("key","f1","f2");
System.out.println("size:"+size);
return "ok";
}
@GetMapping("/testZSet")
@ResponseBody
public String testZSet(){
redisTemplate.execute((RedisConnection connection)->{
connection.flushAll();
return null;
});
redisTemplate.opsForZSet().add("key","zhangsan",10);
redisTemplate.opsForZSet().add("key","lisi",20);
redisTemplate.opsForZSet().add("key","wangwu",30);
Set members=redisTemplate.opsForZSet().range("key",0,-1);
System.out.println("members:"+members);
Set> memberWithScore=redisTemplate.opsForZSet().rangeWithScores("key",0,-1);
System.out.println("memberWithScore:"+memberWithScore);
Double score=redisTemplate.opsForZSet().score("key","zhangsan");
System.out.println("score:"+score);
redisTemplate.opsForZSet().remove("key","zhangsan");
Long size=redisTemplate.opsForZSet().size("key");
System.out.println("size:"+size);
Long rank=redisTemplate.opsForZSet().rank("key","lisi");
System.out.println("rank:"+rank);
return "ok";
}