elkers
10 天以前 3a1efe863c0cf30683870c204fb9b55ff6c35500
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.nanjing.water.service.cache;
 
import com.nanjing.water.common.util.SerializeUtil;
import com.nanjing.water.common.util.StringUtil;
import com.nanjing.water.repository.impl.AdminMenuMapperImpl;
import com.nanjing.water.repository.po.AdminMenuPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
 
@Service
public class AdminMenuService {
    @Autowired
    private AdminMenuMapperImpl dao;
 
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    /**
     * 获取所有菜单(缓存)
     */
    public List<AdminMenuPO> listAllWithCache() {
        try {
            if (redisTemplate.hasKey("adminMenusCache")) {
                String json = redisTemplate.opsForValue().get("adminMenusCache");
                if (StringUtil.isNotNullOrEmpty(json)) {
                    return SerializeUtil.toListObject(json, AdminMenuPO.class);
                }
            }
        } catch (Exception e) {
            //
        }
        List<AdminMenuPO> list = dao.getList();
        redisTemplate.opsForValue().set("adminMenusCache", SerializeUtil.toJson(list));
        return list;
    }
 
    // 删除菜单缓存
    public void removeListAllCache() {
        redisTemplate.delete("adminMenusCache");
    }
 
 
    /**
     * 获取用户拥有的菜单(缓存)
     */
    public List<AdminMenuPO> listUserAllWithCache(Long id, List<Long> listRoleId, Function<List<Long>, List<AdminMenuPO>> funcGetUserMenu) {
        try {
            if (redisTemplate.hasKey("userMenusCache")) {
                Object json = redisTemplate.opsForHash().get("userMenusCache", id);
                if (Objects.nonNull(json)) {
                    return SerializeUtil.toListObject(json.toString(), AdminMenuPO.class);
                }
            }
        } catch (Exception e) {
            //
        }
        List<AdminMenuPO> list = funcGetUserMenu.apply(listRoleId);
        redisTemplate.opsForHash().put("adminMenusCache", id, SerializeUtil.toJson(list));
        return list;
    }
    // 删除指定用户拥有的菜单缓存
    public void removeUserMenuCache(Long id) {
        redisTemplate.opsForHash().delete("userMenusCache", id);
    }
    // 删除所有用户拥有的菜单缓存
    public void removeAllUserMenuCache() {
        redisTemplate.delete("userMenusCache");
    }
}