使用redis的有序集合zset實現排行榜功能,步驟有:java
一、下載jedis-2.7.2.jarredis
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> <type>jar</type> <scope>compile</scope> </dependency>
二、下載jackson所需的jar包dom
使用jackson實現數據的序列化和反序列化jsp
<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>
三、初始化JedisPool對象,並經過JedisPool建立Jedis對象spa
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(); } }
四、建立數據模型對象3d
public class User { private String id; private String userName; private Integer gender; private String redisterDate; private Double score; /** 排名 */ private Integer rank; ... ... ... }
五、經過jedis添加模擬數據code
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(); }
六、經過jedis排序,篩選數據xml
/** * 獲取降序排列的數據 * @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; }
七、建立處理請求的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); } }