Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 // 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;
         }
       });
 }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
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;
         }
       });
 }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
 /**
  * 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);
 }
Ejemplo n.º 8
0
 public boolean hsetnx(String key, Object field, String value) {
   hashOps = redisTemplate.opsForHash();
   return hashOps.putIfAbsent(key, field, value);
 }
Ejemplo n.º 9
0
 /**
  * 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);
 }
Ejemplo n.º 10
0
 public TemplateTest(ObjectFactory<Object> objFactory, RedisTemplate template) {
   this.objFactory = objFactory;
   this.template = template;
   ConnectionFactoryTracker.add(template.getConnectionFactory());
 }
Ejemplo n.º 11
0
 /**
  * Hash数据类型 批量 key绑定的hash集合新增元素
  *
  * @param key
  * @param map
  */
 public void putHash(String key, Map<Object, Object> map) {
   hashOps = redisTemplate.opsForHash();
   hashOps.putAll(key, map);
 }
Ejemplo n.º 12
0
 // String数据类型 存储一对键值 支持value为非String类型
 public void set(String key, Serializable value) {
   valueOps = redisTemplate.opsForValue();
   valueOps.set(key, value);
 }
Ejemplo n.º 13
0
 /**
  * Set数据类型 读取key绑定集合的所有元素
  *
  * @param key
  * @return set集合
  */
 public Set getAllSet(String key) {
   setOps = redisTemplate.opsForSet();
   return (Set) setOps.members(key);
 }
Ejemplo n.º 14
0
 /**
  * Hash数据类型 读取 key绑定的hash集合的keys
  *
  * @param key
  * @return set集合
  */
 public Set getHashKeys(String key) {
   hashOps = redisTemplate.opsForHash();
   return (Set) hashOps.keys(key);
 }
Ejemplo n.º 15
0
 public void hincreby(String key, Object field, Integer delta) {
   hashOps = redisTemplate.opsForHash();
   hashOps.increment(key, field, delta);
 }
Ejemplo n.º 16
0
 /**
  * Hash数据类型 读取 key绑定的hash集合
  *
  * @param key
  * @return map集合 key-value
  */
 public Map<Object, Object> getHash(String key) {
   hashOps = redisTemplate.opsForHash();
   return hashOps.entries(key);
 }
Ejemplo n.º 17
0
 /**
  * 设置某个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);
 }
Ejemplo n.º 18
0
 /**
  * Hash数据类型 查找 key绑定的hash集合的大小
  *
  * @param key
  * @return
  */
 public long getHashSize(String key) {
   hashOps = redisTemplate.opsForHash();
   return hashOps.size(key);
 }
Ejemplo n.º 19
0
 /**
  * Hash数据类型 查找 key绑定的hash集合中hashKey的值
  *
  * @param key
  * @param hashKey
  * @return
  */
 public Object getHashValue(String key, Object hashKey) {
   hashOps = redisTemplate.opsForHash();
   return hashOps.get(key, hashKey);
 }
Ejemplo n.º 20
0
 /**
  * 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);
 }
Ejemplo n.º 21
0
 // 取消某key过期,持久化存储
 public boolean persist(String key) {
   return redisTemplate.persist(key);
 }
Ejemplo n.º 22
0
 /**
  * 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);
 }
Ejemplo n.º 23
0
 // 删除键为key的键值对
 public void delete(String key) {
   redisTemplate.delete(key);
 }
Ejemplo n.º 24
0
 public void delete(List<String> keys) {
   redisTemplate.delete((Collection) keys);
 }
Ejemplo n.º 25
0
 // String数据类型 存储一对键值并设置有效时间 支持value为非String类型
 public void set(String key, Serializable value, long timeout, TimeUnit unit) {
   valueOps = redisTemplate.opsForValue();
   valueOps.set(key, value, timeout, unit);
 }
Ejemplo n.º 26
0
 // 得到某key有效时长
 public long getExpire(String key) {
   return redisTemplate.getExpire(key);
 }
Ejemplo n.º 27
0
 // String数据类型  将value追加到已存在的某key的value值末尾
 public void append(String key, String value) {
   valueOps = redisTemplate.opsForValue();
   valueOps.append(key, value);
 }
Ejemplo n.º 28
0
 public boolean hexists(String key, Object field) {
   hashOps = redisTemplate.opsForHash();
   return hashOps.hasKey(key, field);
 }
Ejemplo n.º 29
0
 @Test
 public void testKeys() throws Exception {
   assertTrue(template.keys("*") != null);
 }
Ejemplo n.º 30
0
 /**
  * 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);
 }