Jedis基本操作
public class BasicOperation {
private static String redisServer = "127.0.0.1";
private static int redisPort = 6379;
public static void main(String[] args) {
Jedis jedis = new Jedis(redisServer, redisPort);
String pingRes = jedis.ping();
System.out.println(pingRes);
if ("PONG".equals(pingRes)) {
String key = "test";
jedis.set(key, "test");
System.out.println(jedis.get(key));
System.out.println(jedis.ttl(key));
jedis.expire(key, 10);
System.out.println(jedis.ttl(key));
// list
String listKey = "testList";
jedis.del(listKey);
jedis.rpush(listKey, "1");
jedis.rpush(listKey, "2");
jedis.rpush(listKey, "3");
System.out.println(jedis.llen(listKey));
System.out.println(jedis.lrange(listKey, 0, -1));
jedis.rpop(listKey);
System.out.println(jedis.llen(listKey));
System.out.println(jedis.lrange(listKey, 0, -1));
// set
String setKey = "testSet";
jedis.del(setKey);
jedis.sadd(setKey, "3");
jedis.sadd(setKey, "2");
jedis.sadd(setKey, "1");
System.out.println(jedis.smembers(setKey));
String setKeySecond = "testSetSecond";
jedis.del(setKeySecond);
jedis.sadd(setKeySecond, "10");
jedis.sadd(setKeySecond, "20");
jedis.sadd(setKeySecond, "30");
System.out.println(jedis.smembers(setKeySecond));
System.out.println(jedis.sunion(setKey, setKeySecond));
System.out.println(jedis.smembers(setKey));
System.out.println(jedis.smembers(setKeySecond));
// sorted set
String sortedSetKey = "testSortedSet";
jedis.del(sortedSetKey);
jedis.zadd(sortedSetKey, 1, "3");
jedis.zadd(sortedSetKey, 3, "1");
jedis.zadd(sortedSetKey, 2, "2");
System.out.println(jedis.zrange(sortedSetKey, 0, -1));
System.out.println(jedis.zcard(sortedSetKey));
// hash
String hashKey = "testHash";
jedis.del(hashKey);
jedis.hset(hashKey, "name", "xiaoming");
jedis.hset(hashKey, "age", "10");
jedis.hset(hashKey, "city", "Beijing");
jedis.hset(hashKey, "district", "Haidian");
System.out.println(jedis.hgetAll(hashKey));
jedis.hdel(hashKey, "district");
System.out.println(jedis.hgetAll(hashKey));
} else {
System.out.println("cannot connect to redis");
}
}
}

作者: Acode
出处: http://www.cnblogs.com/acode/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可留言咨询.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析