shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.iplatform.base.cache;
 
import com.iplatform.base.Constants;
import com.iplatform.base.UserCacheProvider;
import com.iplatform.base.service.UserServiceImpl;
import com.iplatform.model.po.S_user_core;
import com.walker.cache.Cache;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.support.redis.cache.RedisCacheProvider;
 
import java.util.List;
 
/**
 * Redis实现的用户缓存提供者。
 * @author 时克英
 * @date 2022-11-06
 */
public class RedisUserCacheProvider extends RedisCacheProvider<S_user_core> implements UserCacheProvider {
 
    private UserServiceImpl userService = null;
 
    private boolean allowInitCache = true;
 
    /**
     * 设置是否允许初始化加载用户到缓存。
     * @param allowInitCache
     * @date 2023-07-17
     */
    public void setAllowInitCache(boolean allowInitCache) {
        this.allowInitCache = allowInitCache;
    }
 
    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }
 
    public RedisUserCacheProvider(){
        this.setUseRedis(true);
        this.setLoadPage(false);
    }
 
    @Override
    public String getProviderName() {
        return Constants.CACHE_NAME_USER;
    }
 
    @Override
    public Class<?> getProviderType() {
        return S_user_core.class;
    }
 
    @Override
    protected int loadDataToCache(Cache cache) {
        if(this.allowInitCache){
            List<S_user_core> hosts = this.userService.selectAll(new S_user_core());
            if(!StringUtils.isEmptyList(hosts)){
                // ------------------------- 切换成普通缓存步骤:3
                if(this.isUseRedis()){
                    // 如果redis中缓存数量和数据库中不一致(少),则清空redis缓存,重新加载数据库数据到缓存中。
                    long totalCache = cache.getPersistentSize();
                    if(totalCache != hosts.size()){
                        logger.info("redis缓存中用户数量小于实际用户,需要清空缓存重新加载! cache = " + totalCache + ", db = " + hosts.size());
                        cache.clear();
 
                        for(S_user_core h : hosts){
                            cache.put(String.valueOf(h.getId()), h);
                        }
                    }
                }//------------------------------------------
                return hosts.size();
            }
        } else {
            logger.info("........由于用户量较大,所以配置:不需要初始化加载用户缓存!");
        }
        return 0;
    }
 
    @Override
    public S_user_core getUser(long userId) {
        S_user_core user_core = this.getCacheData(String.valueOf(userId));
        if(user_core == null){
            logger.warn("缓存中未找到用户对象,尝试从数据库加载, userId = " + userId);
            user_core = this.userService.get(new S_user_core(userId));
            this.putCacheData(String.valueOf(userId), user_core);
        }
        return user_core;
    }
 
    @Override
    public void updateUser(S_user_core user_core) {
        if(user_core == null){
            throw new IllegalArgumentException("更新用户缓存错误,user_core = null");
        }
        this.updateCacheData(String.valueOf(user_core.getId()), user_core);
    }
 
    @Override
    public void removeUser(long userId) {
        this.removeCacheData(String.valueOf(userId));
    }
 
    @Override
    public void putUser(S_user_core user_core) {
        if(user_core == null){
            throw new IllegalArgumentException("添加用户缓存错误: user_core = null");
        }
        this.putCacheData(String.valueOf(user_core.getId()), user_core);
    }
}