赞
踩
Jedis 是 Redis 官方首选的 Java 客户端开发包,也就是使用java操作redis的中间件
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
</dependencies>
操作命令
public class TestPing {
public static void main(String[] args) {
//new Jedis即可
Jedis jedis = new Jedis("127.0.0.1", 6379);
//jedis所有的命令都是之前操作的命令
String ping = jedis.ping();
System.out.println(ping);
}
}
public class TestKey { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("清空数据:" + jedis.flushDB()); System.out.println("判断某个键是否存在:" + jedis.exists("name")); System.out.println("新增键值对:" + jedis.set("name", "juzi")); System.out.println("新增键值对:" + jedis.set("password", "123456")); System.out.println("系统中所有的键如下:"); Set<String> keys = jedis.keys("*"); System.out.println(keys); System.out.println("删除键:" + jedis.decr("password")); System.out.println("判断键是否存在:" + jedis.exists("password")); System.out.println("查看键<name>存储的类型:" + jedis.type("name")); System.out.println("随机返回key空间的一个:" + jedis.randomKey()); System.out.println("重命名key:" + jedis.rename("name", "username")); System.out.println("取出修改后的key:" + jedis.get("username")); System.out.println("按索引查询:" + jedis.select(0)); System.out.println("清空当前数据库中所有的key:" + jedis.flushDB()); System.out.println("返回当前数据库中key的数量:" + jedis.dbSize()); System.out.println("删除所有数据库中所有的key:" + jedis.flushAll()); } }
public class TestString { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("##########增加数据##########"); System.out.println(jedis.set("k1", "v1")); System.out.println(jedis.set("k2", "v2")); System.out.println(jedis.set("k3", "v3")); System.out.println("删除键k2:"+ jedis.del("k2")); System.out.println("获取键k2:"+ jedis.get("k2")); System.out.println("修改k1:"+ jedis.set("k1", "value1")); System.out.println("在k3后面增加值:"+ jedis.append("k3", "End")); System.out.println("获取键k3:"+ jedis.get("k3")); System.out.println("增加多个键值对:"+ jedis.mset("k01","v01", "k02","v02", "k03", "v03")); System.out.println("获取多个键值对:"+ jedis.mget("k01", "k02", "k03")); System.out.println("删除多个键值对:"+ jedis.del("k01", "k02")); System.out.println("获取多个键值对:"+ jedis.mget("k01", "k02", "k03")); jedis.flushDB(); System.out.println("##########新增键值对防止覆盖原先值##########"); System.out.println(jedis.setnx("k1","v1")); System.out.println(jedis.setnx("k2","v2")); System.out.println(jedis.setnx("k2","value2")); System.out.println(jedis.get("k1")); System.out.println(jedis.get("k2")); System.out.println("##########新增键值对并设置有效时间##########"); System.out.println(jedis.setex("k3", 2, "v3")); System.out.println(jedis.get("k3")); try { TimeUnit.SECONDS.sleep(3); }catch (Exception e){ e.printStackTrace(); } System.out.println(jedis.get("k3")); System.out.println("##########获取原键值对并更新为新值##########"); System.out.println(jedis.getSet("k2", "value2")); System.out.println(jedis.get("k2")); System.out.println("获取k2的字符串" + jedis.getrange("k2",1, 5)); } }
public class TestList { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("######添加一个List####"); jedis.lpush("collections", "ArrayList", "Vector", "Stock", "HashMap", "weakHashMap", "LinkedList"); jedis.lpush("collections", "HashSet"); jedis.lpush("collections", "TreeSet"); jedis.lpush("collections", "TreeMap"); System.out.println("collections的所有内容:" + jedis.lrange("collections", 0 , -1)); System.out.println("collections的区间0-3的元素:" + jedis.lrange("collections", 0, 3)); System.out.println("#################################"); System.out.println("删除指定元素个数:" + jedis.lrem("collections", 2, "HashMap")); System.out.println("collections的所有内容:" + jedis.lrange("collections", 0 , -1)); System.out.println("通过下标截取指定范围内的列表:" + jedis.ltrim("collections", 0 ,3 )); System.out.println("collections的所有内容:" + jedis.lrange("collections", 0 , -1)); System.out.println("collections列表出栈(左端):" + jedis.lpop("collections")); System.out.println("collections的所有内容:" + jedis.lrange("collections", 0 , -1)); System.out.println("collections增加元素,从列表右边:" + jedis.rpush("collections", "HashMap")); System.out.println("collections列表出栈(右端):" + jedis.rpop("collections")); System.out.println("collections的所有内容:" + jedis.lrange("collections", 0 , -1)); System.out.println("修改collections下标为1的元素" + jedis.lset("collections", 1, "LinkedList")); System.out.println("collections的所有内容:" + jedis.lrange("collections", 0 , -1)); System.out.println("collections的长度:" + jedis.llen("collections")); System.out.println("获取collections下标为2的元素" + jedis.lindex("collections", 2)); System.out.println("#################################"); jedis.lpush("sortedlist", "3","5","7","4","2"); System.out.println("排序前:" + jedis.lrange("sortedlist" , 0 , -1)); System.out.println(jedis.sort("sortedlist")); System.out.println("排序后:" + jedis.lrange("sortedlist" , 0 , -1)); } }
public class TestSet { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("在集合中添加元素(不重复)"); System.out.println(jedis.sadd("set", "v1","v2","v3","v4","v5")); System.out.println(jedis.sadd("set", "v6")); System.out.println(jedis.sadd("set", "v6")); System.out.println("set中的所有元素:" + jedis.smembers("set")); System.out.println("删除两个元素:" + jedis.srem("set", "v4", "v5")); System.out.println("set中的所有元素:" + jedis.smembers("set")); System.out.println("随机移除set中的元素:" + jedis.srandmember("set")); System.out.println("set中的所有元素:" + jedis.smembers("set")); System.out.println("set中的所有元素数量:" + jedis.scard("set")); System.out.println("判断某一个元素是否存在:" + jedis.sismember("set", "v3")); System.out.println("判断某一个元素是否存在:" + jedis.sismember("set", "v4")); System.out.println("##################################################################"); System.out.println(jedis.sadd("set1", "v1","v2","v3","v4","v5")); System.out.println(jedis.sadd("set2", "v4","v5","v6","v7","v8")); System.out.println("set1中删除某一个元素移动到set2中"+jedis.smove("set1","set3", "v3")); System.out.println("set1中删除某一个元素移动到set2中"+jedis.smove("set1","set3", "v2")); System.out.println("set1中的所有元素:" + jedis.smembers("set1")); System.out.println("set3中的所有元素:" + jedis.smembers("set3")); System.out.println("###################################集合运算###############################"); System.out.println("set1中的所有元素:" + jedis.smembers("set1")); System.out.println("set2中的所有元素:" + jedis.smembers("set2")); System.out.println("set1与set2交集:" + jedis.sinter("set1", "set2")); System.out.println("set1与set2并集:" + jedis.sunion("set1", "set2")); System.out.println("set1与set2差集:" + jedis.sdiff("set1", "set2")); } }
public class TestHash { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("清空数据:" + jedis.flushDB()); Map<String, String> map = new HashMap<>(); map.put("k1", "v1"); map.put("k2", "v2"); map.put("k3", "v3"); map.put("k4", "v4"); jedis.hmset("hash", map); jedis.hset("hash", "k5", "v5"); System.out.println("hash的所有键值对为:" + jedis.hgetAll("hash")); System.out.println("hash的所有键为:" + jedis.hkeys("hash")); System.out.println("hash的所有值为:" + jedis.hvals("hash")); System.out.println("将k6保存的值增加一个整数,不存在则添加:" + jedis.hincrBy("hash", "k6" ,1)); System.out.println("hash的所有键值对为:" + jedis.hgetAll("hash")); System.out.println("将k6保存的值增加一个整数,不存在则添加:" + jedis.hincrBy("hash", "k6" ,1)); System.out.println("hash的所有键值对为:" + jedis.hgetAll("hash")); System.out.println("删除一个或者多个键值对:" + jedis.hdel("hash", "k2")); System.out.println("hash的所有键值对为:" + jedis.hgetAll("hash")); System.out.println("hash的所有键值对的个数为:" + jedis.hlen("hash")); System.out.println("hash是否存在k2:" + jedis.hexists("hash", "k2")); System.out.println("hash是否存在k3:" + jedis.hexists("hash", "k3")); System.out.println("获取hash中的值:" + jedis.hmget("hash", "k3")); System.out.println("获取hash中的值:" + jedis.hmget("hash", "k3", "k4", "k5")); } }
public class TestTx { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "juzi"); jsonObject.put("age", "3"); String s = jsonObject.toJSONString(); Transaction multi = jedis.multi(); try { multi.set("user1", s); multi.set("user2", s); // int i = 1/0; multi.exec(); } catch (Exception e) { jedis.disconnect(); e.printStackTrace(); } finally { System.out.println(jedis.get("user1")); System.out.println(jedis.get("user2")); jedis.close(); } } }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。