首页 > 基础资料 博客日记
Java客户端使用redis命令
2025-01-16 04:00:08基础资料围观31次
这篇文章介绍了Java客户端使用redis命令,分享给大家做个参考,收藏Java资料网收获更多编程知识
博主主页: 码农派大星.
数据结构专栏:Java数据结构
数据库专栏:数据库
JavaEE专栏:JavaEE
软件测试专栏:软件测试
关注博主带你了解更多知识
目录
使用string命令mget,mset,getrange,setrange
使用set命令sadd,smembers,sismember,scard,spop
使用hash命令hset,hget,hexists,hdel
使用hash命令hkeys,hvals,hmget,hmset
使用zset命令zcard,zrem,zscore,zrank
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);*/
test3(jedis);
}
}
通用命令get,set
public static void test1(Jedis jedis){
System.out.println("get 和 set 使用");
//先清空数据库
jedis.flushAll();
jedis.set("key","111");
jedis.set("key2","222");
SetParams params = new SetParams();
params.ex(10);
params.nx();
jedis.set("key","333",params);
}
通用命令exists和del
public static void test2(Jedis jedis){
System.out.println("exists 和 del");
jedis.flushAll();
jedis.set("key","111");
jedis.set("key2","222");
jedis.set("key3","333");
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);
result2 = jedis.del("key","key2","key3");
System.out.println("result2:"+result2);
}
通用命令keys
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);
}
通用命令expire,ttl,type
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) {
throw new RuntimeException(e);
}
long time = jedis.ttl("key");
System.out.println("time:"+ time);
}
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);
}
使用string命令mget,mset,getrange,setrange
public static void test2(Jedis jedis){
System.out.println("getrange和setrange");
jedis.flushAll();
jedis.set("key","abcdefijk");
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 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);
}
}
使用string命令append,incr,decr
public static void test3(Jedis jedis){
System.out.println("append");
jedis.flushAll();
jedis.set("key","abcdef");
jedis.append("key","ghil");
String value = jedis.get("key");
System.out.println("value:"+value);
}
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);
}
使用list命令lpush,lrange,rpush
public static void test(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 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);
}
使用list命令lpop,rpop,blpop,llen
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 = jedis.lpop("key");
System.out.println(result);
result = jedis.lpop("key");
System.out.println(result);
result = jedis.lpop("key");
System.out.println(result);
}
public static void test4(Jedis jedis){
System.out.println("rpop");
jedis.flushAll();
jedis.rpush("key","111","222","333");
String result = jedis.rpop("key");
System.out.println(result);
result = jedis.rpop("key");
System.out.println(result);
result = jedis.rpop("key");
System.out.println(result);
result = jedis.rpop("key");
System.out.println(result);
}
public static void test5(Jedis jedis){
System.out.println("bloop");
jedis.flushAll();
List<String> results = jedis.blpop(100,"key");
System.out.println("results[0]:" + results.get(0));
System.out.println("results[1]:"+results.get(1));
}
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);
}
使用set命令sadd,smembers,sismember,scard,spop
public static void test(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 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 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 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);
}
使用set命令sinter,sinterstore
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 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);
}
使用hash命令hset,hget,hexists,hdel
public static void test(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","f100");
System.out.println("result:" + result);
}
public static void test2(Jedis jedis){
System.out.println("hexists");
jedis.flushAll();
jedis.hset("key","f1","111");
jedis.hset("key","f2","111");
jedis.hset("key","f3","111");
boolean result = jedis.hexists("key","f1");
System.out.println("result:"+result);
result = jedis.hexists("key","f100");
System.out.println("result:"+ result);
}
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);
}
使用hash命令hkeys,hvals,hmget,hmset
public static void test4(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 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","f2","f1","f3");
System.out.println("values:"+values);
}
使用zset命令zadd,zrange
public static void test(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> membersWithScores = jedis.zrangeWithScores("keys", 0, -1);
System.out.println("membersWithScores:" + membersWithScores);
String member = membersWithScores.get(0).getElement();
double score = membersWithScores.get(0).getScore();
System.out.println("member:"+member+", score"+score);
}
使用zset命令zcard,zrem,zscore,zrank
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 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", "lisi");
System.out.println("n: " + n);
List<Tuple> result = jedis.zrangeWithScores("key", 0, -1);
System.out.println("result: " + result);
}
public static void test4(Jedis jedis) {
System.out.println("zscore");
jedis.flushAll();
jedis.zadd("key", 10, "zhangsan");
jedis.zadd("key", 20, "lisi");
jedis.zadd("key", 30, "wangwu");
Double score = jedis.zscore("key", "zhangsan2");
System.out.println("score: " + score);
}
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", "zhangsan2");
System.out.println("rank: " + rank);
}
文章来源:https://blog.csdn.net/jj666mhhh/article/details/144487484
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: