package com.common.util;
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set;
import redis.clients.jedis.Jedis;
import com.alibaba.fastjson.JSONObject;
public class RedisUtil {
/*************************************************** ********************************************************/ /** * redis操作字符串 *
* @Description
* @author zhangxiang
* @date: 2017年11月24日 下午2:38:45 */
public static class RedisString { /** * 设置指定 key 的值 * * @param key * @param value * @return */ public static boolean set(String key, String value) { Jedis jedis = null; String sucess = null; try { jedis = JedisPoolUtil.getJedis(); sucess = jedis.set(key, value);
} catch (Exception e)
字
符
串
} /**
{ e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); }
if (\"OK\".equals(sucess)) { return true; } else { return false; }
* 设置指定 key 的值,并指定存活时间 *
* @param key
* @param seconds秒 * @param value * @return */
public static boolean set(String key, int seconds, String value) { Jedis jedis = null; String sucess = null; try { jedis = JedisPoolUtil.getJedis(); sucess = jedis.setex(key, seconds, value); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } if (\"OK\".equals(sucess)) { return true; } else { return false;
} /**
}
* 只有在 key 不存在时设置 key 的值 *
* @param key * @param value * @return */
public static boolean setnx(String key, String value) { Jedis jedis = null; long num = 0; try { jedis = JedisPoolUtil.getJedis(); num = jedis.setnx(key, value); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } if (num == 1) { return true; } else { return false; } } /**
* 获取指定 key 的值 *
* @param key * @return */
public static String get(String key) { Jedis jedis = null; String value = null;
}
try { jedis = JedisPoolUtil.getJedis(); value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); }
return value;
/**
* 将给定 key 的值设为 value ,并返回 key 的旧值(old value) *
* @param key * @param value * @return */
public static String getSet(String key, String value) { Jedis jedis = null; String oldvalue = null; try { jedis = JedisPoolUtil.getJedis(); oldvalue = jedis.getSet(key, value); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return oldvalue; }
/**
* 将 key 中储存的数字值增一 *
* @param key * @return
*/
public static long incr(String key) { Jedis jedis = null; long value = 0; try { jedis = JedisPoolUtil.getJedis(); value = jedis.incr(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return value; } /**
* 将 key 所储存的值加上给定的增量值(increment) *
* @param key
* @param increment
* 增量值 * @return */
public static long incrBy(String key, long increment) { Jedis jedis = null; long value = 0; try { jedis = JedisPoolUtil.getJedis(); value = jedis.incrBy(key, increment); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return value; }
/**
* 将 key 中储存的数字值减一 *
* @param key * @return */
public static long decr(String key) { Jedis jedis = null; long value = 0; try { jedis = JedisPoolUtil.getJedis(); value = jedis.decr(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return value; }
/**
* key 所储存的值减去给定的减量值(decrement) *
* @param key
* @param increment减量值 * @return */
public static long decrBy(String key, long increment) { Jedis jedis = null; long value = 0; try { jedis = JedisPoolUtil.getJedis(); value = jedis.decrBy(key, increment); } catch (Exception e) { e.printStackTrace(); } finally
}
{ JedisPoolUtil.release(jedis); }
return value;
/**
* 是否存在key *
* @param key
* @return true 存在 */
public static boolean exists(String key) { Jedis jedis = null; boolean flag = false; try { jedis = JedisPoolUtil.getJedis(); flag = jedis.exists(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return flag; }
/**
* 设置key存活时间 *
* @param key * @param seconds
* 秒 * @return */
public static boolean expire(String key, int seconds) { Jedis jedis = null; long num = 0; try {
}
jedis = JedisPoolUtil.getJedis(); num = jedis.expire(key, seconds); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); }
if (num == 1) { return true; } else { return false; }
/**
* 删除key *
* @param key * @return */
public static boolean del(String key) { Jedis jedis = null; long num = 0; try { jedis = JedisPoolUtil.getJedis(); num = jedis.del(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } if (num == 1) { return true; } else {
} } /**
return false;
* map转化成字json字符串存储 * @param key * @param map * @return */
public static boolean redisSetMap(String key,Map map) { Jedis jedis = null; String sucess = null; try{ jedis = JedisPoolUtil.getJedis(); sucess = jedis.set(key, JSONObject.toJSONString(map)); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } if (\"OK\".equals(sucess)) { return true; } else { return false; } } /*** map转化成字json字符串存储 * @param key
* @param seconds 秒 * @param map * @return */
public static boolean redisSetMap(String key,int seconds,Map map) { Jedis jedis = null; String sucess = null;} /**
try{ jedis = JedisPoolUtil.getJedis(); sucess = jedis.setex(key, seconds, JSONObject.toJSONString(map)); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); }
if (\"OK\".equals(sucess)) { return true; } else { return false; }
* 将redis存储的字符串转化成map * @param key * @return */
public static Map redisGetMap(String key) { Jedis jedis = null; Map map = new HashMap(); try{ jedis = JedisPoolUtil.getJedis(); String value = jedis.get(key); map = JSONObject.parseObject(value); } catch (Exception e) { e.printStackTrace(); } return map; } /*** List