引入包:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.1</version> </dependency>常量配置:
public class RedisConfig { //可用连接实例的最大数目,默认值为8; //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 public static int MAX_ACTIVE = 1024; //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 public static int MAX_IDLE = 200; //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; public static long MAX_WAIT = 10000; public static int TIMEOUT = 10000; public static int RETRY_NUM = 5;}JedisUtil 获取Jedis实例及关闭资源
import java.util.HashMap;import java.util.Map;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class JedisUtil { PRivate JedisUtil() { } private static class RedisUtilHolder{ private static final JedisUtil instance = new JedisUtil(); } public static JedisUtil getInstance(){ return RedisUtilHolder.instance; } private static Map<String, JedisPool> maps = new HashMap<String, JedisPool>(); private static JedisPool getPool(String ip, int port) { String key = ip + ":" + port; JedisPool pool = null; if (!maps.containsKey(key)) { JedisPoolConfig config = new JedisPoolConfig(); // config.setMaxActive(RedisConfig.MAX_ACTIVE); //最大连接数 config.setMaxTotal(RedisConfig.MAX_ACTIVE); //最大连接数 config.setMaxIdle(RedisConfig.MAX_IDLE); ////控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。 // config.setMaxWait(RedisConfig.MAX_WAIT); //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnection config.setMaxWaitMillis(RedisConfig.MAX_WAIT); config.setTestOnBorrow(true); //jedis调用borrowObject方法时,是否进行有效检查 config.setTestOnReturn(true); //调用returnObject方法时,是否进行有效检查 pool = new JedisPool(config, ip, port, RedisConfig.TIMEOUT); maps.put(key, pool); } else { pool = maps.get(key); } return pool; } public Jedis getJedis(String ip,int port){ Jedis jedis=null; int count=0; do{ try { count++; jedis=getPool(ip, port).getResource(); } catch (Exception e) { getPool(ip, port).returnBrokenResource(jedis); } }while(jedis==null && count<RedisConfig.RETRY_NUM); return jedis; } public void closeJedis(Jedis jedis,String ip,int port){ if(jedis != null) { getPool(ip,port).returnResource(jedis); } }}测试类:
import java.util.HashMap;import java.util.Map;import java.util.concurrent.TimeUnit;import redis.clients.jedis.Jedis;import redis.clients.jedis.SortingParams;public class Test { private static final String ip = "192.168.150.129"; private static final int port = 6379; private static Jedis jedis = null; public static void main(String[] args) throws InterruptedException { JedisUtil jedisUtil = JedisUtil.getInstance(); jedis=jedisUtil.getJedis(ip, port);// testKey();// testString();// testNumber();// testList();// testSet();// testSortedSet();// testHash(); testSort(); jedisUtil.closeJedis(jedis, ip, port); System.out.println("测试完毕"); } //键操作 public static void testKey() throws InterruptedException{ System.out.println("清空数据:"+jedis.flushDB()); System.out.println("判断某个key是否存在:"+jedis.exists("username")); System.out.println("新增用户:"+jedis.set("username", "zhangsan")); System.out.println("新增密码:"+jedis.set("passWord", "123")); System.out.println("系统存在的key:"+jedis.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")); } //string操作 public static void testString() throws InterruptedException{ jedis.flushDB(); System.out.println("========增加数据=========="); System.out.println(jedis.set("key1", "value1")); System.out.println(jedis.set("key2", "value2")); System.out.println(jedis.set("key3", "value3")); System.out.println("删除key2:"+jedis.del("key2")); System.out.println("获取key2:"+jedis.get("key2")); System.out.println("修改key1:"+jedis.set("key1", "valueChanged")); System.out.println("获取key1的值:"+jedis.get("key1")); System.out.println("在key3的后面追加值:"+jedis.append("key3", "End")); System.out.println("key3的值:"+jedis.get("key3")); System.out.println("增加多个键值对:"+jedis.msetnx("key01","value01","key02","value02","key03","value03")); System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03")); System.out.println("删除多个键值对:"+jedis.del("key01","key02","key03")); System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03")); jedis.flushDB(); 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(jedis.get("key1")); System.out.println(jedis.get("key2")); System.out.println("==========新增键值对并设置有效时间========="); System.out.println(jedis.setex("key3", 3, "value3")); System.out.println(jedis.get("key3")); TimeUnit.SECONDS.sleep(3); System.out.println(jedis.get("key3")); System.out.println("==========获取原值更新为新值========="); System.out.println(jedis.getSet("key2", "abcdefg")); System.out.println(jedis.get("key2")); System.out.println("=========截取字符串==========="); System.out.println(jedis.getrange("key2", 1, -1)); } //数字操作 public static void testNumber(){ jedis.flushDB(); jedis.set("key1", "1"); jedis.set("key2", "2"); System.out.println("key1的值:"+jedis.get("key1")); System.out.println("key2的值:"+jedis.get("key2")); System.out.println("key1的值加1:"+jedis.incr("key1")); System.out.println("获取key1的值:"+jedis.get("key1")); System.out.println("key2的值减1:"+jedis.decr("key2")); System.out.println("获取key2的值:"+jedis.get("key2")); System.out.println("key1的值加上整数5:"+jedis.incrBy("key1", 5)); System.out.println("获取key1的值:"+jedis.get("key1")); System.out.println("key2的值减去5:"+jedis.decrBy("key2", 5)); System.out.println("获取key2的值:"+jedis.get("key2")); } //list操作 public static void testList(){ jedis.flushDB(); System.out.println("============添加一个list=========="); jedis.lpush("collections", "arrayList","vector","stack","hashMap"); jedis.lpush("collections", "hashSet"); jedis.lpush("collections", "treeSet"); jedis.lpush("collections", "linkedHashSet"); System.out.println("collecions的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("collecions区间0-3的内容:"+jedis.lrange("collections", 0, 3)); System.out.println("========================="); //删除列表指定的值,第二个参数为删除的个数(有重复时),后add的先被删除,类似于出栈 System.out.println("删除指定元素个数:"+jedis.lrem("collections", 2, "arrayList")); 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("列表出栈(左端):"+jedis.lpop("collections")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("从列表右端添加元素:"+jedis.rpush("collections", "list1")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("列表出栈(右端):"+jedis.rpop("collections")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("修改指定下标的元素:"+jedis.lset("collections", 0, "list0")); System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); System.out.println("========================="); System.out.println("collections的长度:"+jedis.llen("collections")); System.out.println("获取下标为2的元素:"+jedis.lindex("collections", 2)); 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")); } //set操作 public static void testSet(){ jedis.flushDB(); System.out.println("==========向集合中添加元素============="); jedis.sadd("set", "e0","e2","e3","e4","e5","e6","e7"); jedis.sadd("set", "e6"); jedis.sadd("set", "e6"); System.out.println("set的元素为:"+jedis.smembers("set")); System.out.println("删除一个元素:"+jedis.srem("set", "e0")); System.out.println("set的元素为:"+jedis.smembers("set")); System.out.println("删除两个元素:"+jedis.srem("set", "e2","e3")); System.out.println("set的元素为:"+jedis.smembers("set")); System.out.println("随机的移除集合的一个元素:"+jedis.spop("set")); System.out.println("随机的移除集合的一个元素:"+jedis.spop("set")); System.out.println("set的元素为:"+jedis.smembers("set")); System.out.println("set包含元素的个数:"+jedis.scard("set")); System.out.println("某个元素是否在集合中:"+jedis.sismember("set", "e0")); System.out.println("某个元素是否在集合中:"+jedis.sismember("set", "e4")); System.out.println("某个元素是否在集合中:"+jedis.sismember("set", "e5")); System.out.println("================================="); System.out.println(jedis.sadd("set1", "a","b","c","d","e","f")); System.out.println(jedis.sadd("set2", "a","b","c","d","e")); System.out.println(jedis.smove("set1", "set3", "a")); System.out.println(jedis.smove("set1", "set3", "b")); System.out.println(jedis.smembers("set1")); System.out.println(jedis.smembers("set3")); System.out.println("===================集合运算===================="); System.out.println(jedis.smembers("set1")); System.out.println(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")); } //sortedSet操作 public static void testSortedSet(){ jedis.flushDB(); Map map = new HashMap(); map.put("key2",1.2 ); map.put("key3",4.0); map.put("key4",5.0); map.put("key5",0.2); System.out.println(jedis.zadd("zset", 3, "key1")); System.out.println(jedis.zadd("zset", map)); System.out.println("所有元素:"+jedis.zrange("zset", 0, -1)); System.out.println("所有元素:"+jedis.zrangeWithScores("zset", 0, -1)); System.out.println("所有元素:"+jedis.zrangeByScore("zset", 0, 100)); System.out.println("所有元素:"+jedis.zrangeByScoreWithScores("zset", 0, 100)); System.out.println("zset中key2的分值:"+jedis.zscore("zset", "key2")); System.out.println("zset中key2的排名:"+jedis.zrank("zset", "key2")); System.out.println("删除key3元素:"+jedis.zrem("zset", "key3")); System.out.println("所有元素:"+jedis.zrange("zset", 0, -1)); System.out.println("元素的个数:"+jedis.zcard("zset")); System.out.println("分值在1-4之间的元素个数:"+jedis.zcount("zset", 1, 4)); System.out.println("key2的分值加上5:"+jedis.zincrby("zset", 5, "key2")); System.out.println("key3的分值加上4:"+jedis.zincrby("zset", 4, "key3")); System.out.println("所有元素:"+jedis.zrange("zset", 0, -1)); } //hash操作 public static void testHash(){ jedis.flushDB(); 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保持的值加一个整数,若不存在则创建:"+jedis.hincrBy("hash", "key6", 6)); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("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("键值对的个数:"+jedis.hlen("hash")); System.out.println("判断是否存在key2:"+jedis.hexists("hash", "key2")); System.out.println("判断是否存在key3:"+jedis.hexists("hash", "key3")); System.out.println("获取hash中的值:"+jedis.hmget("hash", "key3")); System.out.println("获取hash中的值:"+jedis.hmget("hash", "key3","key4")); } //排序操作 public static void testSort(){ jedis.flushDB(); jedis.lpush("collections", "ArrayList","Vector","Stack","HashMap","WeakHashMap","LinkedHashMap"); System.out.println("collections的内容为:"+jedis.lrange("collections", 0, -1)); SortingParams sortingParams = new SortingParams(); System.out.println(jedis.sort("collections", sortingParams.alpha())); System.out.println("=============================="); jedis.lpush("sortedList", "3","6","2","0","7","4"); System.out.println("排序前:"+jedis.lrange("sortedList", 0, -1)); System.out.println("升序:"+jedis.sort("sortedList", sortingParams.asc())); System.out.println("降序:"+jedis.sort("sortedList", sortingParams.desc())); System.out.println("==============================="); jedis.lpush("userList", "55"); jedis.lpush("userList", "22"); jedis.lpush("userList", "11"); jedis.lpush("userList", "33"); jedis.hsetnx("user:55", "name", "66"); jedis.hsetnx("user:33", "name", "55"); jedis.hsetnx("user:22", "name", "33"); jedis.hsetnx("user:11", "name", "79"); jedis.hsetnx("user:11", "name", "24"); jedis.hsetnx("user:22", "add", "beijing"); jedis.hsetnx("user:33", "add", "shanghai"); jedis.hsetnx("user:55", "add", "guangzhou"); sortingParams=new SortingParams(); sortingParams.get("user:*->name"); sortingParams.get("user:*->add"); System.out.println(jedis.sort("userList",sortingParams)); }}
新闻热点
疑难解答