Redis(Remote Dictionary Server,远程数据字典服务器)是一个开源的高性能内存数据库,常用作缓存服务器使用,也做消息队列使用。因其高性能、丰富的数据类型、可扩展等特性受开发者青睐,这里介绍在java中使用Jedis操作Redis的基本用法。
1. 字符串String。
package com.zws.redis.examples;
import java.util.concurrent.TimeUnit;
import redis.clients.jedis.Jedis;
public class RedisString {
public static void main(String[] args) throws InterruptedException {
String host = "192.168.137.131";
int port = 6379;
String password = "redis";
Jedis jedis = new Jedis(host, port);
jedis.auth(password);
jedis.ping();
jedis.set("name", "张三");//设置
jedis.set("age", "23");
String name = jedis.get("name");//获取
String age = jedis.get("age");
System.out.println("name:" + name + ",age:" + age);
jedis.set("name", "李四");//覆盖
name = jedis.get("name");
System.out.println("name:" + name);
jedis.append("name", "-尼古拉斯");//追加
name = jedis.get("name");//获取
age = jedis.get("age");
System.out.println("name:" + name + ",age:" + age);
jedis.del("name");//删除
name = jedis.get("name");
System.out.println("name:" + name);
jedis.del("name", "age");//删除多个key
name = jedis.get("name");//获取
age = jedis.get("age");
System.out.println("name:" + name + ",age:" + age);
System.out.println("===========新增键值对防止覆盖原先值==============");
System.out.println(jedis.setnx("key1", "value1"));
System.out.println(jedis.setnx("key2", "value2"));
System.out.println(jedis.setnx("key2", "value2-new"));
System.out.println("key1:" + jedis.get("key1"));
System.out.println("key2:" + jedis.get("key2"));
System.out.println("===========新增键值对并设置有效时间=============");
System.out.println(jedis.setex("key3", 2, "value3"));
System.out.println(jedis.get("key3"));
TimeUnit.SECONDS.sleep(3);
System.out.println("key3:" + jedis.get("key3"));
System.out.println("===========获取原值,更新为新值==========");
//GETSET is an atomic set this value and return the old value command.
System.out.println(jedis.getSet("key2", "key2GetSet"));
System.out.println("key2:" + jedis.get("key2"));
System.out.println("截取key2的值:" + jedis.getrange("key2", 2, 4));
jedis.close();
}
}
2. 集合List。
package com.zws.redis.examples;import java.util.List;import redis.clients.jedis.Jedis;public class RedisList {public static void main(String[] args) {String host = "192.168.137.131";int port = 6379;Jedis jedis = new Jedis(host, port);jedis.auth("redis");jedis.ping();System.out.println("===========添加一个list===========");Long cnt = jedis.del("collections");System.out.println("del cnt:" + cnt); jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap"); jedis.lpush("collections", "HashSet"); jedis.lpush("collections", "TreeSet"); jedis.lpush("collections", "TreeMap"); jedis.lpush("collections", "BrrayList"); jedis.lpush("collections", "CrrayList"); jedis.lpush("collections", "CrrayList"); List<String> collections = jedis.lrange("collections", 0, -1); System.out.println("collections的内容:" + collections);//-1代表倒数第一个元素,-2代表倒数第二个元素, 倒序出来 System.out.println("collections区间0-3的元素:"+jedis.lrange("collections", 0, 3));// 倒序出来 System.out.println("==============================="); // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈 System.out.println("删除指定元素个数:"+jedis.lrem("collections", 2, "HashMap")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("删除下标0-3区间之外的元素:"+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添加元素,从列表右端,与lpush相对应:"+jedis.rpush("collections", "EnumMap")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); 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, "LinkedArrayList")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("==============================="); System.out.println("collections的长度:"+jedis.llen("collections")); System.out.println("获取collections下标为2的元素:"+jedis.lindex("collections", 2)); System.out.println("==============================="); jedis.del("sortedList"); jedis.lpush("sortedList", "3","6","2","0","7","4"); System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1)); System.out.println("sortedList返回有序:" + jedis.sort("sortedList")); System.out.println("sortedList排序后:"+jedis.lrange("sortedList", 0, -1));jedis.close();//Redis中还有阻塞式的列表弹出命令以及在列表之间移动元素的命令:blpop, brpop, rpoplpush, brpoplpush等。}}
3. 散列HashMap。
package com.zws.redis.examples;import java.util.HashMap;import java.util.Map;import redis.clients.jedis.Jedis;public class RedisHashMap {public static void main(String[] args) throws InterruptedException {String host = "192.168.137.131";int port = 6379;String password = "redis";Jedis jedis = new Jedis(host, port);jedis.auth(password);jedis.ping();jedis.del("hash");Map<String,String> map = new HashMap<String, String>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); map.put("key4","value4"); jedis.hmset("hash",map); jedis.hset("hash", "key5", "value5"); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("散列hash的所有键为:"+jedis.hkeys("hash")); System.out.println("散列hash的所有值为:"+jedis.hvals("hash")); System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:"+jedis.hincrBy("hash", "key6", 6)); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:"+jedis.hincrBy("hash", "key6", 3)); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("删除一个或者多个键值对:"+jedis.hdel("hash", "key2")); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("散列hash中键值对的个数:"+jedis.hlen("hash")); System.out.println("判断hash中是否存在key2:"+jedis.hexists("hash","key2")); System.out.println("判断hash中是否存在key3:"+jedis.hexists("hash","key3")); System.out.println("获取hash中的值:"+jedis.hmget("hash","key3")); System.out.println("获取hash中的值:"+jedis.hmget("hash","key3","key4"));jedis.close();}}
4. Set。
package com.zws.redis.examples;import redis.clients.jedis.Jedis;public class RedisSet {public static void main(String[] args) throws InterruptedException {String host = "192.168.137.131";int port = 6379;String password = "redis";Jedis jedis = new Jedis(host, port);jedis.auth(password);jedis.ping();jedis.del("eleSet");System.out.println("============向集合中添加元素============");System.out.println(jedis.sadd("eleSet", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"));System.out.println(jedis.sadd("eleSet", "e6"));System.out.println(jedis.sadd("eleSet", "e6"));System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));System.out.println("删除一个元素e0:" + jedis.srem("eleSet", "e0"));System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));System.out.println("删除两个元素e7和e6:" + jedis.srem("eleSet", "e7", "e6"));System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));System.out.println("eleSet中包含元素的个数:" + jedis.scard("eleSet"));System.out.println("e3是否在eleSet中:" + jedis.sismember("eleSet", "e3"));System.out.println("e1是否在eleSet中:" + jedis.sismember("eleSet", "e1"));System.out.println("e2是否在eleSet中:" + jedis.sismember("eleSet", "e2"));System.out.println("=================================");System.out.println(jedis.sadd("eleSet1", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"));System.out.println(jedis.sadd("eleSet2", "e1", "e2", "e4", "e3", "e0", "e8"));System.out.println("将eleSet1中删除e1并存入eleSet3中:" + jedis.smove("eleSet1", "eleSet3", "e1"));System.out.println("将eleSet1中删除e2并存入eleSet3中:" + jedis.smove("eleSet1", "eleSet3", "e2"));System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));System.out.println("eleSet3中的元素:" + jedis.smembers("eleSet3"));System.out.println("============集合运算=================");System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));System.out.println("eleSet2中的元素:" + jedis.smembers("eleSet2"));System.out.println("eleSet1和eleSet2的交集:" + jedis.sinter("eleSet1", "eleSet2"));System.out.println("eleSet1和eleSet2的并集:" + jedis.sunion("eleSet1", "eleSet2"));System.out.println("eleSet1和eleSet2的差集:" + jedis.sdiff("eleSet1", "eleSet2"));// eleSet1中有,eleSet2中没有 //关于Set还有一些其他命令:srandmember, sdiffstore, sinterstore, sunionstore等。jedis.close();}}
5.排序Sort
package com.zws.redis.examples;import redis.clients.jedis.Jedis;import redis.clients.jedis.SortingParams;public class RedisSort {public static void main(String[] args) {String host = "192.168.137.131";int port = 6379;String password = "redis";Jedis jedis = new Jedis(host, port);jedis.auth(password);jedis.ping();jedis.del("collections", "sortedList");jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");System.out.println("collections的内容:" + jedis.lrange("collections", 0, -1));SortingParams sortingParameters = new SortingParams();System.out.println("返回升序集合:" + jedis.sort("collections", sortingParameters.alpha().asc()));System.out.println("返回降序集合:" + jedis.sort("collections", sortingParameters.alpha().desc()));System.out.println("===============================");jedis.lpush("sortedList", "3", "6", "2", "0", "7", "4");System.out.println("sortedList排序前:" + jedis.lrange("sortedList", 0, -1));System.out.println("sortedList升序:" + jedis.sort("sortedList", sortingParameters.asc()));System.out.println("sortedList降序:" + jedis.sort("sortedList", sortingParameters.desc()));System.out.println("===============================");jedis.lpush("userlist", "33");jedis.lpush("userlist", "22");jedis.lpush("userlist", "55");jedis.lpush("userlist", "11");jedis.hset("user:66", "name", "66");jedis.hset("user:55", "name", "55");jedis.hset("user:33", "name", "33");jedis.hset("user:22", "name", "79");jedis.hset("user:11", "name", "24");jedis.hset("user:11", "add", "beijing");jedis.hset("user:22", "add", "shanghai");jedis.hset("user:33", "add", "guangzhou");jedis.hset("user:55", "add", "chongqing");jedis.hset("user:66", "add", "xi'an");sortingParameters = new SortingParams();sortingParameters.get("user:*->name");sortingParameters.get("user:*->add");System.out.println(jedis.sort("userlist", sortingParameters));jedis.close();}}
6. 排序集合SortedSet.
package com.zws.redis;import java.util.HashMap;import java.util.Map;import redis.clients.jedis.Jedis;public class RedisSortedSet {public static void main(String[] args) {String host = "192.168.137.131";int port = 6379;Jedis jedis = new Jedis(host, port);jedis.auth("redis");jedis.ping();jedis.del("englishScore");Map<String, Double> englishScore = new HashMap<String, Double>(); englishScore.put("熊大", 56D); englishScore.put("熊二", 67.9D); englishScore.put("张三", 90D); englishScore.put("李四", 60D); englishScore.put("王五", 40D); System.out.println(jedis.zadd("englishScore", 39D,"赵六")); System.out.println(jedis.zadd("englishScore", englishScore)); System.out.println("englishScore中的所有元素:"+jedis.zrange("englishScore", 0, -1)); System.out.println("englishScore中的所有元素:"+jedis.zrangeWithScores("englishScore", 0, -1)); System.out.println("englishScore中的所有元素:"+jedis.zrangeByScore("englishScore", 0,100)); System.out.println("englishScore中的所有元素:"+jedis.zrangeByScoreWithScores("englishScore", 0,100)); System.out.println("englishScore中张三的分值:"+jedis.zscore("englishScore", "张三")); System.out.println("englishScore中张三的排名:"+jedis.zrank("englishScore", "张三")); System.out.println("删除englishScore中的元素张三:"+jedis.zrem("englishScore", "张三")); System.out.println("englishScore中的所有元素:"+jedis.zrange("englishScore", 0, -1)); System.out.println("englishScore中元素的个数:"+jedis.zcard("englishScore")); System.out.println("englishScore中分值在60-100之间的元素的个数:"+jedis.zcount("englishScore", 60, 100)); System.out.println("熊大的分值加上5:"+jedis.zincrby("englishScore", 5, "熊大")); System.out.println("熊二的分值加上4:"+jedis.zincrby("englishScore", 4, "熊二")); System.out.println("englishScore中的所有元素:"+jedis.zrange("englishScore", 0, -1));jedis.close();}}
7. key相关
package com.zws.redis.examples;import java.util.Set;import java.util.concurrent.TimeUnit;import redis.clients.jedis.Jedis;public class RedisKey {public static void main(String[] args) throws InterruptedException {String host = "192.168.137.131";int port = 6379;Jedis jedis = new Jedis(host, port);jedis.auth("redis");jedis.ping();System.out.println("是否存在username:" + jedis.exists("username"));System.out.println("新增<'username','zzh'>的键值对:" + jedis.set("username", "zzh"));System.out.println("是否存在username:" + jedis.exists("username"));System.out.println("新增<'password','password'>的键值对:" + jedis.set("password", "password"));Set<String> keys = jedis.keys("*");System.out.println("系统中所有的键:" + keys);System.out.println("删除键password:" + jedis.del("password"));System.out.println("判断键password是否存在:" + jedis.exists("password"));System.out.println("设置键username的过期时间为5s:" + jedis.expire("username", 5));TimeUnit.SECONDS.sleep(2);System.out.println("查看键username的剩余生存时间:" + jedis.ttl("username"));System.out.println("移除键username的生存时间:" + jedis.persist("username"));System.out.println("查看键username的剩余生存时间:" + jedis.ttl("username"));System.out.println("查看键username所存储的值的类型:" + jedis.type("username"));jedis.close();}}