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
package com.ishop.merchant.cache;
 
import com.ishop.merchant.Constants;
import com.ishop.merchant.MerProductCategoryCache;
import com.ishop.merchant.service.MerchantProductCategoryServiceImpl;
import com.ishop.merchant.util.ProductCategoryUtils;
import com.ishop.merchant.util.cache.MerProductCateSortComparator;
import com.ishop.model.po.EbMerchantProductCategory;
import com.ishop.model.vo.ProductCategoryVo;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.support.redis.cache.RedisListCacheProvider;
 
import java.util.Iterator;
import java.util.List;
 
public class RedisMerProductCategoryCache extends RedisListCacheProvider<EbMerchantProductCategory> implements MerProductCategoryCache {
 
    private final MerProductCateSortComparator sortComparator = new MerProductCateSortComparator();
 
    public RedisMerProductCategoryCache(){
        this.setUseRedis(true);
        this.setLoadPage(false);
        // 2024-01-05 设置支持缓存失效,该参数很重要,如果需要失效而没有设置在调用后会出现数据无法失效!
        this.setSupportExpiredCache(true);
    }
 
    @Override
    public void putList(String key, List<EbMerchantProductCategory> data) {
        this.putCacheList(key, data);
    }
 
    @Override
    public void putList(String key, List<EbMerchantProductCategory> data, long expiredSeconds) {
        this.putCacheList(key, data, expiredSeconds);
    }
 
    @Override
    public void putListAppend(String key, EbMerchantProductCategory data) {
        this.putCacheListAppend(key, data);
    }
 
    public List<ProductCategoryVo> getTree(String key){
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("商户商品分类必须提供");
        }
//        List<EbMerchantProductCategory> allTree = new ArrayList<>();
        List<EbMerchantProductCategory> list = this.getList(key);
        if(!StringUtils.isEmptyList(list)){
            // 2023-06-17
            // 注意:这是本地缓存,需要重新创建新集合,把缓存列表映射进来,目的是排除掉无效数据。
            // 但在 Redis 缓存中将不存在该情况,因为数据隔离所以无需重复拷贝。
            EbMerchantProductCategory e = null;
            for(Iterator<EbMerchantProductCategory> it = list.iterator(); it.hasNext();){
                e = it.next();
                if(e.getIsDel().intValue() == 1){
                    it.remove();
                }
                if(e.getIsShow().intValue() == 0){
                    it.remove();
                }
            }
        }
        return ProductCategoryUtils.acquireListTree(list, sortComparator);
    }
 
    @Override
    public List<EbMerchantProductCategory> getList(String key) {
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key必须提供");
        }
        List<EbMerchantProductCategory> list = this.getCacheList(key);
        if(list == null){
            logger.debug("没有缓存商户商品分类,从数据库加载并缓存:" + key);
            list = this.merchantProductCategoryService.queryMerchantProductCategoryList(Integer.parseInt(key));
            if(list == null){
                logger.warn("数据库未加载到:商户商品分类,无法缓存:" + key);
                return null;
            }
            this.putList(key, list);
        }
        return list;
    }
 
    @Override
    public void removeList(String key, EbMerchantProductCategory data) {
        this.removeCacheList(key, data);
    }
 
    @Override
    public String getProviderName() {
        return Constants.CACHE_NAME_MER_PRODUCT_CATEGORY;
    }
 
    @Override
    public Class<?> getProviderType() {
        return EbMerchantProductCategory.class;
    }
 
    public void setMerchantProductCategoryService(MerchantProductCategoryServiceImpl merchantProductCategoryService) {
        this.merchantProductCategoryService = merchantProductCategoryService;
    }
 
    private MerchantProductCategoryServiceImpl merchantProductCategoryService;
}