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(); }
@Override public Boolean hexists(String key, String field) { ShardedJedis jedis = shardedJedisPool.getResource(); try { return jedis.hexists(key, field); } catch (Exception e) { logger.error("Execute redis command failure", e); } finally { jedis.close(); } return Boolean.FALSE; }
public boolean hasLiked(int uid, int bid) { String hashcode = encodeUtil.encode(bid, uid); if (shardedJedis.hexists(hashcode, "isLiked") && shardedJedis.hget(hashcode, "isLiked").equals(BookLikes.LIKED + "")) { return true; } else { return false; } }
/** * 判断key是否存在 * * @param domain 域名 * @param key 键值 * @return */ public boolean existsHSet(String domain, String key) { ShardedJedis shardedJedis = null; boolean isExist = false; try { shardedJedis = slaveShardedJedisPool.getResource(); isExist = shardedJedis.hexists(domain, key); } catch (Exception ex) { logger.error("existsHSet error.", ex); } finally { returnResource(shardedJedis); } return isExist; }
/** * 通过key和field判断是否有指定的value存在 * * @param key * @param field * @return */ public Boolean hexists(String key, String field) { ShardedJedis jedis = null; Boolean res = false; try { jedis = pool.getResource(); res = jedis.hexists(key, field); } catch (Exception e) { pool.returnBrokenResource(jedis); e.printStackTrace(); } finally { returnResource(pool, jedis); } return res; }