Redis缓存实例3:Sortedset类型

 1 @Override
 2 public List<Category> findAll() {
 3     //1.从redis中查询
 4     //1.1获取jedis客户端
 5     Jedis jedis = JedisUtil.getJedis();
 6     //1.2可使用sortedset排序查询
 7     Set<String> categorys = jedis.zrange("category", 0, -1);
 8     List<Category> cs = null;
 9     //2.判断查询的集合是否为空
10     if (categorys == null || categorys.size() == 0) {
11 
12         System.out.println("从数据库查询....");
13         //3.如果为空,需要从数据库查询,在将数据存入redis
14         //3.1 从数据库查询
15         cs = categoryDao.findAll();
16         //3.2 将集合数据存储到redis中的 category的key
17         for (int i = 0; i < cs.size(); i++) {
18 
19             jedis.zadd("category", cs.get(i).getCid(), cs.get(i).getCname());
20         }
21     } else {
22         System.out.println("从redis中查询.....");
23 
24         //4.如果不为空,将set的数据存入list
25         cs = new ArrayList<Category>();
26         for (String name : categorys) {
27             Category category = new Category();
28             category.setCname(name);
29             cs.add(category);
30 
31         }
32     }
33 
34     return cs;
35 }

猜你喜欢

转载自www.cnblogs.com/aaaazzzz/p/13383611.html