redis工具类

import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtils
{
	private static final String IP = "127.0.0.1"; // ip
	private static final int PORT = 6379; // 端口
	private static final String AUTH = ""; // 密码
	private static int MAX_ACTIVE = 1024; // 最大连接数
	private static int MAX_IDLE = 200; // 设置最大空闲数
	private static int MAX_WAIT = 10000; // 最大连接时间
	private static int TIMEOUT = 10000; // 超时时间
	private static boolean BORROW = true; // 在borrow一个事例时是否提前进行validate操作
	private static JedisPool pool = null;
	private static Logger logger = Logger.getLogger(RedisUtils.class);
	static
	{
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(MAX_ACTIVE);
		config.setMaxIdle(MAX_IDLE);
		config.setMaxWaitMillis(MAX_WAIT);
		config.setTestOnBorrow(BORROW);
		pool = new JedisPool(config, IP, PORT, TIMEOUT);
	}

	/**
	 * 获取连接
	 */
	public static synchronized Jedis getJedis()
	{
		try
		{
			if (pool != null)
			{
				return pool.getResource();
			} else
			{
				return null;
			}
		} catch (Exception e)
		{
			logger.info("连接池连接异常");
			return null;
		}

	}

	/**
	 * <p>
	 * 通过key获取储存在redis中的value
	 * </p>
	 * <p>
	 * 并释放连接
	 * </p>
	 * 
	 * @param key
	 * @return 成功返回value 失败返回null
	 */
	public static String get(String key)
	{
		Jedis jedis = null;
		String value = null;
		try
		{

			jedis = pool.getResource();
			value = jedis.get(key);

		} catch (Exception e)
		{

			logger.error(e.getMessage());
		} finally
		{
			getColse(jedis);
		}
		return value;
	}

	/**
	 * @Description:设置失效时间
	 * @param @param key
	 * @param @param seconds
	 * @param @return
	 * @return boolean 返回类型
	 */
	public static void disableTime(String key, int seconds)
	{
		Jedis jedis = null;
		try
		{
			jedis = getJedis();
			jedis.expire(key, seconds);

		} catch (Exception e)
		{
			logger.debug("设置失效失败.");
		} finally
		{
			getColse(jedis);
		}
	}

	/**
	 * @Description:插入对象
	 * @param @param key
	 * @param @param obj
	 * @param @return
	 * @return boolean 返回类型
	 */
	public static boolean addObject(String key, Object obj)
	{

		Jedis jedis = null;
		String value = JSONObject.toJSONString(obj);
		try
		{
			jedis = getJedis();
			String code = jedis.set(key, value);
			if (code.equals("ok"))
			{
				return true;
			}
		} catch (Exception e)
		{
			logger.debug("插入数据有异常.");
			return false;
		} finally
		{
			getColse(jedis);
		}
		return false;
	}

	/**
	 * @Description:存储key~value
	 * @param @param key
	 * @param @param value
	 * @return void 返回类型
	 */

	public static boolean addValue(String key, String value)
	{
		Jedis jedis = null;
		try
		{
			jedis = getJedis();
			String code = jedis.set(key, value);
			if (code.equals("ok"))
			{
				return true;
			}
		} catch (Exception e)
		{
			logger.debug("插入数据有异常.");
			return false;
		} finally
		{
			getColse(jedis);
		}
		return false;
	}

	/**
	 * @Description:删除key
	 * @param @param key
	 * @param @return
	 * @return boolean 返回类型
	 */
	public static boolean delKey(String key)
	{
		Jedis jedis = null;
		try
		{
			jedis = getJedis();
			Long code = jedis.del(key);
			if (code > 1)
			{
				return true;
			}
		} catch (Exception e)
		{
			logger.debug("删除key异常.");
			return false;
		} finally
		{
			getColse(jedis);
		}
		return false;
	}

	/**
	 * 判断key是否存在
	 * 
	 * @param key
	 * @return true OR false
	 */
	public static Boolean exists(String key)
	{
		Jedis jedis = null;
		try
		{
			jedis = pool.getResource();
			return jedis.exists(key);
		} catch (Exception e)
		{

			logger.error(e.getMessage());
			return false;
		} finally
		{
			getColse(jedis);
		}
	}

	/**
	 * @Description: 关闭连接
	 * @param @param jedis
	 * @return void 返回类型
	 */

	public static void getColse(Jedis jedis)
	{
		if (jedis != null)
		{
			jedis.close();
		}
	}

	public static void main(String[] args)
	{
		RedisUtils.addObject("皮皮虾", "pipipxia");
		RedisUtils.disableTime("皮皮虾", 10);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值