Commit 4bcd7413 authored by maliang's avatar maliang

权限控制

parent d385cb8f
......@@ -138,6 +138,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.10.2</version>
</dependency>
</dependencies>
......
......@@ -24,6 +24,6 @@ public interface AdvertDao {
@Select("select *,advertiser_id advertiserId,advertiser_name,total_income totalIncome,income_seven incomeSeven,income_thirty incomeThirty,exposure_view exposureView,click_view clickView from advertiser_data_count t where advertiser_id={#advertiserId}")
List<AdvertiserData> getAdvertiserDataById(@Param("advertiserId") String advertiserId);
@Select("select minutes,sum(exposure) exposure,sum(click) click from advertiser_data_runtime t where advertiser_id={#advertiserId} and report_date<={#day} and report_date<={#day} group by minutes")
@Select("select minutes,sum(exposure) exposure,sum(click) click from advertiser_data_minutes t where advertiser_id={#advertiserId} and report_date<={#day} and report_date<={#day} group by minutes")
List<AdvertiserData> getAdvertExCl(@Param("advertiserId") String advertiserId,@Param("day") String day);
}
package com.boot.security.server.utils.redis;
public class CacheCaller<T> {
public T call() {
return null;
}
public T call(int index) {
return null;
}
}
package com.boot.security.server.utils.redis;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.*;
import java.util.*;
import java.util.Map.Entry;
public class JedisHelper {
//private static Cache jobCache = CacheManager.getInstance().getCache("redis_job_list");
private static int sleepTime = 300; // 默认sleep时间 300毫秒
private static int sleepMaxCount = 3; // 默认等待次数
public static String zMinScore = "-inf"; // 有序集(Sorted set) 的score的最小值
public static String zMaxScore = "+inf"; // 有序集(Sorted set) 的score的最大值
private static JedisManager jm = JedisManager.getInstance();
// public static void addjob(String k) {
// Element e = new Element(k, k);
// jobCache.put(e);
// }
//
// public static void removeJob(String k) {
// jobCache.remove(k);
// }
//
// public static boolean checkkey(String k) {
// Element e = jobCache.get(k);
// if (null != e) {
// return true;
// } else {
// return false;
// }
// }
public static Long hincrBy(String key, String field, long increment) {
ShardedJedis jedis = null;
Long value = null;
try {
jedis = jm.getShardedJedis();
value = jedis.hincrBy(key, field, increment);
jm.returnSharedJedis(jedis);
} catch (Exception var8) {
//this.logger.error("JedisHelper.", var8);
jm.returnBrokenSharedJedis(jedis);
}
return value;
}
public static void set(String key, Object o) {
String json = JSON.toJSONString(o);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.set(key, json);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
}
public static void set(String key, int expires, Object o) {
String json = JSON.toJSONString(o);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Transaction t = jedis.getShard(key).multi();
t.set(key, json);
t.expire(key, expires);
t.exec();
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
}
public static int setnx(String key, Object o) {
ShardedJedis jedis = null;
String value = JSON.toJSONString(o);
Long result = 0L;
try {
jedis = jm.getShardedJedis();
result = jedis.setnx(key, value);
jm.returnSharedJedis(jedis);
return result.intValue();
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
return 0;
}
}
public static int setnx(String key, int expires, Object o) {
ShardedJedis jedis = null;
String value = JSON.toJSONString(o);
Long result = 0L;
try {
jedis = jm.getShardedJedis();
Transaction t = jedis.getShard(key).multi();
Response<Long> response = t.setnx(key, value);
t.expire(key, expires);
t.exec();
if (response != null) {
result = response.get();
}
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
return 0;
}
if (result == null) {
return 0;
}
return result.intValue();
}
public static <T> T getSet(Class<T> clazz, String key, Object o) {
ShardedJedis jedis = null;
try {
String value = null;
if (!(o instanceof String)) {
value = JSON.toJSONString(o);
} else {
value = (String) o;
}
jedis = jm.getShardedJedis();
String json = jedis.getSet(key, value);
jm.returnSharedJedis(jedis);
if (StringUtils.isNotEmpty(json)) {
T t = JSON.parseObject(json, clazz);
return t;
} else {
return null;
}
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
return null;
}
}
// public static void loadDataToRedis(final String lockKey, final RedisLoader loader) {
// ThreadPoolManager.es.execute(new Runnable() {
// @Override
// public void run() {
// doJobWithRedisDistributedLock(lockKey, loader);
// }
// });
// }
public static void doJobWithRedisDistributedLock(final String lockKey, final RedisLoader loader) {
try {
int lock = 0;
int timeout = 10000;
long timeStamp = getCurrentTime() + timeout + 1;
lock = setnx(lockKey, timeout, timeStamp);
if (lock == 1) {
loader.doJob();
if (getCurrentTime() < get(Long.class, lockKey)) {
del(lockKey);
}
return;
} else {
Long expireTime = get(Long.class, lockKey);
expireTime = expireTime == null ? 0 : expireTime;
if (getCurrentTime() > expireTime) {
Long valueBeforeSet = getSet(Long.class, lockKey, timeStamp);
valueBeforeSet = valueBeforeSet == null ? 0 : valueBeforeSet;
if (getCurrentTime() > valueBeforeSet) {
loader.doJob();
if (getCurrentTime() < get(Long.class, lockKey)) {
del(lockKey);
}
return;
}
}
}
} catch (Exception e) {
//ignore
}
}
private static long getCurrentTime() {
return System.currentTimeMillis();
}
public static boolean exists(String key) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
boolean result = jedis.exists(key);
jm.returnSharedJedis(jedis);
return result;
} catch (Exception e) {
//
System.out.println(e.getStackTrace());
jm.returnBrokenSharedJedis(jedis);
return false;
}
}
/**
* 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
* @param key KEY值
* @param seconds 过期时间,单位:秒
*/
public static void expire(String key, int seconds) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.expire(key, seconds);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
}
/**
* 为给定 key 获取生存时间
* @param key KEY值
*/
public static long ttl(String key) {
Long ret = 0l;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
ret = jedis.ttl(key);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
return ret;
}
public static <T> T get(Class<T> clazz, String key) {
String json = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
json = jedis.get(key);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
T t = JSON.parseObject(json, clazz);
return t;
}
public static <T> List<T> getList(Class<T> clazz, String key) {
String json = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
json = jedis.get(key);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
List<T> list = JSON.parseArray(json, clazz);
return list;
}
public static Long incr(String key) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Long l = jedis.incr(key);
jm.returnSharedJedis(jedis);
return l;
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
return 0L;
}
}
public static Long incrby(String key, int increment) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Long l = jedis.incrBy(key, increment);
jm.returnSharedJedis(jedis);
return l;
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
return 0L;
}
}
public static Long incrby(String key, int increment, int expireSecond) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Long l = jedis.incrBy(key, increment);
jedis.expire(key, expireSecond);
jm.returnSharedJedis(jedis);
return l;
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
return 0L;
}
}
public static Long decr(String key) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Long l = jedis.decr(key);
jm.returnSharedJedis(jedis);
return l;
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
return 0L;
}
}
public static Long decrby(String key, int increment) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Long l = jedis.decrBy(key, increment);
jm.returnSharedJedis(jedis);
return l;
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
return 0L;
}
}
public static void del(String key) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.del(key);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
}
public static void hdel(String key, String... fields) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.hdel(key, fields);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
}
public static void hset(String key, String field, Object o) {
String json = JSON.toJSONString(o);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.hset(key, field, json);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
}
public static void hset(String key, String field, int expire, Object o) {
String json = JSON.toJSONString(o);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Transaction t = jedis.getShard(key).multi();
t.hset(key, field, json);
t.expire(key, expire);
t.exec();
jm.returnSharedJedis(jedis);
} catch (Exception e) {
//
jm.returnBrokenSharedJedis(jedis);
}
}
public static <T> T hget(Class<T> clazz, String key, String field) {
String json = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
json = jedis.hget(key, field);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
T t = JSON.parseObject(json, clazz);
return t;
}
public static <T> Map<String, T> hgetAll(Class<T> valueClazz, String key) {
Map<String, String> json = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
json = jedis.hgetAll(key);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
Map<String, T> map = new HashMap<String, T>();
for (Entry<String, String> item : json.entrySet()) {
T t = JSON.parseObject(item.getValue(), valueClazz);
map.put(item.getKey(), t);
}
return map;
}
public static void lpush(String key, int expires, Object... os) {
String[] jsons = new String[os.length];
for (int i = 0; i < jsons.length; i++) {
jsons[i] = JSON.toJSONString(os[i]);
}
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Transaction t = jedis.getShard(key).multi();
for (String json : jsons) {
t.lpush(key, json);
}
t.expire(key, expires);
t.exec();
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static void rpush(String key, Object... os) {
String[] jsons = new String[os.length];
for (int i = 0; i < jsons.length; i++) {
jsons[i] = JSON.toJSONString(os[i]);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.rpush(key, jsons[i]);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
}
public static void rpush(String key, List<? extends Object> list) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
for (Object obj : list) {
jedis.rpush(key, obj.toString());
}
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static void lrem(String key, long count, Object value) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
String v = JSON.toJSONString(value);
jedis.lrem(key, count, v);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static long llen(String key) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
long length = jedis.llen(key);
jm.returnSharedJedis(jedis);
return length;
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
return 0L;
}
}
public static void ltrim(String key, long start, long end) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.ltrim(key, start, end);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static <T> List<T> lrange(Class<T> clazz, String key, long start, long end) {
List<T> list = new ArrayList<T>();
List<String> sList = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
sList = jedis.lrange(key, start, end);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
if (sList.isEmpty()) {
for (String str : sList) {
T t = JSON.parseObject(str, clazz);
list.add(t);
}
}
return list;
}
public static void lset(String key, long index, Object value) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.lset(key, index, JSON.toJSONString(value));
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static <T> T lindex(Class<T> clazz, String key, int index) {
ShardedJedis jedis = null;
String str = null;
try {
jedis = jm.getShardedJedis();
str = jedis.lindex(key, index);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
return JSON.parseObject(str, clazz);
}
public static int zcount(String key) {
int count = 0;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
count = jedis.zcount(key, zMinScore, zMaxScore).intValue();
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
return count;
}
public static void zadd(String key, Map<? extends Object, Double> scoreObjects) {
// List<Map<Double, String>> list = new ArrayList<Map<Double, String>>();
// for (Entry<? extends Object, Double> enrty : scoreObjects.entrySet()) {
// String json = JSON.toJSONString(enrty.getKey());
// Double score = enrty.getValue();
//
// boolean isAdd = false;
// int size = list.size();
// int index = 0;
// while (index < size) {
// Map<Double, String> scoreMembers = list.get(index);
// if (scoreMembers.containsKey(score)) {
// index++;
// } else {
// scoreMembers.put(score, json);
// isAdd = true;
// break;
// }
// }
// if (!isAdd) {
// Map<Double, String> scoreMembers = new HashMap<Double, String>();
// scoreMembers.put(score, json);
// list.add(scoreMembers);
// }
// }
//
// ShardedJedis jedis = null;
// try {
// for (Map<Double, String> scoreMembers : list) {
// jedis = jm.getShardedJedis();
// jedis.zadd(key, scoreMembers);
// }
//
// jm.returnSharedJedis(jedis);
// } catch (Exception e) {
//
// jm.returnBrokenSharedJedis(jedis);
// }
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Transaction t = jedis.getShard(key).multi();
for (Entry<? extends Object, Double> enrty : scoreObjects.entrySet()) {
String json = JSON.toJSONString(enrty.getKey());
Double score = enrty.getValue();
t.zadd(key, score, json);
}
t.exec();
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static void zadd(String key, int expire, Map<? extends Object, Double> scoreObjects) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Transaction t = jedis.getShard(key).multi();
for (Entry<? extends Object, Double> enrty : scoreObjects.entrySet()) {
String json = JSON.toJSONString(enrty.getKey());
Double score = enrty.getValue();
t.zadd(key, score, json);
}
t.expire(key, expire);
t.exec();
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static void zadd(String key, int expire, double score, Object o) {
String json = JSON.toJSONString(o);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.zadd(key, score, json);
Transaction t = jedis.getShard(key).multi();
t.zadd(key, score, json);
t.expire(key, expire);
t.exec();
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static void zadd(String key, double score, Object o) {
String json = JSON.toJSONString(o);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.zadd(key, score, json);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static long zcard(String key) {
long count = 0;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
count = jedis.zcard(key);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
return count;
}
public static long zcount(String key, String min, String max) {
long count = 0;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
count = jedis.zcount(key, min, max);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
return count;
}
public static void zrem(String key, Object o) {
String json = JSON.toJSONString(o);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.zrem(key, json);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static void zremrangebyrank(String key, int start, int end) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.zremrangeByRank(key, start, end);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
/**
* 用于判断member是否在zset列表中
* @param key zset缓存key值
* @param member zset列表中某条记录的索引
* @return true存在;false不存在
* @author lf
*/
public static boolean isMemberExists(String key, String member) {
Double value = zscore(key, member);
if (value == null) {
return false;
}
return value > 0;
}
public static Double zscore(String key, String member) {
ShardedJedis jedis = null;
Double score = null;
try {
jedis = jm.getShardedJedis();
score = jedis.zscore(key, member);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
return score;
}
public static void zincrby(String key, double score, Object o) {
String json = JSON.toJSONString(o);
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.zincrby(key, score, json);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
public static <T> Map<T, Double> zrevrange(Class<T> clazz, String key, long start, long end) {
Map<T, Double> map = new LinkedHashMap<T, Double>();
Set<Tuple> set = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
set = jedis.zrevrangeWithScores(key, start, end);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
return null;
}
for (Tuple tuple : set) {
T t = JSON.parseObject(tuple.getElement(), clazz);
Double score = tuple.getScore();
map.put(t, score);
}
return map;
}
public static <T> Map<T, Double> zrange(Class<T> clazz, String key, long start, long end) {
Map<T, Double> map = new LinkedHashMap<T, Double>();
Set<Tuple> set = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
set = jedis.zrangeWithScores(key, start, end);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
return null;
}
for (Tuple tuple : set) {
T t = JSON.parseObject(tuple.getElement(), clazz);
Double score = tuple.getScore();
map.put(t, score);
}
return map;
}
private static <T> Map<T, Double> zrevrangeByScoreCommon(Class<T> clazz, String key, Object maxScore,
Object minScore, int offset, int count) {
Map<T, Double> map = new LinkedHashMap<T, Double>();
Set<Tuple> set = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
if (maxScore.getClass().equals(Long.class) && minScore.getClass().equals(Long.class)) {
set = jedis.zrevrangeByScoreWithScores(key, Long.valueOf(maxScore.toString()), Long.valueOf(minScore
.toString()), offset, count);
} else if (maxScore.getClass().equals(Double.class) && minScore.getClass().equals(Double.class)) {
set = jedis.zrevrangeByScoreWithScores(key, Double.valueOf(maxScore.toString()), Double
.valueOf(minScore.toString()), offset, count);
} else if (maxScore.getClass().equals(String.class) && minScore.getClass().equals(String.class)) {
set = jedis.zrevrangeByScoreWithScores(key, maxScore.toString(), minScore.toString(), offset, count);
}
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
return null;
}
for (Tuple tuple : set) {
T t = JSON.parseObject(tuple.getElement(), clazz);
Double score = tuple.getScore();
map.put(t, score);
}
return map;
}
public static <T> Map<T, Double> zrevrangeByScore(Class<T> clazz, String key, String max, String min, int offset,
int count) {
return zrevrangeByScoreCommon(clazz, key, max, min, offset, count);
}
public static <T> Map<T, Double> zrevrangeByScore(Class<T> clazz, String key, double max, double min, int offset,
int count) {
return zrevrangeByScoreCommon(clazz, key, max, min, offset, count);
}
public static <T> Map<T, Double> zrevrangeByScore(Class<T> clazz, String key, long max, long min, int offset,
int count) {
return zrevrangeByScoreCommon(clazz, key, max, min, offset, count);
}
private static <T> Map<T, Double> zrevrangeByScoreCommon(Class<T> clazz, String key, Object maxScore,
Object minScore) {
Map<T, Double> map = new LinkedHashMap<T, Double>();
Set<Tuple> set = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
if (maxScore.getClass().equals(Long.class) && minScore.getClass().equals(Long.class)) {
set = jedis.zrevrangeByScoreWithScores(key, Long.valueOf(maxScore.toString()), Long.valueOf(minScore
.toString()));
} else if (maxScore.getClass().equals(Double.class) && minScore.getClass().equals(Double.class)) {
set = jedis.zrevrangeByScoreWithScores(key, Double.valueOf(maxScore.toString()), Double
.valueOf(minScore.toString()));
} else if (maxScore.getClass().equals(String.class) && minScore.getClass().equals(String.class)) {
set = jedis.zrevrangeByScoreWithScores(key, maxScore.toString(), minScore.toString());
}
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
return null;
}
for (Tuple tuple : set) {
T t = JSON.parseObject(tuple.getElement(), clazz);
Double score = tuple.getScore();
map.put(t, score);
}
return map;
}
public static <T> Map<T, Double> zrevrangeByScore(Class<T> clazz, String key, String max, String min) {
return zrevrangeByScoreCommon(clazz, key, max, min);
}
public static <T> Map<T, Double> zrevrangeByScore(Class<T> clazz, String key, double max, double min) {
return zrevrangeByScoreCommon(clazz, key, max, min);
}
public static <T> Map<T, Double> zrevrangeByScore(Class<T> clazz, String key, long max, long min) {
return zrevrangeByScoreCommon(clazz, key, max, min);
}
/**
* 移除有序集 key 中,指定排名(rank)区间内的所有成员<br>
* 区间分别以下标参数 start 和 end 指出,包含 start 和 end 在内
* @param key key值
* @param start 起始下标
* @param end 结束下标
*/
public static void zremrangeByScore(String key, double start, double end) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
jedis.zremrangeByScore(key, start, end);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
private static <T> Map<T, Double> zrangeByScoreCommon(Class<T> clazz, String key, Object maxScore, Object minScore,
int offset, int count) {
Map<T, Double> map = new LinkedHashMap<T, Double>();
Set<Tuple> set = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
if (maxScore.getClass().equals(Long.class) && minScore.getClass().equals(Long.class)) {
set = jedis.zrangeByScoreWithScores(key, Long.valueOf(minScore.toString()), Long.valueOf(maxScore
.toString()), offset, count);
} else if (maxScore.getClass().equals(Double.class) && minScore.getClass().equals(Double.class)) {
set = jedis.zrangeByScoreWithScores(key, Double.valueOf(minScore.toString()), Double.valueOf(maxScore
.toString()), offset, count);
} else if (maxScore.getClass().equals(String.class) && minScore.getClass().equals(String.class)) {
set = jedis.zrangeByScoreWithScores(key, minScore.toString(), maxScore.toString(), offset, count);
}
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
return null;
}
for (Tuple tuple : set) {
T t = JSON.parseObject(tuple.getElement(), clazz);
Double score = tuple.getScore();
map.put(t, score);
}
return map;
}
public static <T> Map<T, Double> zrangeByScore(Class<T> clazz, String key, String max, String min, int offset,
int count) {
return zrangeByScoreCommon(clazz, key, max, min, offset, count);
}
public static <T> Map<T, Double> zrangeByScore(Class<T> clazz, String key, double max, double min, int offset,
int count) {
return zrangeByScoreCommon(clazz, key, max, min, offset, count);
}
public static <T> Map<T, Double> zrangeByScore(Class<T> clazz, String key, long max, long min, int offset, int count) {
return zrangeByScoreCommon(clazz, key, max, min, offset, count);
}
public static Set<String> keys(String key) {
System.out.println("keys() start:" + new Date());
Set<String> result = new HashSet<String>();
if (StringUtils.isBlank(key)) {
return result;
}
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Iterator<Jedis> it = jedis.getAllShards().iterator();
while (it.hasNext()) {
Jedis j = it.next();
Set<String> r = j.keys(key);
if (r != null) {
result.addAll(r);
}
}
jm.returnSharedJedis(jedis);
} catch (Exception e) {
e.printStackTrace();
jm.returnBrokenSharedJedis(jedis);
}
System.out.println("keys end:" + new Date());
return result;
}
/** 页面缓存存取专用*/
public static void setPage(String key, int expires, String o) {
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
Transaction t = jedis.getShard(key).multi();
t.set(key, o);
t.expire(key, expires);
t.exec();
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
}
/** 页面缓存存取专用*/
public static String getPage(String key) {
String o = null;
ShardedJedis jedis = null;
try {
jedis = jm.getShardedJedis();
o = jedis.get(key);
jm.returnSharedJedis(jedis);
} catch (Exception e) {
jm.returnBrokenSharedJedis(jedis);
}
return o;
}
}
package com.boot.security.server.utils.redis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class JedisManager {
private ShardedJedisPool shardPool;
class JedisConfig {
private JedisConfig() {
initaize();
}
//private String path="config.properties";
private void initaize() {
System.out.println("redis initaizing ...");
//String path = SystemConfig.getConfig("core.properties.path");
Properties cbuilder = new Properties();
try {
cbuilder.load(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties") , "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
//ConfigBuilder cbuilder = ConfigBuilderFactory.getConfigBuilder(path);
JedisPoolConfig pConfig = new JedisPoolConfig();
pConfig.setMaxIdle(Integer.parseInt(cbuilder.getProperty("redis.maxIdle")));
pConfig.setMaxTotal(Integer.parseInt(cbuilder.getProperty("redis.maxActive")));
pConfig.setMinIdle(Integer.parseInt(cbuilder.getProperty("redis.minIdle")));
int nodes = Integer.parseInt(cbuilder.getProperty("redis.cluster.count"));
// 单位:毫秒
Integer poolTimeout = Integer.parseInt(cbuilder.getProperty("redis.pool.timeout"));
List<JedisShardInfo> jdinfoList = new ArrayList<JedisShardInfo>();
for (int i = 0; i < nodes; i++) {
String masterHost = cbuilder.getProperty("redis.cluster.master." + (i + 1) + ".host");
String masterPort = cbuilder.getProperty("redis.cluster.master." + (i + 1) + ".port");
String masterName = cbuilder.getProperty("redis.cluster.master." + (i + 1) + ".name", masterHost);
JedisShardInfo jsi = new JedisShardInfo(masterHost, Integer.valueOf(masterPort), poolTimeout, masterName);
jdinfoList.add(jsi);
}
shardPool = new ShardedJedisPool(pConfig, jdinfoList);
System.out.println("redis initialize success");
}
}
private JedisManager() {
new JedisConfig();
}
private static JedisManager instance = new JedisManager();
public static JedisManager getInstance() {
return instance;
}
public ShardedJedis getShardedJedis() {
return shardPool.getResource();
}
/**
* 正常归还jedis
*
* @param jedis
*/
public void returnSharedJedis(final ShardedJedis jedis) {
shardPool.returnResource(jedis);
}
/**
* jedis出现异常的时候销毁jedis
*
* @param jedis
*/
public void returnBrokenSharedJedis(final ShardedJedis jedis) {
shardPool.returnBrokenResource(jedis);
}
// /**
// * 根据key值获取写jedis
// *
// * @param key
// * @return
// */
// public Jedis getWriteJedis(final String key) {
// RNodeGroup[] nodes = RConfig.getNodes();
// int nodeIndex = key.hashCode() % nodes.length;
// if (nodeIndex < 0) {
// nodeIndex *= -1;
// }
//
// RNode node = nodes[nodeIndex].getMasterNode();
//
// try {
//
// return node.getjPool().getResource();
//
// } catch (JedisConnectionException jnex) {
//
// closeNode(node);
// node = nodes[nodeIndex].getMasterNode();
// return node.getjPool().getResource();
// }
//
// }
//
// /**
// * 根据key值获取jedis
// *
// * @param key
// * @return
// */
// public Jedis getReadJedis(final String key) {
//
// RNodeGroup[] nodes = RConfig.getNodes();
// int nodeIndex = key.hashCode() % nodes.length;
//
// if (nodeIndex < 0) {
// nodeIndex *= -1;
// }
//
// RNode node = nodes[nodeIndex].getReadNode();
// try {
//
// return node.getjPool().getResource();
//
// } catch (JedisConnectionException jnex) {
// closeNode(node);
// node = nodes[nodeIndex].getReadNode();
// return node.getjPool().getResource();
// }
//
// }
//
// /**
// * 正常归还jedis
// *
// * @param jedis
// */
// public void returnJedis(final Jedis jedis) {
//
// String nodeName = jedis.getClient().getHost() + ":" + jedis.getClient().getPort();
// RNode node = RConfig.getNodeMap().get(nodeName);
// if (node != null) {
// node.getjPool().returnResource(jedis);
// }
// }
//
// /**
// * jedis出现异常的时候销毁jedis
// *
// * @param jedis
// */
// public void returnBrokenJedis(final Jedis jedis) {
//
// String nodeName = jedis.getClient().getHost() + ":" + jedis.getClient().getPort();
// RNode node = RConfig.getNodeMap().get(nodeName);
// if (node != null) {
// node.getjPool().returnBrokenResource(jedis);
// }
// }
//
// /**
// * 无法连接的时候调用
// *
// * @param jedis
// */
// private void closeNode(final RNode rnode) {
//
// RNode masterNode = rnode.getGroup().getMasterNode();
//
// // 表示已经是同一个节点了
// if (rnode.getGroup().getSlaveNode() == rnode.getGroup().getMasterNode()) {
//
// // do nothing
// return;
// }
//
// // 是主还是从
// if (rnode == masterNode) {
//
// rnode.getGroup().getSlaveNode().getjPool().getResource().slaveofNoOne();
//
// rnode.getGroup().setMasterNode(rnode.getGroup().getSlaveNode());
//
// } else {
// rnode.getGroup().setSlaveNode(rnode.getGroup().getMasterNode());
// }
//
// RConfig.getNodeMap().remove(rnode.getNodeName());
//
// }
}
package com.boot.security.server.utils.redis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
class RConfig {
/** 配置文件 **/
private final String DEFAULT_FILE = "redis.properties";
/** 链接timeout时间 **/
private final int POOL_TIMEOUT = 1000;
private RNodeGroup[] nodes;
private final Map<String, RNode> nodeMap = new HashMap<String, RNode>();
private static RConfig config;
private static final Object mutex = new Object();
public static RNodeGroup[] getNodes() {
return getConfig().nodes;
}
public static Map<String, RNode> getNodeMap() {
return getConfig().nodeMap;
}
private static RConfig getConfig() {
if (config == null) {
synchronized (mutex) {
if (config == null) {
config = new RConfig();
}
return config;
}
}
return config;
}
private RConfig() {
initaize();
}
private void initaize() {
System.out.println("redis initaizing ...");
Properties redisProp = new Properties();
try {
redisProp.load(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties") , "UTF-8"));
Set<Object> propKeySet = redisProp.keySet();
for (Object obj : propKeySet) {
System.out.println("**************key:" + obj + ",value:" + redisProp.getProperty(obj.toString()));
}
JedisPoolConfig pConfig = new JedisPoolConfig();
pConfig.setMaxIdle(Integer.valueOf(redisProp.getProperty("redis.maxIdle")));
pConfig.setMaxTotal(Integer.valueOf(redisProp.getProperty("redis.maxActive")));
pConfig.setMinIdle(Integer.valueOf(redisProp.getProperty("redis.minIdle")));
nodes = new RNodeGroup[Integer.valueOf(redisProp.getProperty("redis.cluster.count"))];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = new RNodeGroup();
String masterHost = redisProp.getProperty("redis.cluster.master." + (i + 1) + ".host");
String masterPort = redisProp.getProperty("redis.cluster.master." + (i + 1) + ".port");
JedisPool mPoll = new JedisPool(pConfig, masterHost, Integer.valueOf(masterPort), POOL_TIMEOUT);
RNode masterNode = new RNode(masterHost + ":" + masterPort, mPoll);
masterNode.setGroup(nodes[i]);
String slaveHost = redisProp.getProperty("redis.cluster.slave." + (i + 1) + ".host");
String slavePort = redisProp.getProperty("redis.cluster.slave." + (i + 1) + ".port");
JedisPool sPoll = new JedisPool(pConfig, slaveHost, Integer.valueOf(slavePort), POOL_TIMEOUT);
RNode slaveNode = new RNode(slaveHost + ":" + slavePort, sPoll);
slaveNode.setGroup(nodes[i]);
nodes[i].setMasterNode(masterNode);
nodes[i].setSlaveNode(slaveNode);
nodeMap.put(masterNode.getNodeName(), masterNode);
nodeMap.put(slaveNode.getNodeName(), slaveNode);
}
} catch (FileNotFoundException e) {
//LogHelper.exceptionLog(e);
} catch (IOException e) {
//LogHelper.exceptionLog(e);
}
}
public static void main(String[] args) {
System.out.println(getNodes().length);
}
}
package com.boot.security.server.utils.redis;
import redis.clients.jedis.JedisPool;
public class RNode {
/** 格式host:port **/
private String nodeName;
private JedisPool jPool;
private RNodeGroup group;
public RNode(String nodeName, JedisPool jPool) {
this.nodeName = nodeName;
this.jPool = jPool;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public JedisPool getjPool() {
return jPool;
}
public void setjPool(JedisPool jPool) {
this.jPool = jPool;
}
public RNodeGroup getGroup() {
return group;
}
public void setGroup(RNodeGroup group) {
this.group = group;
}
}
package com.boot.security.server.utils.redis;
public class RNodeGroup {
/** master节点,单个节点只可能存在一个 **/
private RNode masterNode;
/** slave节点,单节点单个slave节点 **/
private RNode slaveNode;
/** 负载均衡 **/
int count;
/** 主从 读的权重 **/
private final int[] weight = {3, 7};
/** 锁 **/
Object mutex = new Object();
/**
* 根据权重获取读取节点
*
* @return
*/
public RNode getReadNode() {
synchronized (mutex) {
count++;
}
if (count >= 100000) {
count = 0;
}
// 去模权重的总和
int randomInt = count % 10;
return randomInt < weight[0] ? masterNode : slaveNode;
}
public RNode getMasterNode() {
return masterNode;
}
public void setMasterNode(RNode masterNode) {
this.masterNode = masterNode;
}
public RNode getSlaveNode() {
return slaveNode;
}
public void setSlaveNode(RNode slaveNode) {
this.slaveNode = slaveNode;
}
}
package com.boot.security.server.utils.redis;
public interface RedisLoader {
void doJob();
}
kafka.broker.list = bi-kafka01:6667,bi-kafka02:6667,bi-kafka03:6667
#kafka.broker.list = 172.17.255.142:9092,172.17.255.144:9092,172.17.255.149:9092
server.kafka.broker.list = 172.17.255.142:9092,172.17.255.144:9092,172.17.255.149:9092
cassandra.node.list = 172.17.255.192
cassandra.port = 9014
kafka.group.id=user_action_logs_test2
cassnadra.keyspace=dmp
## redis配置
#redis连接池最大空闲连接数
redis.maxIdle=20
redis.maxTotal=1000
#redis连接池最小空闲连接数
redis.minIdle=10
#redis连接池最大连接数
redis.maxActive=1000
#redis连接池连接超时时间
redis.pool.timeout=500
#redis集群服务数
redis.cluster.count=1
#redis集群服务地址
redis.cluster.master.1.host=172.17.255.136
redis.cluster.master.1.port=6379
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment