您的当前位置:首页正文

redis使用

2022-06-10 来源:易榕旅网
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> 转化成字json字符串存储 * @param key * @param listMap * @return

*/

public static boolean redisSetListMap(String key,List> listMap) { Jedis jedis = null; String sucess = null; try{ jedis = JedisPoolUtil.getJedis(); sucess = jedis.set(key, JSONObject.toJSONString(listMap)); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } if (\"OK\".equals(sucess)) { return true; } else { return false; } } /**

* List> 转化成字json字符串存储 * @param key

* @param seconds 秒 * @param listMap * @return */

public static boolean redisSetListMap(String key,int seconds,List> {

listMap)

Jedis jedis = null;

String sucess = null; try{ jedis = JedisPoolUtil.getJedis(); sucess = jedis.setex(key, seconds, JSONObject.toJSONString(listMap)); } catch (Exception e) { e.printStackTrace(); } finally {

}

} /**

JedisPoolUtil.release(jedis); }

if (\"OK\".equals(sucess)) { return true; } else { return false; }

* 将redis存储的字符串转化成> * @param key * @return */

@SuppressWarnings(\"unchecked\")

public static List> getListMap(String key) { List> listMap = new ArrayList>(); Jedis jedis = null; try{ jedis = JedisPoolUtil.getJedis(); listMap = (List>) JSONObject.parse(jedis.get(key)); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return listMap; }

/**

* redis操作Hash *

* @Description

* @author zhangxiang

* @date: 2017年11月24日 下午2:42:21 */

public static class redisHash {

/**

* 将哈希表 key 中的字段 field 的值设为 value *

* @param key * @param field * @param value * @return */

public static boolean hset(String key, String field, String value) { Jedis jedis = null; long num = 0; try { jedis = JedisPoolUtil.getJedis(); num = jedis.hset(key, field, value); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } if (num == 1) { return true; } else { return false; } }

/**

* 获取哈希表 key 中的字段 field 的值设 *

* @param key * @param field * @return */

public static String hget(String key, String field) { Jedis jedis = null; String value = null; try

}

{ jedis = JedisPoolUtil.getJedis(); value = jedis.hget(key, field); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); }

return value;

/**

* 获取哈希表中所有值 *

* @param key * @return */

public static List hvals(String key) { Jedis jedis = null; List list = new ArrayList(); try { jedis = JedisPoolUtil.getJedis(); list = jedis.hvals(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return list; }

/**

* 保存一个map(重复的不会保存) *

* @param key * @param hash

* @return */

public static boolean hmset(String key, Map hash) { Jedis jedis = null; String value = null; try { jedis = JedisPoolUtil.getJedis(); try { jedis = JedisPoolUtil.getJedis(); value = jedis.hmset(key, hash); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } if (\"OK\".equals(value)) { return true; } else { return false; } }

/**

* 返回map对象中的所有key *

* @param key * @return */

public static Set hkeys(String key) {

}

Set set = new HashSet(); Jedis jedis = null; try { jedis = JedisPoolUtil.getJedis(); try { jedis = JedisPoolUtil.getJedis(); set = jedis.hkeys(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); }

} catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return set;

/**

* 返回map对象中的所有value *

* @param key * @return */

public static List hmgetMapValue(String key) { List list = hvals(key); return list; } /**

* 获取存储的map * @param key * @return */

}

public static Map hgetAll(String key) { Map map = new HashMap(); Jedis jedis = null; try { jedis = JedisPoolUtil.getJedis(); try { jedis = JedisPoolUtil.getJedis(); map = jedis.hgetAll(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return map; }

/**

* redis操作set *

* @Description

* @author zhangxiang

* @date: 2017年11月24日 下午2:50:13 */

public static class redisSet { /** * 向集合添加一个或多个成员 * @param key * @param set * @return 影响的行数

*/

public static long sadd(String key, Set set) { Jedis jedis = null; long row = 0; try { jedis = JedisPoolUtil.getJedis(); for (String member : set) { row += jedis.sadd(key, member); } } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return row; }

/**

* 向集合添加一个或多个成员 * @param key * @param set

* @return 影响的行数 */

public static long sadd(String key, List list) { Jedis jedis = null; long row = 0; try { jedis = JedisPoolUtil.getJedis(); for (String member : list) { row += jedis.sadd(key, member); } } catch (Exception e) { e.printStackTrace(); } finally {

}

JedisPoolUtil.release(jedis); }

return row;

/**

* 获取集合的成员数 *

* @param key * @return num */

public static long scard(String key) { Jedis jedis = null; long num = 0; try { jedis = JedisPoolUtil.getJedis(); num = jedis.scard(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return num; } /**

* 返回集合中的所有成员 *

* @param key * @return */

public static Set smembers(String key) { Jedis jedis = null; Set set = new HashSet(); try { jedis = JedisPoolUtil.getJedis(); set = jedis.smembers(key); } catch (Exception e)

}

} /**

{ e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); }

return set;

* 移除集合中一个成员 *

* @param key * @return */

public static boolean srem(String key, String member) { Jedis jedis = null; long num = 0; try { jedis = JedisPoolUtil.getJedis(); num = jedis.srem(key, member); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } if (num == 1) { return true; } else { return false; } }

/**

* redis操作list *

* @Description

* @author zhangxiang

* @date: 2017年11月24日 下午3:00:22 */

public static class redisLis { /** * 将一个或多个值插入到列表头部 * * @param key * @param list */ public static void lpush(String key, List list) { Jedis jedis = null; try { jedis = JedisPoolUtil.getJedis(); for (String string : list) { jedis.lpush(key, string); } } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } }

/**

* 将一个或多个值插入到列表底部 *

* @param key * @param list */

public static void rpush(String key, List list) { Jedis jedis = null; try { jedis = JedisPoolUtil.getJedis(); for (String string : list) {

}

}

} /**

jedis.rpush(key, string); }

} catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); }

* 获取列表长度 *

* @param key * @return */

public static long getListSize(String key) { Jedis jedis = null; long num = 0; try { jedis = JedisPoolUtil.getJedis(); num = jedis.llen(key); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.release(jedis); } return num; }

因篇幅问题不能全部显示,请点此查看更多更全内容