Redis sort命令

http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135921.html

1.添加 投票选项到 redis的  List 和HashMap

list    key  ==> tomas:vote:vote_item:%S:list   (PS. %S=voteId)

value==> {video_hashmap_key

map  key  ==> tomas:vote:vote_item:%S:hashMap  (PS. %S=voteId)

         value==> {{id=01;name=001,title=0002,投票数=33.....},{.....}}  

 

Map<String, String> map = new HashMap<String, String>();
//Long size = voteService.getCountNewYearVideos(null, voteId);
map.put("ac", ac);
map.put("vid", "***");
map.put("title", content.getTitle());
map.put("time", content.getSortDate().getTime());
map.put("conver", content.getTitleImg());
map.put("up_id", content.getUser().getId());
map.put("up_name", content.getUsername());
map.put("up_icon", content.getUser().getUserImg());
map.put("voteCount", "0");
//添加视频 也就是投票选项
Long result = voteService.addVoteItem(null, voteId, map);
/**
* 添加视频
*
* @param jedis
* @param voteId 活动id
* @param params ac : acId vid: vid name:title cover:封面 up:uperName
* @return int 添加完成视频总数
*/
@Override
public Long addVoteItem(Jedis jedis, Integer voteId, Map<String, String> params) {

String video_list_key = this.getFormatKeyStr(ALL_VIDEO_LIST, voteId);
String video_hashmap_key = this.getFormatKeyStr(NEW_YEAR_VIDEO_INFO_MAP, voteId, params.get("ac"));
if (this.isExistInVoteItem(jedis, voteId, params.get("ac"))) {
return 0L;
}
if (StringUtils.isEmpty(params.get("vid"))) {
        jedis.incr(getFormatKeyStr(ALL_VIDEO_LIST_INDEX, voteId));   //自增id
}
//添加视频信息到分页list lrange(用) 并且吧map的key当作list的vlaue分页是查询出list
Long all_size = jedis.lpush(video_list_key, video_hashmap_key);
//添加视频信息到Hashmap
jedis.hmset(video_hashmap_key, params);
//设置key过期时间
jedis.expire(video_hashmap_key, DEFAULT_COOLDOWN);
//设置key过期时间
jedis.expire(video_list_key, DEFAULT_COOLDOWN);
return all_size;
}

 

/**
* 获取视频分页视频
*
* @param jedis
* @param voteId 活动id
* @param userId 用户id
* @param pageSize 分页大小
* @param pageNo 第几页
* @param order 排序字段
* @param asc 是否升序
* @return List<Map<String, String>> 视频信息
*/
public List<Map<String, String>> getShapeShiftVideosPage(Jedis jedis, Integer voteId, String userId, int pageSize,
int pageNo, String order, boolean asc) {
String video_list_key = this.getFormatKeyStr(ALL_VIDEO_LIST, voteId);
List<Map<String, String>> rs = new ArrayList<Map<String, String>>();
List<String> result = new ArrayList<>();
System.out.println(getStartIndex(pageNo, pageSize) + "------" + getEndIndex(pageNo, pageSize));
if (asc) {
result = jedis.sort(video_list_key, new SortingParams().by("*->" + order).asc().limit(getStartIndex(pageNo, pageSize), pageSize).
get("*->ac", "*->vid", "*->title", "*->time", "*->conver", "*->up_id", "*->up_name", "*->up_icon", "*->voteCount"));
} else {
result = jedis.sort(video_list_key, new SortingParams().by("*->" + order).desc().limit(getStartIndex(pageNo, pageSize), pageSize).
get("*->ac", "*->vid", "*->title", "*->time", "*->conver", "*->up_id", "*->up_name", "*->up_icon", "*->voteCount"));
}
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < result.size(); i = i + 9) {
if ((i + 1) % 9 == 1) {
map = new HashMap<String, String>();
}
map.put("ac", result.get(i));
map.put("vid", result.get(i + 1));
map.put("title", result.get(i + 2));
map.put("time", result.get(i + 3));
map.put("conver", result.get(i + 4));
map.put("up_id", result.get(i + 5));
map.put("up_name", result.get(i + 6));
map.put("up_icon", result.get(i + 7));
map.put("voteCount", result.get(i + 8));
//检测用户对此item今天是否已经投票
if (!StringUtils.isEmpty(userId) && isVoteTodayByItem(jedis, voteId, result.get(i), userId)) {
map.put("isVote", "1");
} else {
map.put("isVote", "0");
}
rs.add(map);
}
//System.out.println(JSON.toJSON(rs));
return rs;
}
posted @ 2016-04-20 18:33  托马斯布莱克  阅读(327)  评论(0编辑  收藏  举报