Java程序操作Redis(二)

1 部署Redis环境,可参考

http://blog.itpub.net/29485627/viewspace-2139764/

 

2 建立Java Project,并添加commons-pool2-2.2,jedis-2.7.3, log4j-1.2.16, log4j-api-2.7.jar, log4j-core-2.7五个包。

Java程序操作Redis(二)_第1张图片

 

3 在sr/com/zheng下建立RedisUti.java,内容为

package com.zheng;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


public final class RedisUtil {

    private static Logger logger = LogManager.getLogger(RedisUtil.class);

    private static String ADDR = "192.168.121.210";

    private static int PORT = 6379;

    private static String AUTH = null;

    private static int MAX_ACTIVE = 300;

    private static int MAX_IDLE = 200;

    private static int MAX_WAIT = 10000;

    private static int TIMEOUT = 10000;

    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;

    private static Jedis jedis = null;

    static {
        try {
            init();
        } catch (Exception e) {
            logger.error("初始化Redis出错," + e);
        }
    }

    private synchronized static void init() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(MAX_IDLE);
        config.setMaxWaitMillis(MAX_WAIT);
        config.setTestOnBorrow(TEST_ON_BORROW);
        config.setMaxTotal(MAX_ACTIVE);
        jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
    }

    static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                jedis = jedisPool.getResource();
            } else {
                init();
                jedis = jedisPool.getResource();
            }
        } catch (Exception e) {
            logger.error("获取Redis实例出错," + e);
        }
        return jedis;
    }

    public static String set(String key, String value) {
        return set(key, value, null);
    }

    public static String set(String key, String value, Integer timeout) {
        String result = null;

        Jedis jedis = RedisUtil.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.set(key, value);
            if (null != timeout) {
                jedis.expire(key, timeout);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }

    public static String get(String key) {
        String result = null;
        Jedis jedis = RedisUtil.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.get(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }

    public static boolean del(String key) {
        Boolean result = Boolean.FALSE;
        Jedis jedis = RedisUtil.getJedis();
        if (null == jedis) {
            return Boolean.FALSE;
        }
        try {
            jedis.del(key);
        } catch (Exception e) {
            logger.error("Error occured when delete, " + e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }


    public static Long append(String key, String value) {
        Long result = Long.valueOf(0);
        Jedis jedis = RedisUtil.getJedis();
        if (null == jedis) {
            return result;
        }
        try {
            result = jedis.append(key, value);
        } catch (Exception e) {
            logger.error("追加redis数据出错," + e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }

    public static Boolean exists(String key) {
        Boolean result = Boolean.FALSE;
        Jedis jedis = RedisUtil.getJedis();
        if (null == jedis) {
            return result;
        }
        try {
            result = jedis.exists(key);
        } catch (Exception e) {
            logger.error("Error when examine, " + e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return result;
    }
}

4 在src/com/zheng/下建立RedisTest.java,内容为

package com.zheng;
import com.zheng.RedisUtil;

public class RedisTest {
	public static void main(String[] args) {
		RedisUtil.getJedis().set("name", "Zheng");
		System.out.println(RedisUtil.getJedis().get("name"));
	}
}

5 在src下建立log4j2.xml,内容为



  
    
      
    
  
  
     
       


       

    
      
        
  

6 运行RedisTest.java,结果为

Java程序操作Redis(二)_第2张图片


7 代码下载地址为

https://github.com/zhenghaishu/RedisUtil


你可能感兴趣的:(Java)