redis工具类
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisException;
import java.io.*;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class JedisUtils {
private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
private static String ADDR = "127.0.0.1";
private static String AUTH = "password";
private static int PORT = 6379;
private static int MAX_ACTIVE = 300;
private static int MAX_IDLE = 200;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
static {
try {
init();
} catch (Exception e) {
logger.error("初始化Redis出错," + e);
}
}
private synchronized static void init() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
config.setMaxTotal(MAX_ACTIVE);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
}
public static Object getObject(String key) {
Object value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
value = toObject(jedis.get(getBytesKey(key)));
}
} catch (Exception e) {
logger.info("getObject{} "+ key+"=" + value);
} finally {
returnResource(jedis);
}
return value;
}
public static String get(String key) {
String value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.get(key);
}
} catch (Exception e) {
logger.info("getObject{} "+ key+"=" + value);
} finally {
returnResource(jedis);
}
return value;
}
public static String setObject(String key, Object value, int cacheSeconds) {
String result = null;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.set(getBytesKey(key), toBytes(value));
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
} catch (Exception e) {
logger.info("getObject{} "+ key+"=" + value);
} finally {
returnResource(jedis);
}
return result;
}
public static List getObjectList(String key) {
List value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
List list = jedis.lrange(getBytesKey(key), 0, -1);
value = Lists.newArrayList();
for (byte[] bs : list){
value.add(toObject(bs));
}
}
} catch (Exception e) {
logger.info("getObjectList {} = {}", key, value);
} finally {
returnResource(jedis);
}
return value;
}
public static long setObjectList(String key, List value, int cacheSeconds) {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
jedis.del(key);
}
List list = Lists.newArrayList();
for (Object o : value){
list.add(toBytes(o));
}
for (int i = 0; i < value.size(); i++) {
result = jedis.rpush(getBytesKey(key), getBytesKey(value.get(i)));
}
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return result;
}
public static long listObjectAdd(String key, Object... value) {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
List list = Lists.newArrayList();
for (Object o : value){
result = jedis.rpush(getBytesKey(key),toBytes(o));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return result;
}
public static Set getObjectSet(String key) {
Set value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
value = Sets.newHashSet();
Set set = jedis.smembers(getBytesKey(key));
for (byte[] bs : set){
value.add(toObject(bs));
}
}
} catch (Exception e) {
logger.info("getObjectSet {} = {}", key, value);
} finally {
returnResource(jedis);
}
return value;
}
public static long setObjectSet(String key, Set value, int cacheSeconds) {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
jedis.del(key);
}
Set set = Sets.newHashSet();
for (Object o : value){
result = jedis.sadd(getBytesKey(key), toBytes(o));
}
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return result;
}
public static long setSetObjectAdd(String key, Object... value) {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
Set set = Sets.newHashSet();
for (Object o : value){
result = jedis.sadd(getBytesKey(key), toBytes(o));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return result;
}
public static Map getObjectMap(String key) {
Map value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = Maps.newHashMap();
Map map = jedis.hgetAll(getBytesKey(key));
Iterator> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Entry next = iterator.next();
value.put(new String(next.getKey()), toObject(next.getValue()));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return value;
}
public static String setObjectMap(String key, Map value, int cacheSeconds) {
String result = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
jedis.del(key);
}
Map map = Maps.newHashMap();
for (Map.Entry e : value.entrySet()){
map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
}
result = jedis.hmset(getBytesKey(key), (Map)map);
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
} catch (Exception e) {
logger.info("setObjectMap {} = {}", key, value);
} finally {
returnResource(jedis);
}
return result;
}
public static String mapObjectPut(String key, Map value) {
String result = null;
Jedis jedis = null;
try {
jedis = getResource();
Map map = Maps.newHashMap();
for (Map.Entry e : value.entrySet()){
map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
}
result = jedis.hmset(getBytesKey(key), (Map)map);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return result;
}
public static long mapObjectRemove(String key, String mapKey) {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.hdel(getBytesKey(key), getBytesKey(mapKey));
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return result;
}
public static boolean mapObjectExists(String key, String mapKey) {
boolean result = false;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey));
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return result;
}
public static long delObject(String key) {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))){
result = jedis.del(getBytesKey(key));
logger.debug("delObject {}", key);
}else{
logger.debug("delObject {} not exists", key);
}
} catch (Exception e) {
logger.error("delObject {}", key, e);
} finally {
returnResource(jedis);
}
return result;
}
public static boolean existsObject(String key) {
boolean result = false;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.exists(getBytesKey(key));
} catch (Exception e) {
logger.warn("existsObject {}", key, e);
} finally {
returnResource(jedis);
}
return result;
}
public static boolean setRedisLockNX(String key,String value,Integer cacheSeconds) {
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.set(key,value, "NX", "EX", cacheSeconds) == null) {
return false;
}else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
logger.info("setRedisLockNX {}", key, e);
} finally {
returnResource(jedis);
}
return false;
}
public static Long incrCount(String key) {
Jedis jedis = null;
Long incr = -1L;
try {
jedis = getResource();
incr = jedis.incr(getBytesKey(key));
} catch (Exception e) {
e.printStackTrace();
logger.info("setRedisLockNX {}", key, e);
} finally {
returnResource(jedis);
}
return incr;
}
/*===================================================================================================================================*/
public static Jedis getResource() throws JedisException {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
} catch (JedisException e) {
logger.warn("getResource.", e);
returnBrokenResource(jedis);
throw e;
}
return jedis;
}
/*===================================================================================================================================*/
public static void returnBrokenResource(Jedis jedis) {
if (jedis != null) {
jedisPool.returnBrokenResource(jedis);
}
}
public static void returnResource(Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
public static byte[] getBytesKey(Object object) throws UnsupportedEncodingException{
if(object instanceof String){
return ((String)object).getBytes("UTF-8");
}else{
return ObjectUtils.serialize(object);
}
}
public static byte[] toBytes(Object object){
return ObjectUtils.serialize(object);
}
public static Object toObject(byte[] bytes){
return ObjectUtils.unserialize(bytes);
}
public static Object dbSize(int dbindex) {
Jedis jedis = null;
Object result = null;
try {
jedis = getResource();
jedis.select(dbindex);
result = jedis.dbSize();
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return 0;
}
public static void set(byte[] serialize, byte[] serialize2,int dbindex) {
Jedis jedis = null;
try {
jedis = getResource();
jedis.select(dbindex);
jedis.set(serialize, serialize2);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
}
public static byte[] get(byte[] serialize, int dbindex) {
Jedis jedis = null;
try {
jedis = getResource();
jedis.select(dbindex);
return jedis.get(serialize);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
return null;
}
public static void expire(String key, Integer cacheSeconds) {
Jedis jedis = null;
try {
jedis = getResource();
jedis.expire(getBytesKey(key), cacheSeconds);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
}
public static void flushDB(int dbindex) {
Jedis jedis = null;
try {
jedis = getResource();
jedis.select(dbindex);
jedis.flushDB();
} catch (Exception e) {
e.printStackTrace();
} finally {
returnResource(jedis);
}
}
public static byte[] getBytes(String str){
if (str != null){
try {
return str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}else{
return null;
}
}
/** 布隆过滤器 **/
/**
* 根据索引从bitmap中获取值
* @param bitIndex bitset的索引值
* @return
*/
public static boolean getBloomFilterValue(String key,int bitIndex) {
Jedis jedis = null;
boolean flag = false;
try {
jedis = getResource();
flag = jedis.getbit(key, bitIndex);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != jedis) {
jedis.close();
}
}
return flag;
}
/**
* 在bitset中设置key和value
* @param bitIndex
* @param b
*/
public static void setBloomFilterKey(String key,int bitIndex, boolean b) {
Jedis jedis = null;
try {
jedis = getResource();
jedis.setbit(key, bitIndex, b);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != jedis) {
jedis.close();
}
}
}
}
class ObjectUtils{
public static void annotationToObject(Object annotation, Object object){
if (annotation != null){
Class> annotationClass = annotation.getClass();
Class> objectClass = object.getClass();
for (Method m : objectClass.getMethods()){
if (StringUtils.startsWith(m.getName(), "set")){
try {
String s = StringUtils.uncapitalize(StringUtils.substring(m.getName(), 3));
Object obj = annotationClass.getMethod(s).invoke(annotation);
if (obj != null && !"".equals(obj.toString())){
if (object == null){
object = objectClass.newInstance();
}
m.invoke(object, obj);
}
} catch (Exception e) {
// 忽略所有设置失败方法
}
}
}
}
}
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
if (object != null){
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
return baos.toByteArray();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
if (bytes != null && bytes.length > 0){
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
布隆过滤器实现
public class BloomFilterUtil {
// 布隆过滤器key1
private static final String USER_ID_BIT_SET = "user_id_strhash_bloomfilter";
// 初始化集合长度
private static final int length = Integer.MAX_VALUE;
// 准备hash计算次数
private static final int HASH_LENGTH = 5;
/**
* 准备自定义哈希算法需要用到的质数,因为一条数据需要hash计算5次 且5次的结果要不一样
*/
private static int[] primeNums = new int[] { 17, 19, 29, 31, 37 };
/**
* 添加元素到bitSet中
* @param key
*/
public static void addKey(String key) {
for (int i : primeNums) {
// 计算hashcode
int hashcode = hash(key, i);
// 计算映射在bitset上的位置
int bitIndex = hashcode & (length - 1);
JedisUtils.setBloomFilterKey(USER_ID_BIT_SET, bitIndex, true);
}
}
/**
* 判断bitSet中是否有被查询的的key(经过hash处理之后的)
* @param key
* @return
*/
public static boolean hasKey(String key) {
for (int i : primeNums) {
// 计算hashcode
int hashcode = hash(key, i);
// 计算映射在bitset上的位置
int bitIndex = hashcode & (length - 1);
// 只要有一个位置对应不上,则返回false
if (!JedisUtils.getBloomFilterValue(USER_ID_BIT_SET, bitIndex)) {
return false;
}
}
return true;
}
/**
* 自定义hash函数
* @param key
* @param prime
* @return
*/
private static int hash(String key,int prime) {
int h = 0;
char[] value = key.toCharArray();
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = prime * h + val[i];
}
}
return h;
}
}
测试
public static void main(String[] args) {
BloomFilterUtil.addKey("username");
BloomFilterUtil.addKey("username1");
System.out.println(BloomFilterUtil.hasKey("username"));//true
System.out.println(BloomFilterUtil.hasKey("username1"));//true
System.out.println(BloomFilterUtil.hasKey("username2"));//false
}