jediscluster工具类

来源:互联网 发布:单身约会软件 编辑:程序博客网 时间:2024/06/07 17:16
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.nio.charset.Charset;import java.util.Date;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import org.apache.commons.lang3.StringUtils;import org.apache.commons.lang3.time.DateFormatUtils;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;import redis.clients.jedis.JedisPoolConfig;public class JedisPoolUtil {private static int maxIdel =5; private static long maxWaitMillis=100000;private static int timeOut = 3600000;private static String redisServer ="182.180.115.24:7000,182.180.115.24:7001,182.180.113.80:7002,182.180.113.80:7003,182.180.115.125:7004,182.180.115.125:7005";private static boolean testOnBorrow =true;private static JedisPoolUtil jedisUtil=null; private Integer seconds; JedisCluster jc; public JedisCluster getJc() { return jc; } public JedisPoolUtil(GenericObjectPoolConfig genericObjectPoolConfig, String redisServer, Integer timeout) { Set jedisClusterNodes = new HashSet(); //Jedis Cluster will attempt to discover cluster nodes automatically String[] redisServerArr = redisServer.split(","); for (int i = 0; i < redisServerArr.length; i++) { String[] ipPort = redisServerArr[i].split(":"); jedisClusterNodes.add(new HostAndPort(ipPort[0], Integer.parseInt(ipPort[1]))); //root 6tfc%RDX || 99 } jc = new JedisCluster(jedisClusterNodes, timeout, genericObjectPoolConfig); } public void set(String key, Serializable value) throws IOException { jc.setex(key.getBytes(Charset.forName("UTF-8")), seconds, toByteArray(value)); } public void set(String key, Integer seconds, Serializable value) throws IOException { jc.setex(key.getBytes(Charset.forName("UTF-8")), seconds, toByteArray(value)); } public Serializable get(String key) throws IOException, ClassNotFoundException { return toObject(jc.get(key.getBytes(Charset.forName("UTF-8")))); } public void setStr(String key, String value) throws IOException { jc.set(key, value); } public void setStr(String key, int seconds, String value) throws IOException { jc.setex(key, seconds, value); } public String getStr(String key) throws IOException, ClassNotFoundException { return jc.get(key); } public Serializable getByUTF8(String key) throws Exception { Object obj = new Object(); obj = toObject(jc.get(key.getBytes(Charset.forName("UTF-8")))); return (Serializable) obj; } public Long remove(String key) throws IOException, ClassNotFoundException { return jc.del(key.getBytes(Charset.forName("UTF-8"))); } public void reName(String oldkey, String newKey) throws IOException, ClassNotFoundException { jc.rename(oldkey, newKey); } public void setSeconds(Integer seconds) { this.seconds = seconds; } /** * 设置hash缓存,默认30分钟过期 * * @param key * @param hash * @throws Exception */ public void hmset(String key, Map hash) throws Exception { // 不设置过期时间 hmset(key, 0, hash); } /** * 设置hash缓存 * * @param key * @param seconds * 过期时间 * @param hash * @throws Exception */ public void hmset(String key, int seconds, Map hash) throws Exception { boolean seted = false; if (seconds > 0) { seted = true; } else { Set set = jc.hkeys(key); if (null != set && set.size() > 0) { seted = true; } } if (seted) { // 移除空值 HashMap name = new HashMap(); for (Entry entry : hash.entrySet()) { if (!StringUtils.isEmpty(entry.getValue())) { // hash.remove(entry.getKey()); name.put(entry.getKey(), entry.getValue()); } } if (!name.isEmpty()) { jc.hmset(key, name); } } if (seconds > 0) { Long exp = jc.ttl(key); if (exp.longValue() < 0) { jc.expire(key, seconds); } } } /** * 获取缓存hash值 * * @param key * @param fields * @return * @throws Exception */ public Map hmget(String key, String... fields) throws Exception { List values = jc.hmget(key, fields); Map hash = new HashMap(); for (int idx = 0; idx < fields.length; idx++) { hash.put(fields[idx], values.get(idx)); } return hash; } /** * 获取缓存hash值 * * @param key * @param field * @return * @throws Exception */ public String hmget(String key, String field) throws Exception { List values = jc.hmget(key, field); if (null != values && values.size() > 0) { return values.get(0); } else { return null; } } /** * 删除缓存hash值 * * @param key * @param fields * @throws Exception */ public void hdel(String key, String... fields) throws Exception { jc.hdel(key, fields); } /** * 锁缓存 * * @param key * @return * @throws Exception */ public boolean lock(String key) throws Exception { // 默认5分钟 return lock(key, 300); } /** * 锁缓存 * * @param key * @param seconds * @return * @throws Exception */ public boolean lock(String key, int seconds) throws Exception { String fmt = "yyyy-MM-dd HH:mm:ss.SSS"; String now = DateFormatUtils.format(new Date(), fmt); long locked = jc.setnx(key, now); // 获得锁 if (1 == locked) { Long exp = jc.ttl(key); if (exp.longValue() < 0) { jc.expire(key, seconds); } return true; } else {// 未获得锁 // 尝试再次获得锁 String lockTime = jc.get(key); // 如果在获得锁时间期间,其他任务释放了锁 if (StringUtils.isNotBlank(lockTime)) { now = DateFormatUtils.format(new Date(), fmt); locked = jc.setnx(key, now); // 获得锁 if (1 == locked) { Long exp = jc.ttl(key); if (exp.longValue() < 0) { jc.expire(key, seconds); } return true; } else {// 未获得锁 return false; } } else {// 未获得锁 return false; } } } /** * 释放锁 * * @param key * @return * @throws Exception */ public long unlock(String key) throws Exception { return jc.del(key); } /** * 将bytes[]输在还原为Object对象 * * @param bytes * @return * @throws IOException * @throws ClassNotFoundException */ public Serializable toObject(byte[] bytes) throws IOException, ClassNotFoundException { if (bytes == null) { return null; } Serializable obj = null; try { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); obj = (Serializable) ois.readObject(); ois.close(); bis.close(); } catch (IOException e) { throw e; } catch (ClassNotFoundException e) { throw e; } return obj; } /** * 将Object转化成byte[] * * @param obj * @return * @throws IOException */ public byte[] toByteArray(Object obj) throws IOException { if (obj == null) { return null; } byte[] bytes = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); bytes = bos.toByteArray(); oos.close(); bos.close(); } catch (IOException e) { throw e; } return bytes; } public static JedisPoolUtil getPool() {if (jedisUtil == null) {JedisPoolConfig config = new JedisPoolConfig();/** * 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例 */config.setMaxIdle(maxIdel);/** * 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间, * 则直接抛出JedisConnectionException */config.setMaxWaitMillis(maxWaitMillis);/** * 在borrow一个jedis实例时,是否提前进行validate操作;如果为true, 则得到的jedis实例均是可用的 */config.setTestOnBorrow(testOnBorrow);jedisUtil = new JedisPoolUtil(config,redisServer,timeOut);}return jedisUtil;} /** * redis 实现 string * @param key * @param value * @param seconds */public static void setStringVal(String key,String value,Integer seconds){jedisUtil = getPool();if (jedisUtil!=null){JedisCluster jc =jedisUtil.getJc();jc.del(key);jc.set(key, value);if(seconds!=null){jc.expire(key, seconds);}}}/** * reids取string * @param key * @return */public static String getStringVal(String key){String val="";jedisUtil = getPool();if (jedisUtil!=null){JedisCluster jc =jedisUtil.getJc();val = jc.get(key);}return val;} }