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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.ishop.merchant.cache;
 
import com.ishop.merchant.Constants;
import com.ishop.merchant.ProductCategoryCache;
import com.ishop.merchant.service.ProductCategoryServiceImpl;
import com.ishop.merchant.util.ProductCategoryUtils;
import com.ishop.merchant.util.cache.ProductCategorySortComparator;
import com.ishop.model.po.EbProductCategory;
import com.ishop.model.vo.ProductCategoryVo;
import com.walker.cache.Cachable;
import com.walker.cache.Cache;
import com.walker.infrastructure.ApplicationRuntimeException;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.support.redis.cache.RedisCache;
import com.walker.support.redis.cache.RedisCacheProvider;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class RedisProductCategoryCache extends RedisCacheProvider<EbProductCategory> implements ProductCategoryCache {
 
    private final ProductCategorySortComparator sortComparator = new ProductCategorySortComparator();
 
    public RedisProductCategoryCache(){
        this.setUseRedis(true);
        this.setLoadPage(false);
    }
 
    /**
     * 返回第三级(平台)商品分类集合。
     * @param firstId 第一级ID
     * @param limit 限制几条
     * @return
     * @date 2023-06-23
     */
    @Override
    public List<EbProductCategory> getThirdLevelCategoryList(Integer firstId, int limit){
        List<EbProductCategory> thirdList = new ArrayList<>();
        EbProductCategory tempCategory = null;
        EbProductCategory twoLevelCategory = null;
        int count = 0;
        for(Iterator<Cachable> it = this.getCache().getIterator(); it.hasNext();){
            tempCategory = (EbProductCategory) it.next().getValue();
            if(tempCategory.getIsDel().intValue() == 1){
                continue;
            }
            if(tempCategory.getLevel().intValue() != 3){
                continue;
            }
            twoLevelCategory = this.get(tempCategory.getPid());
            if(twoLevelCategory.getIsDel().intValue() != 1 && twoLevelCategory.getPid().intValue() == firstId.intValue()){
                thirdList.add(tempCategory);
                count++;
            }
            if(count >= limit){
                break;
            }
        }
        return thirdList;
    }
 
    @Override
    protected int loadDataToCache(Cache cache) {
        List<EbProductCategory> hosts = this.productCategoryService.selectAll(new EbProductCategory());
        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(EbProductCategory h : hosts){
                        if(StringUtils.isNotEmpty(h.getIcon())){
                            h.setIcon(this.cdnUrl + h.getIcon());
                        }
//                        logger.debug(h.getIcon());
                        cache.put(String.valueOf(h.getId()), h);
                    }
                }
            }//------------------------------------------
            return hosts.size();
        }
        return 0;
    }
 
    @Override
    public List<ProductCategoryVo> getTree(Integer isShow) {
        List<EbProductCategory> allTree = new ArrayList<>();
        EbProductCategory tempCategory = null;
        String temp = null;
        try{
            for(String value : ((RedisCache)this.getCache()).getIterator(null)){
                temp = value;
                tempCategory = JsonUtils.jsonStringToObject(value, EbProductCategory.class);
                if(tempCategory.getIsDel().intValue() == 1){
                    continue;
                }
                if(isShow != null && tempCategory.getIsShow().intValue() != isShow.intValue()){
                    // 2023-06-15,商户端分类树,需要匹配该字段为:1
                    continue;
                }
                allTree.add(tempCategory);
            }
        } catch (Exception ex){
            throw new ApplicationRuntimeException("redis存储'EbProductCategory'解析错误:" + temp, ex);
        }
//        for(Iterator<Cachable> it = this.getCache().getIterator(); it.hasNext();){
//            tempCategory = (EbProductCategory) it.next().getValue();
//            if(tempCategory.getIsDel().intValue() == 1){
//                continue;
//            }
//            if(isShow != null && tempCategory.getIsShow().intValue() != isShow.intValue()){
//                // 2023-06-15,商户端分类树,需要匹配该字段为:1
//                continue;
//            }
//            allTree.add(tempCategory);
//        }
        return ProductCategoryUtils.acquireListTree(allTree, sortComparator);
    }
 
    @Override
    public List<ProductCategoryVo> getListTree() {
        return this.getTree(null);
    }
 
    @Override
    public EbProductCategory get(int id) {
        return this.getCacheData(String.valueOf(id));
    }
 
    @Override
    public void save(EbProductCategory category) {
        if(StringUtils.isNotEmpty(category.getIcon())){
            category.setIcon(this.cdnUrl + category.getIcon());
        }
        this.putCacheData(String.valueOf(category.getId()), category);
    }
 
    @Override
    public void update(EbProductCategory category) {
        if(StringUtils.isNotEmpty(category.getIcon())){
            category.setIcon(this.cdnUrl + category.getIcon());
        }
        this.updateCacheData(String.valueOf(category.getId()), category);
    }
 
    @Override
    public void remove(int id) {
        this.removeCacheData(String.valueOf(id));
    }
 
    @Override
    public String getProviderName() {
        return Constants.CACHE_NAME_PRODUCT_CATEGORY;
    }
 
    @Override
    public Class<?> getProviderType() {
        return EbProductCategory.class;
    }
 
    public void setProductCategoryService(ProductCategoryServiceImpl productCategoryService) {
        this.productCategoryService = productCategoryService;
    }
 
    /**
     * 系统配置的存储文件前缀,根据文件存储类型自动配置。
     * @param cdnUrl
     * @date 2023-06-25
     */
    public void setCdnUrl(String cdnUrl) {
        this.cdnUrl = cdnUrl;
    }
 
    private String cdnUrl = StringUtils.EMPTY_STRING;
    private ProductCategoryServiceImpl productCategoryService;
}