Set and get value to Jedis - Java Big Data

Java examples for Big Data:Jedis

Description

Set and get value to Jedis

Demo Code



import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisAPI implements Serializable {

    private static final long serialVersionUID = 7140599341737537733L;

    private final static Logger logger = LoggerFactory
            .getLogger(Jedis.class);

    private static Integer maxActive = 1000;
    private static Integer maxIdle = 20;
    private static Integer maxWait = 3000;
    private static String hostIp = "10.33.3.225";
    private static Integer port = 6379;
    private static JedisPool jedisPool;

    static {/*from ww w  . j  a va 2 s. com*/
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxActive);
        config.setMaxIdle(maxIdle);
        config.setMaxWaitMillis(maxWait);
        jedisPool = new JedisPool(config, hostIp, port);
    }

    @SuppressWarnings("deprecation")
    public static boolean setString(String key, String value) {
        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value))
            return false;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            logger.error("jedis?????", e);
            return false;
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    @SuppressWarnings("deprecation")
    public static boolean setObj(String key, Object obj) {
        if (StringUtils.isEmpty(key) || obj == null)
            return false;

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), JSON.toJSONString(obj).getBytes());
            return true;
        } catch (Exception e) {
            logger.error("jedis", e);
            return false;
        } finally {
            jedisPool.returnResource(jedis);
        }
    }
    @SuppressWarnings("deprecation")
    public static <T> T getObj(String key, Class<T> classType) {
        if (StringUtils.isEmpty(key))
            return null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] value = jedis.get(key.getBytes());
            String jsonStr = new String(value);
            T t = JSON.parseObject(jsonStr.toString(), classType);
            return t;
        } catch (Exception e) {
            logger.error("jedis", e);
            return null;
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    @SuppressWarnings("deprecation")
    public static String getString(String key) {
        if (StringUtils.isEmpty(key))
            return null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);
        } catch (Exception e) {
            logger.error("jedis", e);
            return null;
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    @SuppressWarnings("deprecation")
    public static boolean del(String key) {
        if (StringUtils.isEmpty(key))
            return false;

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
            return true;
        } catch (Exception e) {
            logger.error("jedis", e);
            return false;
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    public static Integer getMaxActive() {
        return maxActive;
    }

    public static void setMaxActive(Integer maxActive) {
        JedisAPI.maxActive = maxActive;
    }

    public static Integer getMaxIdle() {
        return maxIdle;
    }

    public static void setMaxIdle(Integer maxIdle) {
        JedisAPI.maxIdle = maxIdle;
    }

    public static Integer getMaxWait() {
        return maxWait;
    }

    public static void setMaxWait(Integer maxWait) {
        JedisAPI.maxWait = maxWait;
    }

    public static String getHostIp() {
        return hostIp;
    }

    public static void setHostIp(String hostIp) {
        JedisAPI.hostIp = hostIp;
    }

    public static Integer getPort() {
        return port;
    }

    public static void setPort(Integer port) {
        JedisAPI.port = port;
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void main(String[] args) {

    }

}

Related Tutorials