深入学习redis-基于Jedis通过客户端操作Redis

目录

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的官网地址

redis客户端(JAVA)

java生态中,封装了RESP协议,在这里使用jedis实现redis客户端

配置

引入依赖

创建一个maven项目,引入jedis依赖

    <dependencies><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>4.4.2</version></dependency></dependencies>

进行简单配置,把云服务器的端口当成一个本地的端口使用

连接上云服务器上的 redis ,就需要开放 6379 端口:

  1. 将 java 程序打包成 jar 包,放到 linux 服务器上执行(过于麻烦,不推荐);
  2. 匹配 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);}}
}

常用命令实现

get/set

public class RedisDemo {public static void test1(Jedis jedis){System.out.println("get和set的使用");//先清空数据库jedis.flushAll();//setjedis.set("key","111");jedis.set("key2","222");//getString 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();//setjedis.set("key","111");jedis.set("key2","222");SetParams params=new SetParams();params.ex(10);params.xx();jedis.set("key","333",params);//getString 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);}}
}

exists/del

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);}}
}

keys

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<String> 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);}}
}

expire和ttl

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);}}
}

type

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);}}
}

字符串(String)

mget和mset

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<String> 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);}}
}

getrange和setrange

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);}}
}

append

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);}}
}

incr和decr

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);}}
}

列表(list)

lpush和lrange

public class RedisDemoList {public static void test1(Jedis jedis){System.out.println("lpush和lrange");jedis.flushAll();jedis.lpush("key","111","222","333");List<String> 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);}}
}

rpush

public class RedisDemoList {public static void test2(Jedis jedis){System.out.println("rpush");jedis.flushAll();jedis.rpush("key","111","222","333");List<String> 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);}}
}

lpop

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);}}
}

rpop

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);}}
}

blpop

public class RedisDemoList {public static void test5(Jedis jedis){System.out.println("blpop");jedis.flushAll();//返回结果是一个“二元组”,一个是从哪个key对应的list中删除的,一个是删除的元素是什么List<String> 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);}}
}

llen

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);}}
}

集合(set)

sadd和smembers

public class RedisDemoSet {public static void test1(Jedis jedis){System.out.println("sadd和smembers");jedis.flushAll();jedis.sadd("key","111","222","333");Set<String> 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);}}
}

sismember

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);}}
}

scard

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);}}
}

spop

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);}}
}

sinter

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<String> 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);}}
}

sinterstore

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<String> 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);}}
}

哈希(hash)

hset和hget

public class RedisDemoHash {public static void test1(Jedis jedis){System.out.println("hset和hget");jedis.flushAll();jedis.hset("key","f1","111");Map<String,String> 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);}}
}

hexists

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);}}
}

hdel

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);}}
}

hkeys和hvals

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<String> fields=jedis.hkeys("key");List<String> 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);}}
}

hmget和hmset

public class RedisDemoHash {public static void test5(Jedis jedis){System.out.println("hmget和hmset");jedis.flushAll();Map<String,String> map=new HashMap<>();map.put("f1","111");map.put("f2","222");map.put("f3","333");jedis.hmset("key",map);List<String> 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);}}
}

有序集合(zset)

zadd和zrange

public class RedisDemoZSet {public static void test1(Jedis jedis){System.out.println("zadd和zrange");jedis.flushAll();jedis.zadd("key",10,"zhangsan");Map<String,Double> map=new HashMap<>();map.put("lisi",20.0);map.put("wangwu",30.0);jedis.zadd("key",map);List<String> members=jedis.zrange("key",0,-1);System.out.println("members:"+members);List<Tuple> 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);}}
}

zcard

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);}}
}

zrem

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<Tuple> 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);}}
}

zscore

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);}}
}

zrank

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

SpringDataRedis是Spring中数据操作的模块,包含对各种数据库的集成,其中Redis的集成模块就叫做SpringDataRedis,官网地址:https://spring.io/projects/spring-data-redis

  • 提供了对不同Redis客户端的整合(Lettuce和jedis)
  • 提供了RedisTemplate统一API来操作Redis
  • 支持Redis的发布订阅模式
  • 支持Redis哨兵和Redis集群
  • 支持基于Lettuce的响应式编程
  • 支持基于JDK、JSON、字符串、Spring对象的数据序列化及反序列化
  • 支持基于Redis的JDKCollection实现

RedisTemplate工具类

其中封装了各中对Redis的操作,并且将不同数据类型的操作API封装到了不同的类型中

API返回值类型说明
redisTemplate.opsForValue()ValueOperations操作String类型数据
redisTemplate.opsForHash()HashOperations操作Hash类型数据
redisTemplate.opsForList()ListOperations操作List类型数据
redisTemplate.opsForSet()SetOperations操作Set类型数据
redisTemplate.opsForZSet()ZSetOperations操作ZSet类型数据
redisTemplate通用的命令

SpringDataRedis快速入门

引入依赖

		<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>

配置文件

spring:redis:host: 127.0.0.1port: 8888password:lettuce:pool:max-active: 8  #最大连接max-idle: 8  #最大空闲连接min-idle: 0  #最小空闲连接max-wait: 100  #连接等待时间

注入RedisTemplate

	@Autowiredprivate RedisTemplate redisTemplate;

编写测试

	@Testvoid contextLoads() {//写入一条spring数据redisTemplate.opsForValue().set("name","baekhyun");//获取spring数据Object name=redisTemplate.opsForValue().get("name");System.out.println("name="+name);

SpringDataRedis的序列化方式

RedisTemplate可以接受任意Object作为值写入Redis,只不过写入前会把Object序列化为字节形式,默认是采用JDK序列化,得到的结果如下所示:

这样可读性差而且内存占用较大

因此我们需要自定义RedisTemplate的序列化方式

方案一

1、自定义RedisTemplate;

2、修改RedisTemplate的序列化器GenericJackson2JsonRedisSerializer

@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){//创建RedisTemplate对象RedisTemplate<String,Object> 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反序列化为对象

    @Autowiredprivate StringRedisTemplate stringRedisTemplate;	private static final ObjectMapper mapper=new ObjectMapper();@Testvoid 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的方法

string
//后续redis测试的各种方法,都通过这个Controller提供的http接口来触发
@RestController
public class MyController {@Autowiredprivate StringRedisTemplate redisTemplate;@GetMapping("/testString")@ResponseBodypublic 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";}
}

界面效果

                                                

list
    @GetMapping("/testList")@ResponseBodypublic 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";}

set
    @GetMapping("/testSet")@ResponseBodypublic String testSet(){redisTemplate.execute((RedisConnection connection)->{connection.flushAll();return null;});redisTemplate.opsForSet().add("key","111","222","333");Set<String> 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";}

hash
    @GetMapping("/testHash")@ResponseBodypublic 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";}

zset
    @GetMapping("/testZSet")@ResponseBodypublic 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<String> members=redisTemplate.opsForZSet().range("key",0,-1);System.out.println("members:"+members);Set<ZSetOperations.TypedTuple<String>> 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";}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/234288.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

明确技术和质量管理工作的主管领导、技术和质量管理机构的负责人。技术和质量管理机构负责人应当具备中级及以上测绘专业技术职称

明确技术和质量管理工作的主管领导、技术和质量管理机构的负责人。技术和质量管理机构负责人应当具备中级及以上测绘专业技术职称 1.技术和质量管理工作主管领导、技术和质量机构负责人的任命文件 2.技术和质量机构负责人的专业技术职称

Go 从编译到执行

一、Go运行编译简介 Go语言&#xff08;也称为Golang&#xff09;自从2009年由Google发布以来&#xff0c;已成为现代软件开发中不可或缺的一部分。设计者Rob Pike, Ken Thompson和Robert Griesemer致力于解决多核处理器、网络系统和大型代码库所引发的现实世界编程问题。我们…

猜-MISC-bugku-解题步骤

——CTF解题专栏—— 题目信息&#xff1a; 题目&#xff1a;猜 作者&#xff1a;harry 提示&#xff1a; 解题附件&#xff1a;flag格式key{图中人物名字全拼} 解题思路&#xff1a; 这......头都没有&#xff0c;让我guess&#xff1f;&#xff1f;&#xff1f;详细信息看…

如何使用vs2022通过excel.exe生成VC、C++能够使用的头文件

我们在开发MFC、VC、C项目时&#xff0c;有时候需要操作excel文件的读写&#xff0c;我们一般常用方式是调用微软的excel驱动方式调用&#xff0c;但调用驱动前&#xff0c;我们需要生成我们C能够调用到的头文件&#xff0c;一般常用文件有&#xff1a; #include "CAppli…

FastDFS文件系统本地部署结合Nginx与内网穿透实现远程访问本地服务器

文章目录 前言1. 本地搭建FastDFS文件系统1.1 环境安装1.2 安装libfastcommon1.3 安装FastDFS1.4 配置Tracker1.5 配置Storage1.6 测试上传下载1.7 与Nginx整合1.8 安装Nginx1.9 配置Nginx 2. 局域网测试访问FastDFS3. 安装cpolar内网穿透4. 配置公网访问地址5. 固定公网地址5.…

【算法每日一练]-图论(保姆级教程篇9 最小生成树 ,并查集篇)#道路修建 #兽径管理

目录 题目&#xff1a;道路修建 思路&#xff1a; 题目&#xff1a;兽径管理 思路&#xff1a; 题目&#xff1a;道路修建 思路&#xff1a; “让这些点全部连在一起的最小代价”很明显是最小生成树。绝对不能kruskal&#xff0c;存边一定会超内存。所以只能prim。 但是…

AI搜索相关性在网站和APP上的应用

设定场景&#xff1a;您在寻找一件新衣服&#xff0c;所以在浏览最喜欢的网店。您跳到搜索栏上&#xff0c;输入您要找的东西。您期待出现什么结果&#xff1f; 高度准确、相关和即时的结果。 无论在什么网站上搜索&#xff0c;寻找什么&#xff0c;甚至在打错字或使用了错误的…

VirtualBox上安装CentOS7

基础环境&#xff1a;宿主机是64位Windows10操作系统&#xff0c;通过无线网访问网络。 macOS可以以类似方式进行安装&#xff0c;不同之处见最后补充。 Step1 安装VirtualBox VirtualBox是一款免费、开源、高性能的虚拟机软件&#xff0c;可以跨平台运行&#xff0c;支持Wi…

Linux:进程状态

目录 1.Linux内核关于进程状态的源代码 2. 运行状态 3. 阻塞状态 4. 挂起 5.Linux中的进程状态 5.1 睡眠状态 5.2 暂停状态 5.3 僵尸进程与孤儿进程 我们在学习进程状态时&#xff0c;老师只是简单的让我们记住下面这张图 1.教材中进程操作系统的进程状态 那么这些…

Netfilter中的NAT

目录 前瞻 SNAT和DNAT SNAT DNAT 实验 前瞻 NAT: &#xff08;network address translation&#xff09;&#xff0c;支持PREROUTING&#xff0c;INPUT&#xff0c;OUTPUT&#xff0c;POSTROUTING四个链 NAT分为SNAT和DNAT SNAT&#xff1a;支持POSTROUTING, INPUT&…

突发,合肥一废品回收站发生火灾,富维AI神器助力防灾

昨晚&#xff0c;合肥一废品回收站突发火灾&#xff0c;火光冲天&#xff0c;烟雾蔓延。幸亏及时发现&#xff0c;消防人员迅速到场&#xff0c;控制了火势。这起事件让我们再次认识到火灾报警的重要性。而在这方面&#xff0c;北京富维图像公司的FIS智能图像识别系统就发挥了巨…

Redis:主从复制

目录 概念配置步骤通过命令配置主从复制原理薪火相传反客为主哨兵(Sentinel)模式原理配置SpringBoot整合Sentinel模式 概念 主机更新后根据配置和策略&#xff0c;自动同步到备机的master/slave机制&#xff0c;Master以写为主&#xff0c;Slave以读为主。 作用&#xff1a; …