这两天折腾了下Redis,第一次用这玩意,参考网上的几个 Jedis工具类的例子,不过网上的几个比较复杂,啰嗦,大部分功能用不到。还是自己写的简单实用,特此记录。
JedisUtil.java
package com.fants.redis; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisUtil { protected Logger log = LoggerFactory.getLogger(getClass()); private static JedisPool jedisPool = null; /** 缓存生存时间 */ private final static int expire = 60000; static { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(JRedisPoolConfig.MAX_ACTIVE); config.setMaxIdle(JRedisPoolConfig.MAX_IDLE); config.setMaxWait(JRedisPoolConfig.MAX_WAIT); config.setTestOnBorrow(JRedisPoolConfig.TEST_ON_BORROW); config.setTestOnReturn(JRedisPoolConfig.TEST_ON_RETURN); // redis如果设置了密码: jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP, JRedisPoolConfig.REDIS_PORT, 10000, JRedisPoolConfig.REDIS_PASSWORD); } public static JedisPool getPool() { return jedisPool; } /** * 从jedis连接池中获取获取jedis对象 */ public static Jedis getJedis() { return jedisPool.getResource(); } /** * 回收jedis */ public static void returnJedis(Jedis jedis) { if(jedis!=null) jedisPool.returnResource(jedis); } /** * 设置过期时间 */ public static void expire(String key, int seconds) { if (seconds <= 0) { return; } Jedis jedis = getJedis(); jedis.expire(key, seconds); returnJedis(jedis); } /** * 设置默认过期时间 */ public static void expire(String key) { expire(key, expire); } public static void set(String key,String value){ if(isBlank(key)) return; Jedis jedis = getJedis(); jedis.set(key, value); returnJedis(jedis); } public static void set(String key,Object value){ if(isBlank(key)) return; Jedis jedis = getJedis(); jedis.set(key.getBytes(), SerializeUtil.serialize(value)); returnJedis(jedis); } public static void set(String key,int value){ if(isBlank(key)) return; set(key, String.valueOf(value)); } public static void set(String key,long value){ if(isBlank(key)) return; set(key, String.valueOf(value)); } public static void set(String key,float value){ if(isBlank(key)) return; set(key, String.valueOf(value)); } public static void set(String key,double value){ if(isBlank(key)) return; set(key, String.valueOf(value)); } public static Float getFloat(String key){ if(isBlank(key)) return null; return Float.valueOf(getStr(key)); } public static Double getDouble(String key){ if(isBlank(key)) return null; return Double.valueOf(getStr(key)); } public static Long getLong(String key){ if(isBlank(key)) return null; return Long.valueOf(getStr(key)); } public static Integer getInt(String key){ if(isBlank(key)) return null; return Integer.valueOf(getStr(key)); } public static String getStr(String key){ if(isBlank(key)) return null; Jedis jedis = getJedis(); String value = jedis.get(key); returnJedis(jedis); return value; } public static Object getObj(String key){ if(isBlank(key)) return null; Jedis jedis = getJedis(); byte[] bits = jedis.get(key.getBytes()); Object obj = SerializeUtil.unserialize(bits); returnJedis(jedis); return obj; } public static boolean isBlank(String str){ return str==null||"".equals(str.trim()); } }
JRedisPoolConfig.java
package com.fants.redis; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.jfinal.kit.PathKit; public class JRedisPoolConfig { protected static Logger log = LoggerFactory.getLogger(JRedisPoolConfig.class); public static String REDIS_IP; public static int REDIS_PORT; public static String REDIS_PASSWORD; public static int MAX_ACTIVE; public static int MAX_IDLE; public static long MAX_WAIT; public static boolean TEST_ON_BORROW; public static boolean TEST_ON_RETURN; static { init(); } public static void init() { try { String fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF" + File.separator + "redis.properties"; File file = new File(fullFile); if(file.exists()){ log.info("loading redis config from redis.properties......."); InputStream in = new FileInputStream(fullFile); Properties p = new Properties(); p.load(in); REDIS_IP = p.getProperty("redis.ip"); REDIS_PORT = Integer.parseInt(p.getProperty("redis.port")); REDIS_PASSWORD = p.getProperty("redis.password"); MAX_ACTIVE = Integer.parseInt(p.getProperty("redis.pool.maxActive")); MAX_IDLE = Integer.parseInt(p.getProperty("redis.pool.maxIdle")); MAX_WAIT = Integer.parseInt(p.getProperty("redis.pool.maxWait")); TEST_ON_BORROW = Boolean.parseBoolean(p.getProperty("redis.pool.testOnBorrow")); TEST_ON_RETURN = Boolean.parseBoolean(p.getProperty("redis.pool.testOnReturn")); log.info("redis config load Completed。"); in.close(); in=null; }else{ log.error("redis.properties is not found!"); } } catch (Exception e) { e.printStackTrace(); } } }
SerializeUtil.java
package com.fants.redis; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeUtil { /** * 序列化 * * @param object * @return */ public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 反序列化 * * @param bytes * @return */ public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return null; } }
redis.properties
#\u63A7\u5236\u4E00\u4E2Apool\u53EF\u5206\u914D\u591A\u5C11\u4E2Ajedis\u5B9E\u4F8B\uFF0C\u901A\u8FC7pool.getResource()\u6765\u83B7\u53D6\uFF1B\u5982\u679C\u8D4B\u503C\u4E3A-1\uFF0C\u5219\u8868\u793A\u4E0D\u9650\u5236\uFF1B\u5982\u679Cpool\u5DF2\u7ECF\u5206\u914D\u4E86maxActive\u4E2Ajedis\u5B9E\u4F8B\uFF0C\u5219\u6B64\u65F6pool\u7684\u72B6\u6001\u5C31\u6210exhausted\u4E86\uFF0C\u5728JedisPoolConfig redis.pool.maxActive=1024 #\u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle\u7684jedis\u5B9E\u4F8B\uFF1B redis.pool.maxIdle=200 #\u8868\u793A\u5F53borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B redis.pool.maxWait=1000 #\u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Calidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684\uFF1B redis.pool.testOnBorrow=true #\u5728return\u7ED9pool\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B redis.pool.testOnReturn=true #ip redis.ip=localhost #port redis.port=6379 #password redis.password=myredis