public List getReverseList(String key, long beginIndex, long endIndex) { listOps = redisTemplate.opsForList(); SortQuery<Serializable> query = SortQueryBuilder.sort((Serializable) key) .order(Order.ASC) .limit(beginIndex, endIndex) .build(); return redisTemplate.sort(query); }
public boolean setnx(final String key, final String value, Long expireTime) { Boolean result = redisTemplate.execute( new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(key.getBytes(), value.getBytes()); } }); if (result) redisTemplate.expire(key, expireTime, TimeUnit.MILLISECONDS); return result; }
// String数据类型 存储一对键值 仅支持value为String类型 public void setString(final String key, final String value) { redisTemplate.execute( new RedisCallback<Serializable>() { public Serializable doInRedis(RedisConnection connection) throws DataAccessException { connection.set( redisTemplate.getStringSerializer().serialize(key), redisTemplate.getStringSerializer().serialize(value)); return null; } }); }
public Object hincrebyAndReturn(final String key, final String field, final Long delta) { List<Object> result = redisTemplate.executePipelined( new RedisCallback<byte[]>() { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { connection.hIncrBy(key.getBytes(), field.getBytes(), delta); connection.hGet(key.getBytes(), field.getBytes()); return null; } }); return result.get(0); }
/** * String数据类型 读取键为key的值仅支持value为String类型 * * @param key * @return 字符串 */ public Serializable getString(final String key) { return redisTemplate.execute( new RedisCallback<Serializable>() { @Override public Serializable doInRedis(RedisConnection connection) throws DataAccessException { byte[] byteKey = redisTemplate.getStringSerializer().serialize(key); if (connection.exists(byteKey)) { byte[] value = connection.get(byteKey); String strValue = redisTemplate.getStringSerializer().deserialize(value); return strValue; } return null; } }); }
public Map<String, Map<byte[], byte[]>> hGetAll(final List<String> keys) { List<Object> results = redisTemplate.executePipelined( new RedisCallback<Map<byte[], byte[]>>() { @Override public Map<byte[], byte[]> doInRedis(RedisConnection redisConnection) throws DataAccessException { for (String key : keys) { redisConnection.hGetAll(key.getBytes()); } return null; } }); Map<String, Map<byte[], byte[]>> resultMap = new HashMap(); for (int i = 0; i < keys.size(); i++) { resultMap.put(keys.get(i), (Map<byte[], byte[]>) results.get(i)); } return resultMap; }
/** * Set数据类型 读取key不同于otherKey集合的元素 * * @param key * @param otherKey * @return set集合 */ public Set getDiffSet(String key, String otherKey) { setOps = redisTemplate.opsForSet(); return (Set) setOps.difference(key, otherKey); }
public boolean hsetnx(String key, Object field, String value) { hashOps = redisTemplate.opsForHash(); return hashOps.putIfAbsent(key, field, value); }
/** * Set数据类型 读取key与otherKey集合的交集元素 * * @param key * @param otherKey * @return set集合 */ public Set getInterSet(String key, String otherKey) { setOps = redisTemplate.opsForSet(); return (Set) setOps.intersect(key, otherKey); }
public TemplateTest(ObjectFactory<Object> objFactory, RedisTemplate template) { this.objFactory = objFactory; this.template = template; ConnectionFactoryTracker.add(template.getConnectionFactory()); }
/** * Hash数据类型 批量 key绑定的hash集合新增元素 * * @param key * @param map */ public void putHash(String key, Map<Object, Object> map) { hashOps = redisTemplate.opsForHash(); hashOps.putAll(key, map); }
// String数据类型 存储一对键值 支持value为非String类型 public void set(String key, Serializable value) { valueOps = redisTemplate.opsForValue(); valueOps.set(key, value); }
/** * Set数据类型 读取key绑定集合的所有元素 * * @param key * @return set集合 */ public Set getAllSet(String key) { setOps = redisTemplate.opsForSet(); return (Set) setOps.members(key); }
/** * Hash数据类型 读取 key绑定的hash集合的keys * * @param key * @return set集合 */ public Set getHashKeys(String key) { hashOps = redisTemplate.opsForHash(); return (Set) hashOps.keys(key); }
public void hincreby(String key, Object field, Integer delta) { hashOps = redisTemplate.opsForHash(); hashOps.increment(key, field, delta); }
/** * Hash数据类型 读取 key绑定的hash集合 * * @param key * @return map集合 key-value */ public Map<Object, Object> getHash(String key) { hashOps = redisTemplate.opsForHash(); return hashOps.entries(key); }
/** * 设置某个key的有效时间 * * @param key * @param timeout 超时时长 * @param unit 时间单位 * @return true设置成功 false设置失败 */ public boolean expire(String key, long timeout, TimeUnit unit) { return redisTemplate.expire(key, timeout, unit); }
/** * Hash数据类型 查找 key绑定的hash集合的大小 * * @param key * @return */ public long getHashSize(String key) { hashOps = redisTemplate.opsForHash(); return hashOps.size(key); }
/** * Hash数据类型 查找 key绑定的hash集合中hashKey的值 * * @param key * @param hashKey * @return */ public Object getHashValue(String key, Object hashKey) { hashOps = redisTemplate.opsForHash(); return hashOps.get(key, hashKey); }
/** * Hash数据类型 查找 key绑定的hash集合中hashKey 的元素是否存在 * * @param key * @param hashKey * @return true存在 false不存在 */ public boolean hasHashKey(String key, Object hashKey) { hashOps = redisTemplate.opsForHash(); return hashOps.hasKey(key, hashKey); }
// 取消某key过期,持久化存储 public boolean persist(String key) { return redisTemplate.persist(key); }
/** * Set数据类型 读取key与otherKey集合的并集元素 * * @param key * @param otherKey * @return set集合 */ public Set getUnionSet(String key, String otherKey) { setOps = redisTemplate.opsForSet(); return (Set) setOps.union(key, otherKey); }
// 删除键为key的键值对 public void delete(String key) { redisTemplate.delete(key); }
public void delete(List<String> keys) { redisTemplate.delete((Collection) keys); }
// String数据类型 存储一对键值并设置有效时间 支持value为非String类型 public void set(String key, Serializable value, long timeout, TimeUnit unit) { valueOps = redisTemplate.opsForValue(); valueOps.set(key, value, timeout, unit); }
// 得到某key有效时长 public long getExpire(String key) { return redisTemplate.getExpire(key); }
// String数据类型 将value追加到已存在的某key的value值末尾 public void append(String key, String value) { valueOps = redisTemplate.opsForValue(); valueOps.append(key, value); }
public boolean hexists(String key, Object field) { hashOps = redisTemplate.opsForHash(); return hashOps.hasKey(key, field); }
@Test public void testKeys() throws Exception { assertTrue(template.keys("*") != null); }
/** * Hash数据类型 key绑定的hash集合新增元素 * * @param key * @param hashKey * @param hashValue */ public void putHash(String key, Object hashKey, Object hashValue) { hashOps = redisTemplate.opsForHash(); hashOps.put(key, hashKey, hashValue); }