程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java操作Redis

Java操作Redis

編輯:JAVA綜合教程

Java操作Redis


目前Jedis是官方推薦的比較好的Redis操作API包,我們這裡結合Spring來看下如何使用Redis這一神器。

1、在pom文件中引入jredis包:


redis.clients
jedis
2.8.1

2、在Spring的resource目錄中增加Redis的配置文件/src/main/resources/redis.properties

# Redis settings
redis.host=192.168.56.101
redis.port=6379
redis.pass=lxh20160330
redis.timeout=0

redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

3、在Spring的配置文件中配置好Redis的相關Bean注入:

 

1   
 2   
10 
11       
12     
13     
14     
15     
16     
17         
18         
19         
20         
21         
22         
23     
24 
25     
26         
27         
28             
29                 
30                     
31                     
32                     
33                     
34                 
35             
36         
37     
38 

4、獲取Redis客戶端對象的類

package com.redis.demo.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.redis.demo.service.RedisService;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Repository("redisService")
public class RedisServiceImpl implements RedisService {
private static final Logger log = LoggerFactory.getLogger(RedisServiceImpl.class);

@Autowired
private ShardedJedisPool shardedJedisPool;

public ShardedJedis getRedisClient() {
try {
ShardedJedis shardJedis = shardedJedisPool.getResource();
return shardJedis;
} catch (Exception e) {
log.error("getRedisClent error", e);
}
return null;
}

public void returnResource(ShardedJedis shardedJedis) {
shardedJedisPool.returnResource(shardedJedis);
}

public void returnResource(ShardedJedis shardedJedis, boolean broken) {
if (broken) {
shardedJedisPool.returnBrokenResource(shardedJedis);
} else {
shardedJedisPool.returnResource(shardedJedis);
}
}
}

5、RedisClient類中實現Redis的相關操作

  package com.redis.demo;
    
    import java.util.Collection;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
   import org.springframework.beans.factory.annotation.Autowired;
   
   import com.redis.demo.service.impl.RedisServiceImpl;
   
   import redis.clients.jedis.BinaryClient.LIST_POSITION;
   import redis.clients.jedis.Jedis;
   import redis.clients.jedis.JedisShardInfo;
   import redis.clients.jedis.ShardedJedis;
   import redis.clients.jedis.ShardedJedisPipeline;
   import redis.clients.jedis.SortingParams;
   import redis.clients.jedis.Tuple;
   

   public class RedisClient {
   
       private static final Logger log = LoggerFactory.getLogger(RedisClient.class);
   
       @Autowired
       private RedisServiceImpl redisService;
   
       public void disconnect() {
           ShardedJedis shardedJedis = redisService.getRedisClient();
           shardedJedis.disconnect();
       }
   
       /**
        * 設置單個值
        */
       public String set(String key, String value) {
           String result = null;
   
           ShardedJedis shardedJedis = redisService.getRedisClient();
           if (shardedJedis == null) {
               return result;
           }
           boolean flag = false;
           try {
               result = shardedJedis.set(key, value);
           } catch (Exception e) {
               log.error(e.getMessage(), e);
               flag = true;
           } finally {
               redisService.returnResource(shardedJedis, flag);
           }
           return result;
       }
   
       /**
        * 獲取單個值
        */
       public String get(String key) {
           String result = null;
           ShardedJedis shardedJedis = redisService.getRedisClient();
           if (shardedJedis == null) {
               return result;
           }
   
           boolean flag = false;
           try {
               result = shardedJedis.get(key);
   
           } catch (Exception e) {
               log.error(e.getMessage(), e);
               flag = true;
           } finally {
               redisService.returnResource(shardedJedis, flag);
           }
           return result;
       }
   
       public Boolean exists(String key) {
           Boolean result = false;
           ShardedJedis shardedJedis = redisService.getRedisClient();
           if (shardedJedis == null) {
               return result;
           }
           boolean flag = false;
           try {
               result = shardedJedis.exists(key);
           } catch (Exception e) {
               log.error(e.getMessage(), e);
               flag = true;
           } finally {
               redisService.returnResource(shardedJedis, flag);
           }
           return result;
      }
  
      public String type(String key) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.type(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      /**
       * 在某段時間後失效
       */
      public Long expire(String key, int seconds) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.expire(key, seconds);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      /**
       * 在某個時間點失效
       */
      public Long expireAt(String key, long time) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.expireAt(key, time);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long ttl(String key) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.ttl(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public boolean setbit(String key, long offset, boolean value) {
  
          ShardedJedis shardedJedis = redisService.getRedisClient();
          boolean result = false;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.setbit(key, offset, value);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public boolean getbit(String key, long offset) {
          ShardedJedis shardedJedis = redisService.getRedisClient();
          boolean result = false;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
  
          try {
              result = shardedJedis.getbit(key, offset);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public long setrange(String key, long offset, String value) {
          ShardedJedis shardedJedis = redisService.getRedisClient();
          long result = ;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.setrange(key, offset, value);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String getrange(String key, long startOffset, long endOffset) {
          ShardedJedis shardedJedis = redisService.getRedisClient();
          String result = null;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.getrange(key, startOffset, endOffset);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String getSet(String key, String value) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.getSet(key, value);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long setnx(String key, String value) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.setnx(key, value);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String setex(String key, int seconds, String value) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.setex(key, seconds, value);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long decrBy(String key, long integer) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.decrBy(key, integer);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long decr(String key) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.decr(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long incrBy(String key, long integer) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.incrBy(key, integer);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long incr(String key) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.incr(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long append(String key, String value) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.append(key, value);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String substr(String key, int start, int end) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.substr(key, start, end);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long hset(String key, String field, String value) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hset(key, field, value);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String hget(String key, String field) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hget(key, field);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long hsetnx(String key, String field, String value) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hsetnx(key, field, value);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String hmset(String key, Map hash) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hmset(key, hash);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public List hmget(String key, String... fields) {
          List result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hmget(key, fields);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long hincrBy(String key, String field, long value) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hincrBy(key, field, value);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Boolean hexists(String key, String field) {
          Boolean result = false;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hexists(key, field);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long del(String key) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.del(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long hdel(String key, String field) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hdel(key, field);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long hlen(String key) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hlen(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Set hkeys(String key) {
          Set result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hkeys(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public List hvals(String key) {
          List result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hvals(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Map hgetAll(String key) {
          Map result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.hgetAll(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      /**
       * 在redis list尾部增加一個String
       * */
      public Long rpush(String key, String string) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.rpush(key, string);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      /**
       * 在redis list頭部增加一個String
       * */
      public Long lpush(String key, String string) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.lpush(key, string);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long llen(String key) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.llen(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public List lrange(String key, long start, long end) {
          List result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.lrange(key, start, end);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String ltrim(String key, long start, long end) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.ltrim(key, start, end);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String lIndex(String key, long index) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.lindex(key, index);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String lset(String key, long index, String value) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.lset(key, index, value);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long lrem(String key, long count, String value) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.lrem(key, count, value);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      /**
       * 從redis list頭部取出一個key
       * */
      public String lpop(String key) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.lpop(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      /**
       * 從redis list尾部取出一個key
       * */
      public String rpop(String key) {
          String result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.rpop(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long sadd(String key, String member) {
          Long result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.sadd(key, member);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Set smembers(String key) {
          Set result = null;
          ShardedJedis shardedJedis = redisService.getRedisClient();
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.smembers(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long srem(String key, String member) {
          ShardedJedis shardedJedis = redisService.getRedisClient();
  
          Long result = null;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.srem(key, member);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String spop(String key) {
          ShardedJedis shardedJedis = redisService.getRedisClient();
          String result = null;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.spop(key);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Long scard(String key) {
          ShardedJedis shardedJedis = redisService.getRedisClient();
          Long result = null;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.scard(key);
  
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public Boolean sismember(String key, String member) {
          ShardedJedis shardedJedis = redisService.getRedisClient();
          Boolean result = null;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.sismember(key, member);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              flag = true;
          } finally {
              redisService.returnResource(shardedJedis, flag);
          }
          return result;
      }
  
      public String srandmember(String key) {
          ShardedJedis shardedJedis = redisService.getRedisClient();
          String result = null;
          if (shardedJedis == null) {
              return result;
          }
          boolean flag = false;
          try {
              result = shardedJedis.srandmember(key);
          } catch (Exception e) {
              log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long zadd(String key, double score, String member) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
             result = shardedJedis.zadd(key, score, member);
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrange(String key, int start, int end) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
             result = shardedJedis.zrange(key, start, end);
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long zrem(String key, String member) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
             result = shardedJedis.zrem(key, member);
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Double zincrby(String key, double score, String member) {
         Double result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zincrby(key, score, member);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long zrank(String key, String member) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrank(key, member);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long zrevrank(String key, String member) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrevrank(key, member);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrevrange(String key, int start, int end) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrevrange(key, start, end);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrangeWithScores(String key, int start, int end) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrangeWithScores(key, start, end);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrevrangeWithScores(String key, int start, int end) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrevrangeWithScores(key, start, end);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long zcard(String key) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zcard(key);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Double zscore(String key, String member) {
         Double result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zscore(key, member);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public List sort(String key) {
         List result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.sort(key);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public List sort(String key, SortingParams sortingParameters) {
         List result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.sort(key, sortingParameters);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long zcount(String key, double min, double max) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zcount(key, min, max);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrangeByScore(String key, double min, double max) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrangeByScore(key, min, max);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrevrangeByScore(String key, double max, double min) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrevrangeByScore(key, max, min);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrangeByScore(String key, double min, double max,
             int offset, int count) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrangeByScore(key, min, max, offset, count);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrevrangeByScore(String key, double max, double min,
             int offset, int count) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis
                     .zrevrangeByScore(key, max, min, offset, count);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrangeByScoreWithScores(String key, double min, double max) {

         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrangeByScoreWithScores(key, min, max);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrevrangeByScoreWithScores(String key, double max,
             double min) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrevrangeByScoreWithScores(key, max, min);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrangeByScoreWithScores(String key, double min,
             double max, int offset, int count) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrangeByScoreWithScores(key, min, max,
                     offset, count);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Set zrevrangeByScoreWithScores(String key, double max,
             double min, int offset, int count) {
         Set result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zrevrangeByScoreWithScores(key, max, min,
                     offset, count);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long zremrangeByRank(String key, int start, int end) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zremrangeByRank(key, start, end);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long zremrangeByScore(String key, double start, double end) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.zremrangeByScore(key, start, end);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     public Long linsert(String key, LIST_POSITION where, String pivot,
             String value) {
         Long result = null;
         ShardedJedis shardedJedis = redisService.getRedisClient();
         if (shardedJedis == null) {
             return result;
         }
         boolean flag = false;
         try {
 
             result = shardedJedis.linsert(key, where, pivot, value);
 
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             flag = true;
         } finally {
             redisService.returnResource(shardedJedis, flag);
         }
         return result;
     }
 
     
     @SuppressWarnings("deprecation")
     public List

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved