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
package com.iplatform.base.cache;
 
import com.iplatform.base.Constants;
import com.iplatform.base.NotificationTemplateCache;
import com.iplatform.base.NotifyConstants;
import com.iplatform.base.service.NotificationServiceImpl;
import com.iplatform.model.po.SfNotification;
import com.iplatform.model.po.SfTemplateMessage;
import com.iplatform.model.vo.NotificationTemplateVo;
import com.walker.cache.Cache;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.support.redis.cache.RedisCacheProvider;
 
import java.util.List;
import java.util.Map;
 
public class RedisNotificationTemplateCache extends RedisCacheProvider<NotificationTemplateVo> implements NotificationTemplateCache {
 
    public RedisNotificationTemplateCache(){
        this.setUseRedis(true);
        this.setLoadPage(false);
    }
 
    @Override
    protected int loadDataToCache(Cache cache) {
        List<SfNotification> notificationList = this.notificationService.selectAll(new SfNotification());
        if(!StringUtils.isEmptyList(notificationList)){
            // ------------------------- 切换成普通缓存步骤:3
            if(this.isUseRedis()){
                // 如果redis中缓存数量和数据库中不一致(少),则清空redis缓存,重新加载数据库数据到缓存中。
                long totalCache = cache.getPersistentSize();
                if(totalCache != notificationList.size()){
                    logger.info(Constants.CACHE_NAME_NOTIFICATION_TEMPLATE + ": redis缓存中用户数量小于实际用户,需要清空缓存重新加载! cache = " + totalCache + ", db = " + notificationList.size());
                    cache.clear();
 
                    Map<Long, SfTemplateMessage> templateMessageMap = this.notificationService.queryTemplateIdMap();
                    if(templateMessageMap.size() == 0){
                        logger.warn("提醒模板未找到任何有效数据");
                        return 0;
                    }
                    for(SfNotification h : notificationList){
                        NotificationTemplateVo vo = new NotificationTemplateVo();
                        vo.setName(h.getMark());
                        if(h.getIsWechat().intValue() == NotifyConstants.SWITCH_OPEN){
                            vo.setWechat(true);
                            vo.setWechatId(h.getWechatId());
                            vo.setWechatTempId(templateMessageMap.get(h.getWechatId().longValue()).getTempId());
                        }
                        if(h.getIsRoutine().intValue() == NotifyConstants.SWITCH_OPEN){
                            vo.setRoutine(true);
                            vo.setRoutineId(h.getRoutineId());
                            vo.setRoutineTempId(templateMessageMap.get(h.getRoutineId().longValue()).getTempId());
                        }
                        if(h.getIsSms().intValue() == NotifyConstants.SWITCH_OPEN){
                            vo.setSms(true);
                            vo.setSmsId(h.getSmsId());
                            vo.setSmsTempId(templateMessageMap.get(h.getSmsId().longValue()).getTempId());
                        }
                        cache.put(h.getMark(), vo);
                    }
                }
            }//------------------------------------------
            return notificationList.size();
        }
        return 0;
    }
 
    @Override
    public NotificationTemplateVo get(String mark) {
        return this.getCacheData(mark);
    }
 
    @Override
    public void save(NotificationTemplateVo category) {
        this.putCacheData(category.getName(), category);
    }
 
    @Override
    public void update(NotificationTemplateVo category) {
        this.updateCacheData(category.getName(), category);
    }
 
    @Override
    public void remove(String mark) {
        this.removeCacheData(mark);
    }
 
    @Override
    public String getProviderName() {
        return Constants.CACHE_NAME_NOTIFICATION_TEMPLATE;
    }
 
    @Override
    public Class<?> getProviderType() {
        return NotificationTemplateVo.class;
    }
 
    public void setNotificationService(NotificationServiceImpl notificationService) {
        this.notificationService = notificationService;
    }
 
    private NotificationServiceImpl notificationService;
}