Beispiel #1
0
  private void HashOperate() {
    System.out.println("======================hash==========================");
    // 清空数据
    System.out.println(jedis.flushDB());

    System.out.println("=============增=============");
    System.out.println(
        "hashs中添加key001和value001键值对:" + shardedJedis.hset("hashs", "key001", "value001"));
    System.out.println(
        "hashs中添加key002和value002键值对:" + shardedJedis.hset("hashs", "key002", "value002"));
    System.out.println(
        "hashs中添加key003和value003键值对:" + shardedJedis.hset("hashs", "key003", "value003"));
    System.out.println("新增key004和4的整型键值对:" + shardedJedis.hincrBy("hashs", "key004", 4l));
    System.out.println("hashs中的所有值:" + shardedJedis.hvals("hashs"));
    System.out.println();

    System.out.println("=============删=============");
    System.out.println("hashs中删除key002键值对:" + shardedJedis.hdel("hashs", "key002"));
    System.out.println("hashs中的所有值:" + shardedJedis.hvals("hashs"));
    System.out.println();

    System.out.println("=============改=============");
    System.out.println("key004整型键值的值增加100:" + shardedJedis.hincrBy("hashs", "key004", 100l));
    System.out.println("hashs中的所有值:" + shardedJedis.hvals("hashs"));
    System.out.println();

    System.out.println("=============查=============");
    System.out.println("判断key003是否存在:" + shardedJedis.hexists("hashs", "key003"));
    System.out.println("获取key004对应的值:" + shardedJedis.hget("hashs", "key004"));
    System.out.println("批量获取key001和key003对应的值:" + shardedJedis.hmget("hashs", "key001", "key003"));
    System.out.println("获取hashs中所有的key:" + shardedJedis.hkeys("hashs"));
    System.out.println("获取hashs中所有的value:" + shardedJedis.hvals("hashs"));
    System.out.println();
  }
 /**
  * 通过key给指定的field的value加上给定的值
  *
  * @param key
  * @param field
  * @param value
  * @return
  */
 public Long hincrby(String key, String field, Long value) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.hincrBy(key, field, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
  public void changeBooklikes(BookLikes bookLikes) {

    if (bookLikes == null) {
      return;
    }
    if (bookLikes.getLike_time() == null) {
      bookLikes.setLike_time(new Date());
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
    String time = sdf.format(bookLikes.getLike_time());

    String hashcode = encodeUtil.encode(bookLikes.getBid(), bookLikes.getUid());

    shardedJedis.hdel(hashcode, "bid", "uid", "like_time", "isLiked");
    shardedJedis.hincrBy(hashcode, "bid", bookLikes.getBid());
    shardedJedis.hincrBy(hashcode, "uid", bookLikes.getUid());
    shardedJedis.hset(hashcode, "like_time", time);
    if (bookLikes.getIsLiked() == BookLikes.LIKED) {
      shardedJedis.hincrBy(hashcode, "isLiked", BookLikes.UNLIKED);
    } else {
      shardedJedis.hincrBy(hashcode, "isLiked", BookLikes.LIKED);
    }
  }