使用redis的有序集合zset实现排行榜功能,步骤有:
1、下载jedis-2.7.2.jar
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> <type>jar</type> <scope>compile</scope> </dependency>
2、下载jackson所需的jar包
使用jackson实现数据的序列化和反序列化
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.4</version> </dependency>
3、初始化JedisPool对象,并通过JedisPool创建Jedis对象
public class JedisHandler { private static JedisPool jedisPool = null; static { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(8); // maximum active connections poolConfig.setMaxIdle(100); // maximum idle connections jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379); } public static Jedis createJedis() { return jedisPool.getResource(); } }
4、创建数据模型对象
public class User { private String id; private String userName; private Integer gender; private String redisterDate; private Double score; /** 排名 */ private Integer rank; ... ... ... }
5、通过jedis添加模拟数据
public static void setData() { Jedis jedis = JedisHandler.createJedis(); User user = new User("100", "汤姆", 1, "2012-10-20"); jedis.zadd("user:score", 1000d, MarshalJSONUtil.marshalBeanToJson(user)); user = new User("101", "韩梅梅", 2, "2013-03-10"); jedis.zadd("user:score", 873.3, MarshalJSONUtil.marshalBeanToJson(user)); user = new User("102", "Jack", 1, "2008-11-10"); jedis.zadd("user:score", 1302.6d, MarshalJSONUtil.marshalBeanToJson(user)); user = new User("103", "Rose", 2, "2015-02-03"); jedis.zadd("user:score", 200d, MarshalJSONUtil.marshalBeanToJson(user)); user = new User("104", "李雷", 1, "2014-01-26"); jedis.zadd("user:info", 534.73d, MarshalJSONUtil.marshalBeanToJson(user)); Random random = new Random(); for(int i = 1; i < 101; i++) { int index = 104 + i; user = new User(index + "", "用户" + index, random.nextInt(1) + 1, "2015-01-26"); jedis.zadd("user:score", 100d + 1, MarshalJSONUtil.marshalBeanToJson(user)); } jedis.close(); }
6、通过jedis排序,筛选数据
/** * 获取降序排列的数据 * @return 排名后的用户集合 */ public static List<User> getRevRankData(int count) { Jedis jedis = JedisHandler.createJedis(); List<User> list = new ArrayList<User>(); try { Set<Tuple> set = jedis.zrevrangeWithScores("user:score", 0, count); Iterator<Tuple> iterator = set.iterator(); int rank = 1; while(iterator.hasNext()) { Tuple tuple = iterator.next(); String userInfo = tuple.getElement(); User user = MarshalJSONUtil.marshalToBean(userInfo, User.class); user.setScore(tuple.getScore()); user.setRank(rank++); list.add(user); } } catch(Exception e) { e.printStackTrace(); } finally { jedis.close(); } return list; }
7、创建处理请求的Servlet
public class RankServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter("type"); String countStr = request.getParameter("count"); int count = (countStr == null || "".equals(countStr)) ? -1 : Integer.parseInt(countStr); List<User> list = null; if("asc".equalsIgnoreCase(type)) { list = RankUtil.getRankData(count); } else { list = RankUtil.getRevRankData(count); } request.setAttribute("list", list); request.getRequestDispatcher("/rank.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }