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

	}
}
posted @   Acode  阅读(1161)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
您是本站第访问量位访问者!
点击右上角即可分享
微信分享提示