private static int RANK_CACHETTL = 3600*24*30; // RANK類緩存有效期1個(gè)月
private static final Log logger = LogFactory.getLog(RedisService.class);
@Autowired
private MFrameworkService mframeworkservice;
@Autowired
protected MBeanService mbeanservice;
// 保存redis記錄(當(dāng)數(shù)值是int類型時(shí))
public void saveRank(String key,int number,String userid){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
Long count = jedis.zadd(key, number, userid);
if(count!=null && count==1){ // 首次創(chuàng)建時(shí)讶泰,設(shè)置過期時(shí)間
jedis.expire(key, RANK_CACHETTL);
}
}
//保存redis記錄(當(dāng)數(shù)值是long類型時(shí))
public void saveRank(String key,long number,String userid){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
Long count = jedis.zadd(key, number, userid);
if(count!=null && count==1){ // 首次創(chuàng)建時(shí)竖独,設(shè)置過期時(shí)間
jedis.expire(key, RANK_CACHETTL);
}
}
//獲取當(dāng)前名次(第一名是0,以此類推)
public Long getRankNumber(String key,String userid){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
return jedis.zrevrank(key, userid);
}
//獲取當(dāng)前名次的百分比
public float getRankPercent(String key,String userid){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
Long rankNumber = getRankNumber(key, userid);
Long size = jedis.zcount(key, 0, 999999999);//獲取參與的總?cè)藬?shù)
// ?1-用戶當(dāng)前的排名/活動(dòng)參與總?cè)藬?shù)=當(dāng)前用戶所占的百分比
logger.info("RedisRankService,rankNumber:"+rankNumber+",size:"+size);
//排名為空,獲取的分?jǐn)?shù)不是空,同時(shí)size不是0(分母不能為0)
if(!StringUtils.isEmpty(rankNumber) && !StringUtils.isEmpty(size)&&size!=0)
return Float.parseFloat(String.valueOf(rankNumber))/Float.parseFloat(String.valueOf(size));
return 0f;
}
//獲取分?jǐn)?shù)
public Double getRankValue(String key,String userid){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
Double value = jedis.zscore(key, userid);
return (StringUtils.isEmpty(value)||value==null)?0d:value;
}
//更新排行榜中某一條數(shù)據(jù)并返回更新后的值
public Double refreshRankValue(String key,int number,String userid){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
Double value = jedis.zincrby(key, number, userid);
return (StringUtils.isEmpty(value)||value==null)?0d:value;
}
//獲取排行榜public JSONArray getRank(String key,int RankNumber,int orderby){
JSONArray josna = new JSONArray();
JSONObject json = null;RankVo vo = null;
JedisCluster jedis =? RedisClusterCache.getJedisCluster();Setfset;
if(orderby==0)//正序
fset= jedis.zrangeWithScores(key, 0, RankNumber-1);
else//倒敘
fset= jedis.zrevrangeWithScores(key, 0, RankNumber-1);
for (Tuple s : fset) {
String userid = s.getElement();
UserInfo userinfo = mframeworkservice.get(UserInfo.class, userid);
if(StringUtils.isEmpty(userinfo))
continue;
vo = RankVo.newInstance(RankVo.class);
vo.setAvatarurl(userinfo.getAvatarurl());
vo.setUserid(userid);
vo.setNickname(userinfo.getNickname());
vo.setMobile(userinfo.getMobile());
vo.setGames_total(CommonUtils.get2Float(s.getScore()/100f));
json = JSONBeanUtils.getBeanJSONObject(BeanInfo.getBeanInfo(RankVo.class), vo);
josna.put(json);
}
return josna;
}
//獲取幫我支持的列表public JSONArray getShareRecord(String key,int RankNumber,int orderby){
JSONArray josna = new JSONArray();
JSONObject json = null;RankVo vo = null;
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
Setfset;
if(orderby==0)//正序
fset= jedis.zrangeWithScores(key, 0, RankNumber-1);
else//倒敘
fset= jedis.zrevrangeWithScores(key, 0, RankNumber-1);
for (Tuple s : fset) {
String userid = s.getElement();
UserInfo userinfo = mframeworkservice.get(UserInfo.class, userid);
if(StringUtils.isEmpty(userinfo))
continue;
vo = RankVo.newInstance(RankVo.class);
vo.setAvatarurl(userinfo.getAvatarurl());
vo.setUserid(userid);
vo.setNickname(userinfo.getNickname());
vo.setMobile(userinfo.getMobile());
long time =new Double(s.getScore()).longValue();
vo.setCreatetime(new Date(time));
String total = jedis.get(key.substring(0,32)+time+s.getElement());
if(StringUtils.isEmpty(total)||"null".equals(total))
total = jedis.get(key.substring(0,32)+time+"");
if(StringUtils.isEmpty(total)||"null".equals(total))
total = jedis.get(time+"");
if(StringUtils.isEmpty(total)||"null".equals(total))
continue;
vo.setGames_total(CommonUtils.get2Float(Float.parseFloat(total)/100f));
json = JSONBeanUtils.getBeanJSONObject(BeanInfo.getBeanInfo(RankVo.class), vo);
josna.put(json);
}
return josna;
}
//獲取中獎(jiǎng)記錄列表public JSONArray getPrizeRecord(String key,int RankNumber,int orderby,BeanInfo beanInfo){
JSONArray josna = new JSONArray();
JSONObject json = null;PrizeRecordVo vo = null;if(key.length()<32)return josna;
JedisCluster jedis =? RedisClusterCache.getJedisCluster();Setfset;
if(orderby==0)//正序
fset= jedis.zrangeWithScores(key, 0, RankNumber-1);
else//倒敘
fset= jedis.zrevrangeWithScores(key, 0, RankNumber-1);
for (Tuple s : fset) {
String userid = s.getElement().substring(0,32);
UserInfo userinfo = mframeworkservice.get(UserInfo.class, userid);
if(StringUtils.isEmpty(userinfo))
continue;
vo = PrizeRecordVo.newInstance(PrizeRecordVo.class);
vo.setAvatarurl(userinfo.getAvatarurl());
vo.setUserid(userid);
vo.setNickname(userinfo.getNickname());
vo.setMobile(userinfo.getMobile());
long time =new Double(s.getScore()).longValue();
vo.setCreatetime(new Date(time));
String prizeid = jedis.get(key.substring(0,32)+time+s.getElement());
if(StringUtils.isEmpty(prizeid)||"null".equals(prizeid))
prizeid = jedis.get(time+"");
if(StringUtils.isEmpty(prizeid)||"null".equals(prizeid))
continue;
MBeanBase bean = mbeanservice.get(beanInfo, prizeid);
if(bean!=null){
vo.setPrize_title(bean.get("prize_title").toString());
vo.setPrize_imgurl(bean.get("prize_imgurl").toString());
}
json = JSONBeanUtils.getBeanJSONObject(BeanInfo.getBeanInfo(PrizeRecordVo.class), vo);
josna.put(json);
}
return josna;
}
//獲取redis記錄
public String getBykey(String key){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
return jedis.get(key);
}
//獲取redis記錄
public int getIntBykey(String key){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
return StringUtils.isEmpty(jedis.get(key))?0:Integer.parseInt(jedis.get(key));
}
// 保存redis記錄
// add by dbb, 過期時(shí)間:15日
public void saveByKey(String key,String value){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
// jedis.set(key, value);
jedis.setex(key, 3600*24*30, value);
}
//獲取當(dāng)前排行榜中一個(gè)區(qū)間范圍的人數(shù)
public int getRankCount(String key){
JedisCluster jedis =? RedisClusterCache.getJedisCluster();
Long size = jedis.zcount(key, 0, 9999999999999d);
return StringUtils.isEmpty(size)?0:size.intValue();
}