reids命令可以参考中文官网:http://redis.cn/commands.html
关于reids的使用,可以封装到工具类进行调用:
Redis的工具类:JedisAdapter
除了数据结构:reids还可以用来保持事务的一致性;例如: 1:关于Redis的事务:利用reids的exec命令保证执行,不然就discard回滚
例如声明两个方法:
Transaction multi(Jedis jedis)
List<Object> exec(Transaction tx, Jedis jedis)
public Transaction multi(Jedis jedis) { try { return jedis.multi(); } catch (Exception e) { logger.error("发生异常" + e.getMessage()); } finally { } return null; } public List<Object> exec(Transaction tx, Jedis jedis) { try { return tx.exec(); //保证事务执行 } catch (Exception e) { logger.error("发生异常" + e.getMessage()); tx.discard(); //回滚 } finally { if (tx != null) { try { tx.close(); } catch (IOException ioe) { // .. } } if (jedis != null) { jedis.close(); } } return null; }
2:对于两个方法的使用:
public boolean follow(int userId, int entityType, int entityId) { String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId); String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType); Date date = new Date(); // 实体的粉丝增加当前用户 Jedis jedis = jedisAdapter.getJedis(); Transaction tx = jedisAdapter.multi(jedis); tx.zadd(followerKey, date.getTime(), String.valueOf(userId)); // 当前用户对这类实体关注+1 tx.zadd(followeeKey, date.getTime(), String.valueOf(entityId)); List<Object> ret = jedisAdapter.exec(tx, jedis); return ret.size() == 2 && (Long) ret.get(0) > 0 && (Long) ret.get(1) > 0; }
followee/follower两个队列
3:常见的redis数据结构和使用:
List:双向列表,适用于最新列表,关注列表 lpush lpop blpop lindex lrange lrem linsert lset rpushSet:适用于无顺序的集合,点赞点踩,抽奖,已读,共同好友 sdiff smembers sinter scard SortedSet:排行榜,优先队列 zadd zscore zrange zcount zrank zrevrank Hash:对象属性,不定长属性数 hset hget hgetAll hexists hkeys hvals KV:单一数值,验证码,PV,缓存 set setex incr
Jedis jedis = new Jedis("redis://localhost:6379/9"); jedis.flushDB(); // get set jedis.set("hello", "world"); print(1, jedis.get("hello")); jedis.rename("hello", "newhello"); print(1, jedis.get("newhello")); jedis.setex("hello2", 1800, "world"); // jedis.set("pv", "100"); jedis.incr("pv"); jedis.incrBy("pv", 5);//一次加五 print(2, jedis.get("pv")); jedis.decrBy("pv", 2); print(2, jedis.get("pv")); print(3, jedis.keys("*"));//打印出来所有的key // String listName = "list"; jedis.del(listName); for (int i = 0; i < 10; ++i) { jedis.lpush(listName, "a" + String.valueOf(i)); } print(4, jedis.lrange(listName, 0, 12)); print(4, jedis.lrange(listName, 0, 3)); print(5, jedis.llen(listName));//长度 print(6, jedis.lpop(listName));//先进后出,pop print(7, jedis.llen(listName));//长度 print(8, jedis.lrange(listName, 2, 6)); print(9, jedis.lindex(listName, 3)); print(10, jedis.linsert(listName, BinaryClient.LIST_POSITION.AFTER, "a4", "xx"));//插入 print(10, jedis.linsert(listName, BinaryClient.LIST_POSITION.BEFORE, "a4", "bb"));//删除 print(11, jedis.lrange(listName, 0 ,12)) ; // hash String userKey = "userxx"; jedis.hset(userKey, "name", "jim"); jedis.hset(userKey, "age", "12"); jedis.hset(userKey, "phone", "18618181818"); print(12, jedis.hget(userKey, "name")); print(13, jedis.hgetAll(userKey)); jedis.hdel(userKey, "phone"); print(14, jedis.hgetAll(userKey)); print(15, jedis.hexists(userKey, "email")); print(16, jedis.hexists(userKey, "age")); print(17, jedis.hkeys(userKey)); print(18, jedis.hvals(userKey)); jedis.hsetnx(userKey, "school", "zju"); jedis.hsetnx(userKey, "name", "yxy"); print(19, jedis.hgetAll(userKey)); // set集和 String likeKey1 = "commentLike1"; String likeKey2 = "commentLike2"; for (int i = 0; i < 10; ++i) { jedis.sadd(likeKey1, String.valueOf(i)); jedis.sadd(likeKey2, String.valueOf(i*i)); } print(20, jedis.smembers(likeKey1)); //smembers(key)取值 print(21, jedis.smembers(likeKey2)); print(22, jedis.sunion(likeKey1, likeKey2));//求并集 print(23, jedis.sdiff(likeKey1, likeKey2)); //补集 print(24, jedis.sinter(likeKey1, likeKey2));//交集 print(25, jedis.sismember(likeKey1, "12"));//判断是否存在 print(26, jedis.sismember(likeKey2, "16"));//判断 jedis.srem(likeKey1, "5"); print(27, jedis.smembers(likeKey1)); jedis.smove(likeKey2, likeKey1, "25"); //从一移进2一个25 print(28, jedis.smembers(likeKey1)); print(29, jedis.scard(likeKey1)); //求总和 String rankKey = "rankKey";//优先队列zset() jedis.zadd(rankKey, 15, "jim"); jedis.zadd(rankKey, 60, "Ben"); jedis.zadd(rankKey, 90, "Lee"); jedis.zadd(rankKey, 75, "Lucy"); jedis.zadd(rankKey, 80, "Mei"); print(30, jedis.zcard(rankKey));// zcard()输出 print(31, jedis.zcount(rankKey, 61, 100)); print(32, jedis.zscore(rankKey, "Lucy")); jedis.zincrby(rankKey, 2, "Lucy"); print(33, jedis.zscore(rankKey, "Lucy")); jedis.zincrby(rankKey, 2, "Luc"); print(34, jedis.zscore(rankKey, "Luc")); print(35, jedis.zrange(rankKey, 0, 100)); print(36, jedis.zrange(rankKey, 0, 10));//范围输出 print(36, jedis.zrange(rankKey, 1, 3)); print(36, jedis.zrevrange(rankKey, 1, 3));//从低到高排列 for (Tuple tuple : jedis.zrangeByScoreWithScores(rankKey, "60", "100")) { print(37, tuple.getElement() + ":" + String.valueOf(tuple.getScore())); } print(38, jedis.zrank(rankKey, "Ben")); print(39, jedis.zrevrank(rankKey, "Ben")); String setKey = "zset"; jedis.zadd(setKey, 1, "a"); jedis.zadd(setKey, 1, "b"); jedis.zadd(setKey, 1, "c"); jedis.zadd(setKey, 1, "d"); jedis.zadd(setKey, 1, "e"); print(40, jedis.zlexcount(setKey, "-", "+")); print(41, jedis.zlexcount(setKey, "(b", "[d"));//不包含 print(42, jedis.zlexcount(setKey, "[b", "[d"));//包含,b到d jedis.zrem(setKey, "b"); print(43, jedis.zrange(setKey, 0, 10)); jedis.zremrangeByLex(setKey, "(c", "+"); print(44, jedis.zrange(setKey, 0 ,2)); /*连接池 JedisPool pool = new JedisPool(); for (int i = 0; i < 100; ++i) { Jedis j = pool.getResource(); print(45, j.get("pv")); j.close(); }*/ User user = new User(); user.setName("xx"); user.setPassword("ppp"); user.setHeadUrl("a.png"); user.setSalt("salt"); user.setId(1); print(46, JSONObject.toJSONString(user));//序列化user jedis.set("user1", JSONObject.toJSONString(user)); String value = jedis.get("user1"); User user2 = JSON.parseObject(value, User.class);//反序列化,转化为user对象; print(47, user2); int k = 2;
原文地址:https://www.cnblogs.com/liguo-wang/p/10524798.html
时间: 2024-10-16 10:32:06