深入学习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.mzph.cn/news/187565.shtml

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

相关文章

运行启动vue项目报报错node: --openssl-legacy-provider is not allowed in NODE_OPTIONS解决

报错的问题就是package.json中的Scripts下的dev 解决方法就是要不升级你的应用代码&#xff0c;支持 新版本的node.js 要不就是删除SET NODE_OPTIONS--openssl-legacy-provider &&代码&#xff0c;如下代码即可正常运行起来

windows判断端口是否在使用 bat脚本

脚本 REM 查询端口是否占用 netstat -ano|findstr 3306 >nul &&echo y >1.log ||echo n >1.log REM 读取文本内容赋值给变量 set /P resu<1.log if %resu% y (echo port in use ) else (echo port not in use ) mysql服务不运行的时候检测效果 mysql服…

鸿蒙原生应用/元服务开发-开发者如何进行真机测试

前提条件&#xff1a;已经完成鸿蒙原生应用/元服务开发&#xff0c;已经能相对熟练使用DevEco Studio,开发者自己有鸿蒙4.0及以上的真机设备。 真机测试具体流程如下 1.手机打开开发者模式 2.在项目中&#xff0c;左上角 文件(F)->项目结构 进行账号连接 3.运行

Flash学习

FLASH介绍 FLASH是常用的&#xff0c;用于存储数据的半导体器件&#xff0c;它具有容量大&#xff0c;可重复擦写&#xff0c;按“扇区/块”擦除、掉电后数据可继续保存的特性。 常见的FLASH有NOR FLASH和NAND FLASH。 NOR和NAND是两种数字门电路&#xff0c;可以简单地认为F…

【负载均衡 SLB介绍及其算法详解】(一万两千字)

目录 一、负载均衡 SLB 定义 二、负载均衡SLB的作用 三、负载均衡器&#xff08;Load Balancer&#xff09; 【1】工作原理 【2】主要功能 【3】关键概念 四、工作负载&#xff08;Workload&#xff09; 五、负载均衡算法 【1】轮询&#xff08;Round Robin&#xff0…

python 中文件相对路径 和绝对路径

什么是绝对路径 绝对路径&#xff1a;就是从盘符(c盘、d盘)开始一直到文件所在的具体位置。 例如&#xff1a;xxx.txt 文件的绝对路径为&#xff1a; “C:\Users\xiaoyuzhou\Desktop\file\xxx.txt”相对路径 “相对路径”就是针对“当前文件夹”这一参考对象&#xff0c;来描述…

TZOJ 1375 偶数求和

答案&#xff1a; #include <stdio.h> int main() {int n 0, m 0, j 0, s 0, count1 0,k0;int arr[101] { 0 }; //选择数组是用来控制打印格式while (scanf("%d%d", &n, &m) 2 && (n < 100 && n>0)) //多组数据输入…

计算机网络HTTP篇

目录 一、HTTP基本概念 二、GET 与 POST 2.1、GET 与 POST 有什么区别&#xff1f; 2.2、GET 和 POST 方法都是安全和幂等的吗&#xff1f; 三、HTTP 缓存 3.1、强制缓存&#xff1a; 3.2、协商缓存 四、HTTP 特性 4.1、HTTP/1.1 4.1.1、HTTP/1.1 的优点 4.1.2、HTT…

使用ApexSQLLog工具恢复数据库

目录 前言 一、ApexSQLLog是什么&#xff1f; 二、使用步骤 1.连接你要恢复的数据库 2.选择你要恢复的时间点的数据 3.恢复指定操作的数据 4.恢复指定的表 5.输出结果方式 6.输出结果方式 7.生成还原的sql语句 总结 前言 我们在操作数据库的时候可能误操作把数据修…

【Qt开发流程】之打印文档

描述 Qt为打印提供了广泛的跨平台支持。使用每个平台上的打印系统&#xff0c;Qt应用程序可以打印到连接的打印机上&#xff0c;也可以通过网络打印到远程打印机上。Qt的打印系统还支持PDF文件生成&#xff0c;为基本报表生成工具提供了基础。 支持打印的类 下面的类支持选择…

【经验分享】openGauss 客户端(Data Studio / DBeaver)连接方式

前言 本篇介绍了openGauss常用的客户端连接工具Data Studio和DBeaver 01 客户端工具 openGauss部署之后&#xff0c;在服务器上提供了在命令行下运行的数据库连接工具gsql。此工具除了具备操作数据库的基本功能&#xff0c;还提供了若干高级特性&#xff0c;便于用户使用。…

数据结构---堆

1.堆的概念及结构 堆的性质&#xff1a; 堆中某个节点的值总是不大于或不小于其父节点的值堆总是一棵完全二叉树 2.举例说明 堆一般是把数组数据看做是一棵完全二叉树 小堆要求&#xff1a;任意一个父亲<孩子大堆要求&#xff1a;任意一个父亲>孩子 比如&#xff1…

Zookeeper+Kafka集群

注&#xff1a;本章使用的Kafka为2.7.0版本 Zookeeper概述 1.Zookeeper定义 Zookeeper是一个开源的分布式的&#xff0c;为分布式框架提供协调服务的Apache项目。 2.Zookeeper工作机制 Zookeeper从设计模式角度来理解&#xff1a;是一个基于观察者模式设计的分布式服务管理…

MAMP Pro v6.8.1(PHP/MySQL开发环境)

MAMP Pro是一款专为Mac用户设计的全功能本地服务器软件&#xff0c;可以将电脑变成一个完整的Web开发环境。无论个人开发者、网站管理员还是团队协作&#xff0c;MAMP Pro都提供了强大的工具和便捷的管理方式&#xff0c;能够更加高效地构建和测试网站。 MAMP Pro的基本功能包括…

数据库的多表查询(MYSQL)表表联立

根据以上三张表格&#xff0c;对三张表格进行不同的联立&#xff0c;查询并显示符合条件的内容。 1. 查出至少有一个员工的部门。显示部门编号、部门名称、部门位置、部门人数。 mysql> SELECT d.deptno AS 部门编号, d.dname as 部门名称, d.loc as 部门位置, COUNT(e.emp…

长沙电信大楼火灾调查报告发布:系烟头引发。FIS来护航安全

近日&#xff0c;长沙电信大楼的火灾调查报告引起广泛关注。调查发现&#xff0c;火灾是由未熄灭的烟头引发&#xff0c;烟头点燃了室外平台的易燃物&#xff0c;迅速蔓延至整个建筑。这起悲剧再次提醒我们&#xff0c;小小的疏忽可能酿成大灾难。但如果我们能及时发现并处理这…

大模型的RPA应用 | 代理流程自动化(APA),开启智能自动化新纪元

随着技术创新的持续推进&#xff0c;自动化技术已经变得至关重要&#xff0c;成为驱动企业和社会向前发展的核心动力。在自动化的里程碑中&#xff0c;机器人流程自动化&#xff08;RPA&#xff09;已经有效地将简单、重复且规则性的任务自动化。可是随着对处理更为复杂、多变且…

P29 C++箭头运算符以及它对于结构体与类的指针关系

本期的主要内容是箭头运算符以及它对于结构体与类的指针可以做什么&#xff0c;最后实现我们自己的运算符重载。 01 为什么要使用运算符重载 从开发的角度而言&#xff0c;运算符重载的存在是为了提高开发效率&#xff0c;增加代码的可复用性&#xff0c;很多时候简化了代码。…

【开源】基于Vue+SpringBoot的高校宿舍调配管理系统

项目编号&#xff1a; S 051 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S051&#xff0c;文末获取源码。} 项目编号&#xff1a;S051&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能需求2.1 学生端2.2 宿管2.3 老师端 三、系统…

Android系统源码中添加可编译运行执行程序,java

文章目录 Android系统源码中添加可编译运行执行程序&#xff0c;java1.Android设备中执行编译运行java代码2.编译执行jar包 Android系统源码中添加可编译运行执行程序&#xff0c;java 1.Android设备中执行编译运行java代码 新建一个文件夹&#xff0c;以及Java类的包路径 测…