深入学习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.rhkb.cn/news/206988.html

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

相关文章

STM32F407-14.3.7-01PWM输入模式

PWM 输入模式 此模式是输入捕获模式的一个特例。其实现步骤与输入捕获模式基本相同&#xff0c;仅存在以下不同之处&#xff1a; 例如&#xff0c;可通过以下步骤对应用于 TI1① 的 PWM 的周期&#xff08;位于 TIMx_CCR1⑨ 寄存器中&#xff09;和占空 比&#xff08;位于 …

Echarts 柱状图添加标记 最大值 最小值 平均值

标记 最大值 最小值 series: [//图表配置项 如大小&#xff0c;图表类型{name: 图例,type: bar,//图表类型data: [{value: 500,time: 2012-11-12},{value: 454,time: 2020-5-17},{value: 544,time: 2022-1-22},{value: 877,time: 2013-1-30}, {value: 877,time: 2012-11-12}] …

深入了解Rabbit加密技术:原理、实现与应用

一、引言 在信息时代&#xff0c;数据安全愈发受到重视&#xff0c;加密技术作为保障信息安全的核心手段&#xff0c;得到了广泛的研究与应用。Rabbit加密技术作为一种新型加密方法&#xff0c;具有较高的安全性和便捷性。本文将对Rabbit加密技术进行深入探讨&#xff0c;分析…

【深度学习】概率图模型(一)概率图模型理论简介

文章目录 一、概率图模型1. 联合概率表2. 条件独立性假设3. 三个基本问题 二、模型表示1. 有向图模型&#xff08;贝叶斯网络&#xff09;2. 无向图模型&#xff08;马尔可夫网络&#xff09; 三、学习四、推断 概率图模型&#xff08;Probabilistic Graphical Model&#xff0…

EUREKA: HUMAN-LEVEL REWARD DESIGN VIACODING LARGE LANGUAGE MODELS

目录 一、论文速读 1.1 摘要 1.2 论文概要总结 相关工作 主要贡献 论文主要方法 实验数据 未来研究方向 二、论文精度 2.1 论文试图解决什么问题&#xff1f; 2.2 论文中提到的解决方案之关键是什么&#xff1f; 2.3 用于定量评估的数据集是什么&#xff1f;代码有…

【raect.js + hooks】useRef 搭配 Houdini 创造 useRipple

水波纹点击特效 really cool&#xff0c;实现水波纹的方案也有很多&#xff0c;笔者经常使用 material 组件&#xff0c;非常喜欢 mui 中的 ripple&#xff0c;他家的 ripple 特效就是通过 css Houdini 实现的。 今天&#xff0c;我们将复刻一个 ripple&#xff0c;并封装成 ho…

论文阅读——DINOv

首先是关于给了提示然后做分割的一些方法的总结&#xff1a; 左边一列是prompt类型&#xff0c;右边一列是使用各个类型的prompt的模型。这些模型有分为两大类&#xff1a;Generic和Refer&#xff0c;通用分割和参考分割。Generic seg 是分割和提示语义概念一样的所有的物体&am…

UI 自动化测试框架:PO模式+数据驱动

1. PO 设计模式简介 什么是 PO 模式&#xff1f; PO&#xff08;PageObject&#xff09;设计模式将某个页面的所有元素对象定位和对元素对象的操作封装成一个 Page 类&#xff0c;并以页面为单位来写测试用例&#xff0c;实现页面对象和测试用例的分离。 PO 模式的设计思想与…

Java多线程-第20章

Java多线程-第20章 1.创建线程 Java是一种支持多线程编程的编程语言。多线程是指在同一程序中同时执行多个独立任务的能力。在Java中&#xff0c;线程是一种轻量级的子进程&#xff0c;它是程序中的最小执行单元。Java的多线程编程可以通过两种方式实现&#xff1a;继承Threa…

玩转大数据:3-Hadoop家族的力量与挑战

引言 Hadoop作为一个强大的大数据处理框架&#xff0c;以其分布式计算和存储能力在业界备受关注。然而&#xff0c;Hadoop在应用场景、适用范围、社区支持以及后续持续发展等方面也面临着一些挑战。本文将围绕Hadoop的生态应用&#xff0c;以及来自其他生态的挑战&#xff0c;…

传统算法: Pygame 实现快速排序

使用 Pygame 模块实现了快速排序的动画演示。首先,它生成一个包含随机整数的数组,并通过 Pygame 在屏幕上绘制这个数组的条形图。接着,通过快速排序算法对数组进行排序,动画效果可视化每一步的排序过程。在排序的过程中,程序选择一个基准元素(pivot),将数组分成两部分,…

【Spring MVC】Filter 过滤器异常处理 HandlerExceptionResolver 分析

文章目录 前言版本说明测试 Demo1、自定义过滤器 DemoFilter2、自定义业务异常 ServiceException3、自定义异常处理类 DemoExceptionHandler4、DemoController5、请求测试 问题分析1、日志打印记录2、Debug 方法 解决方案1、修改自定义过滤器2、请求测试 解决方案分析1、日志打…

springmvc(基础学习整合)

SpringMVC是Spring框架提供的构建Web应用程序的全功能MVC模块。 在SpringMVC的各个组件中&#xff0c;处理器映射器、处理器适配器、视图解析器称为SpringMVC的三大组件。 springMVC基本介绍&#xff1a; http://t.csdnimg.cn/TOzw9 MVC是一种设计思想&#xff0c;将一个应…

键盘打字盲打练习系列之刻意练习——1

一.欢迎来到我的酒馆 盲打&#xff0c;刻意练习! 目录 一.欢迎来到我的酒馆二.选择一款工具三.刻意练习第一步&#xff1a;基准键位练习第二步&#xff1a;字母键位练习第三步&#xff1a;数字符号键位练习 四.矫正坐姿 二.选择一款工具 工欲善其事必先利其器。在开始之前&…

【开源】基于Vue.js的医院门诊预约挂号系统的设计和实现

项目编号&#xff1a; S 033 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S033&#xff0c;文末获取源码。} 项目编号&#xff1a;S033&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 功能性需求2.1.1 数据中心模块2.1.2…

java-Swing界面简析

一、简析&#xff1a; 调用java提供的 java.swing包下的各种类可以实现界面中的各种组件(比如输入框、密码框按钮、单选框、复选框等) 二、java.swing包的关键类&#xff1a; 顶层容器&#xff1a;Jframe(窗口) 中间容器&#xff1a;Jpanel(面板) 基本控件&#xff1a; I…

asla四大开源组件应用示例(alsa-lib、alsa-utils、alsa-tools、alsa-plugins)

文章目录 alsa设备文件/dev/snd//sys/class/sound/proc/asoundalsa-lib示例1alsa-utilsalsa-toolsalsa-plugins参考alsa设备文件 /dev/snd/ alsa设备文件目录位于,/dev/snd,如下所示 root@xboard:~#ls /dev/snd -l total 0 drwxr-xr-x 2 root root 60 Nov 6 2023 …

《合成孔径雷达成像算法与实现》_使用CS算法对RADARSAT-1数据进行成像

CSA 简介&#xff1a;Chirp Scaling 算法 (简称 CS 算法&#xff0c;即 CSA) 避免了 RCMC 中的插值操作。该算法基于 Scaling 原理&#xff0c;通过对 chirp 信号进行频率调制&#xff0c;实现了对信号的尺度变换或平移。基于这种原理&#xff0c;可以通过相位相乘代替时域插值…

redis相关题

1 什么是Redis Redis(Remote Dictionary Server) 是⼀个使⽤ C 语⾔编写的&#xff0c;开源的&#xff08;BSD许可&#xff09;⾼性能⾮关系型&#xff08;NoSQL&#xff09;的键值对数据库。Redis 可以存储键和五种不同类型的值之间的映射。键的类型只能为字符串&#xff0c;…

代理模式 1、静态代理 2、动态代理 jdk自带动态代理 3、Cglib代理

文章目录 代理模式1、静态代理2、动态代理jdk自带动态代理 3、Cglib代理 来和大家聊聊代理模式 代理模式 代理模式&#xff1a;即通过代理对象访问目标对象&#xff0c;实现目标对象的方法。这样做的好处是&#xff1a;可以在目标对象实现的基础上&#xff0c;增强额外的功能操…