shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.support.redis.cache;
 
import com.walker.cache.Cache;
import com.walker.infrastructure.utils.StringUtils;
 
import java.util.List;
 
/**
 * 支持集合(List)缓存的实现。
 * @param <T>
 * @date 2023-06-14
 */
public abstract class RedisListCacheProvider<T> extends RedisCacheProvider<T>{
 
    @Override
    protected int loadDataToCache(Cache cache) {
        logger.info("基于集合的Redis缓存,不支持初始化加载缓存数据。");
        return 0;
    }
 
    @Override
    public T getCacheData(String key) {
        throw new UnsupportedOperationException("不支持该方法,请调用:getCacheList()");
    }
 
    @Override
    public long getCacheCount(){return 0;}
 
    @Override
    public void removeCacheData(String key) {
        throw new UnsupportedOperationException("不支持该方法,请调用:removeList()");
    }
 
    @Override
    public void updateCacheData(String key, T data) {
        throw new UnsupportedOperationException("不支持更新集合方法,请先删除老数据再添加");
    }
 
    @Override
    public void putCacheData(String key, T data){
        this.putCacheData(key, data, 0);
    }
 
    @Override
    public void putCacheData(String key, T data, long expiredSeconds){
        this.checkKey(key, data);
        this.getRedisCache().putListAppend(key, data);
    }
 
    @Override
    public void putCacheList(String key, List<T> data){
        this.putCacheList(key, data, 0);
    }
 
    @Override
    public void putCacheList(String key, List<T> data, long expiredSeconds){
        if(StringUtils.isEmptyList(data)){
            throw new IllegalArgumentException("list is required!");
        }
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key is required!");
        }
        this.getRedisCache().putList(key, (List<Object>)data, expiredSeconds);
    }
 
    @Override
    public void putCacheListAppend(String key, T data){
        this.checkKey(key, data);
        this.getRedisCache().putListAppend(key, data);
    }
 
    private void checkKey(String key, T data){
        if(data == null){
            throw new IllegalArgumentException("data is required!");
        }
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key is required!");
        }
    }
 
    @Override
    public List<T> getCacheList(String key){
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key is required!");
        }
        return (List<T>)this.getRedisCache().getList(key, 0, -1, getProviderType());
    }
 
    @Override
    public void removeCacheList(String key, T data){
        this.getRedisCache().removeList(key, data);
    }
 
    @Override
    public void removeCacheList(String key){
        this.getRedisCache().removeList(key);
    }
}