Redis Client Wrapper Java Class

Articles 2014/08/12 09:43 용비
import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;

import com.github.jedis.lock.JedisLock;

public class RedisClient {
private static AgLogger logger = AgLogger.getLogger(RedisClient.class);
/**
* Added Date : 2014.05.20
*/
private Node node = null;
private JedisPool pool;
/**
* Added Date : 2014.05.20
*/
public RedisClient() {

this.node = new Node();
node.setIpAddr("localhost");
node.setPort(6379);
pool = new JedisPool(new JedisPoolConfig(), node.getIpAddr(), node.getPort());
}
/**
* Added Date : 2014.05.20
* ---------------------------
*
* @param node : Redis Master Node
*/
public RedisClient(Node node) {
this.node = node;
pool = new JedisPool(new JedisPoolConfig(), node.getIpAddr(), node.getPort());
}
/**
* Added Date : 2014.05.23
* ---------------------------
*
* @param host : Redis Master Node IP
* @param port : Redis Master Node Port
*/
public RedisClient(String host, int port) {
this.node = new Node();
node.setIpAddr(host);
node.setPort(port);
pool = new JedisPool(new JedisPoolConfig(), host, port);
}

/**
* Added Date : 2014.06.17
* ---------------------------
* Redis Ping command for health check.
*
* @return
*/
public String ping() throws JedisConnectionException, JedisException {
logger.info("Redis Ping Start.");
Jedis jedis = borrow();
String result = "";
if (jedis != null) {
result = jedis.ping();
}
        revert(jedis);
logger.info("Redis Ping End.");
return result;
}
/**
* Added Date : 2014.06.16
* ---------------------------
* Write changed configurations to local file.
* This is executed Lua script at redis server side.
*
* @return : Lua Script SHA1 value
*/
public String configRewrite() throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
String result = "";
if (jedis != null) {
result = jedis.scriptLoad("return redis.call('config', 'rewrite')");
}

        revert(jedis);
return result;
}
/**
* Added Date : 2014.06.16
* ---------------------------
* Check the configuration rewrite lua script execution result.
*  
* @param sha1 : Script SHA1 value
* @return : result message. if success, "OK" returned.
*/
public Object configRewriteResult(String sha1) throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
Object result = null;

if (jedis != null) {
result = jedis.evalsha(sha1, 0);
}

        revert(jedis);
return result;
}
/**
* Added Date : 2014.06.16
* ---------------------------
* Change the replication setting.
* Set current slave to master.
*
* @return
*/
public String slaveOfNoOne() throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
String result = "";

if (jedis != null) {
result = jedis.slaveofNoOne();
}

        revert(jedis);
return result;
}
/**
* Added Date : 2014.06.16
* ---------------------------
* Change the replication setting.
* This is the same as SLAVEOF command.
*
* @param host : Master Node IP Address
* @param port : Master Node Port
* @return
*/
public String slaveOf(String host, int port) throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
String result = "";
if (jedis != null) {
result = jedis.slaveof(host, port);
}
        revert(jedis);
return result;
}
/**
* Added Date : 2014.06.12
* ---------------------------
*
* @return : all key count
*/
public long dbSize() throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
long result = 0;
if (jedis != null) {
result = jedis.dbSize();
}

        revert(jedis);
return result;
}
/**
* Added Date : 2014.06.09
* ---------------------------
* Redis Configuration Setter
*
* @param parameter : config parameter
* @param value : config value
* @return
*/
public String configSet(String parameter, String value) throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
String result = "";
if (jedis != null) {
result = jedis.configSet(parameter, value);
}

        revert(jedis);
       
return result;
}
/**
* Added Date : 2014.06.09
* ---------------------------
* Get all configuration
*
* @param pattern
* @return
*/
public List<String> configGet(String pattern) throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
List<String> result = null;
if (StringUtils.isNullString(pattern)) {
if (jedis != null) {
result = jedis.configGet("*");
}
}
else {
if (jedis != null) {
result = jedis.configGet(pattern);
}
}
        revert(jedis);
return result;
}
/**
* Added Date : 2014.05.27
* ---------------------------
* Get all keys by pattern
*
* @param pattern
* @return
*/
public Set<String> keys(String pattern) throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
Set<String> result = null;
if (jedis != null) {
result = jedis.keys(pattern);
}

        revert(jedis);
return result;
}
/**
* Added Date : 2014.05.20
* ---------------------------
* auto increment by key
*
* @param key
* @return
*/
public long incr(String key) throws JedisConnectionException, JedisException, InterruptedException {
Jedis jedis = borrow();
long result = 0L;
    JedisLock lock = new JedisLock(jedis,  "incr_lock:" + key, 100, 100);
 
  lock.acquire();
if (jedis != null) {
  result = jedis.incr(key);
}
if (lock != null) {
            lock.release();
}
        revert(jedis);
return result;
}
/**
* Added Date : 2014.05.20
* ---------------------------
* auto decrement by key
*
* @param key
* @return
*/
public long decr(String key) throws JedisConnectionException, JedisException, InterruptedException {
Jedis jedis = borrow();
long result = 0L;
    JedisLock lock = new JedisLock(jedis, "decr_lock:" + key, 100, 100);
  lock.acquire();
if (jedis != null) {
  result = jedis.decr(key);
}
 
if (lock != null) {
            lock.release();
}

revert(jedis);
return result;
}
/**
* Added Date : 2014.05.20
* ---------------------------
* delete info by key
*
* @param key
* @return
*/
public long del(String key) throws JedisConnectionException, JedisException, InterruptedException {
        Jedis jedis = borrow();
long result = 0L;
    JedisLock lock = new JedisLock(jedis, "del_lock:" + key, 100, 100);

  lock.acquire();
if (jedis != null) {
  result = jedis.del(key);
}
   
if (lock != null) {
lock.release();
}
        revert(jedis);
   
    return result;
   
}
/**
* Added Date : 2014.05.20
* ---------------------------
* get info by key
*
* @param key
* @return
*/
public String get(String key) throws JedisConnectionException, JedisException {
Jedis jedis = borrow();
String result = "";
if (jedis != null) {
result = jedis.get(key);
}
        revert(jedis);
return result;
}
/**
* Added Date : 2014.05.20
* ---------------------------
* set info by key (insert, update)
*
* @param key
* @param value
* @return
*/
public String set(String key, String value) throws JedisConnectionException, JedisException, InterruptedException {
        Jedis jedis = borrow();
String result = "";
    JedisLock lock = new JedisLock(jedis, "set_lock:" + key, 100, 100);
       
  lock.acquire();
   
if (jedis != null) {
result = jedis.set(key, value);
}
   
    if (lock != null) {
    lock.release();
    }
   
        revert(jedis);
       
        return result;
}
/**
* Added Date : 2014.05.20
* ---------------------------
* remove all data in db.
* If use this method, critical.
* Be careful.
*
* @return
*/
public String clear() throws JedisConnectionException, JedisException {
        Jedis jedis = borrow();
String result = "";
if (jedis != null) {
result = jedis.flushDB();
}
        revert(jedis);
       
        return result;
}
/**
* Added Date : 2014.05.20
* ---------------------------
* Check the key exists or not.
*
* @param key
* @return
*/
    public boolean exists(String key) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
boolean result = false;

if (jedis != null) {
result = jedis.exists(key);
}
        revert(jedis);
       
        return result;
    }

    /**
  * Added Date : 2014.05.20
* ---------------------------
     * get all redis server info for monitoring
     *
     * @return
     */
    public String info() throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
        String result = "";

if (jedis != null) {
result = jedis.info();
}
       
        revert(jedis);
       
        return result;
    }
   
    /**
  * Added Date : 2014.05.20
* ---------------------------
     * get redis server info for monitoring of given section
     *
     * @param section
     * @return
     */
    public String info(String section) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
        String result = "";

if (jedis != null) {
result = jedis.info(section);
}
       
        revert(jedis);

        return result;
    }
   
    /**
  * Added Date : 2014.06.09
* ---------------------------
     * migration all keys in given slot to target redis server.
     *
     * @param host
     * @param port
     * @param slot
     * @return
     */
    public String migrateKeysInSlot(String host, int port, int slot) throws JedisConnectionException, JedisException {

    String result = "";

    Set<String> keysInSlot = keys("*");
    int curSlot = 0;
    HashSlot hashSlot = new HashSlot();
   
    for (String s : keysInSlot) {
   
    //Calculate slot of key
    curSlot = hashSlot.slotCalculate(s);
   
    if (curSlot == slot) {
    result = migrate(host, port, s);
    }
    }
       
    return result;
   
    }
   
    /**
  * Added Date : 2014.06.09
* ---------------------------
     * migrate given key to target redis server
     *
     * @param host
     * @param port
     * @param key
     * @return
     */
    public String migrate(String host, int port, String key) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
        String result = "";
       
if (jedis != null) {
result = jedis.migrate(host, port, key, 0, 100000);
}
   
        revert(jedis);

        return result;
    }
   
    /**
  * Modified Date : 2014.06.12
* ---------------------------
  * Added Date : 2014.06.09
* ---------------------------
     * migration all keys in given slot to target redis server.
     * redis cluster is working, dev mode.
     *
     * @param host
     * @param port
     * @param slot
     * @return
     */
    @Deprecated
    public String migrate(String host, int port, int slot) throws JedisConnectionException, JedisException {
   
    String result = "";

    Jedis jedis = borrow();
    List<String> keys = jedis.clusterGetKeysInSlot(slot, 1000);
   
    for (String key : keys) {
      result = migrate(host, port, key);
    }
   
        revert(jedis);
       
        return result;
       
    }
   
    @Deprecated
    public byte[] bGetJ(String key) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
        byte[] value = null;
       
        if (jedis != null) {
        value = jedis.get(key.getBytes());
        }
       
        revert(jedis);
       
        if (value != null) {
        return value;
        } else {
            return null;
        }
       
    }
 
    @Deprecated
    public String bSetJ(String key, byte[] value) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
        String result = "";
 
    if (jedis != null) {
    result = jedis.set(key.getBytes(), value);
    }
       
        revert(jedis);
   
        return result;
    }
 
    @Deprecated
   public List<String> mget(String[] keys) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
        List<String> result = null;
               
    if (jedis != null) {
    result = jedis.mget(keys);
    }

        revert(jedis);

        return result;
       
    }
 
    @Deprecated
   public Set<String> sCopy(String key, String new_key) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
        Set<String> oldSets = null;

    if (jedis != null) {
            oldSets = jedis.smembers(key);
            for (String str : oldSets) {
                jedis.sadd(new_key, str);
            }
    }
       
        revert(jedis);

        return oldSets;
       
    }
 
    @Deprecated
    public void sClear(String key, String oldKey) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();

    if (jedis != null) {
            Set<String> oldSets = jedis.smembers(key);
            for (String str : oldSets) {
                jedis.del(oldKey + ":" + str);
            }
            jedis.del(key);
    }

        revert(jedis);
       
    }
 
    @Deprecated
    public Set<String> sMove(String key, String new_key) throws JedisConnectionException, JedisException {
   
        Jedis jedis = borrow();
        Set<String> oldSets = null;
       
    if (jedis != null) {
            oldSets = jedis.smembers(key);
            for (String str : oldSets) {
                jedis.smove(key, new_key, str);
            }
    }

        revert(jedis);

        return oldSets;
    }
   
    /**
     *
     */
    @SuppressWarnings("unused")
private void destory() throws JedisException {
        pool.destroy();
    }
 
/**
*
* @return
*/
private Jedis borrow() throws JedisConnectionException {
        return pool.getResource();
    }
 
    /**
     *
     * @param jedis
     */
    private void revert(Jedis jedis) throws JedisException {
        pool.returnResource(jedis);
    }
   
   
}

받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://www.yongbi.net/rss/response/613

트랙백 주소 :: http://www.yongbi.net/trackback/613

트랙백 RSS :: http://www.yongbi.net/rss/trackback/613

댓글을 달아 주세요

댓글 RSS 주소 : http://www.yongbi.net/rss/comment/613
[로그인][오픈아이디란?]
오픈아이디로만 댓글을 남길 수 있습니다