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
package com.walker.tcp.lb;
 
import com.walker.infrastructure.utils.StringUtils;
import com.walker.support.redis.cache.RedisCacheProvider;
import com.walker.tcp.Constants;
 
/**
 * 连接元数据对象缓存定义。
 * <p>这仅仅是描述了连接基本信息,不是长连接本身!</p>
 * @author 时克英
 * @date 2023-09-25
 */
public class RedisConnectionMetaCache extends RedisCacheProvider<LongConnectionMeta> {
 
    public RedisConnectionMetaCache(){
        this.setUseRedis(true);
        this.setLoadPage(false);
        // 2024-01-05 设置支持缓存失效,该参数很重要,如果需要失效而没有设置在调用后会出现数据无法失效!
        this.setSupportExpiredCache(true);
    }
 
    public LongConnectionMeta getConnectionMeta(String id){
        return this.getCacheData(id);
    }
 
    public void removeConnectionMeta(String id){
        this.removeCacheData(id);
    }
 
    /**
     * 缓存一个连接元数据对象,默认:24小时候失效。
     * @param connectionMeta
     */
    public void putConnectionMeta(LongConnectionMeta connectionMeta){
//        this.putCacheData(this.getKey(connectionMeta.getConnectionHost(), connectionMeta.getId()), connectionMeta, expiredSeconds);
        this.putCacheData(connectionMeta.getId(), connectionMeta, expiredSeconds);
    }
 
    @Deprecated
    private String getKey(String connectionHost, String id){
        return new StringBuilder(connectionHost).append(StringUtils.SEPARATOR_COLON).append(id).toString();
    }
 
    @Override
    public String getProviderName() {
        return Constants.CACHE_NAME_CONNECTION_META;
    }
 
    @Override
    public Class<?> getProviderType() {
        return LongConnectionMeta.class;
    }
 
    public long getExpiredSeconds() {
        return expiredSeconds;
    }
 
    /**
     * 设置缓存失效时间,单位:秒,如果不设置默认:24小时
     * @param expiredSeconds
     */
    public void setExpiredSeconds(long expiredSeconds) {
        this.expiredSeconds = expiredSeconds;
    }
 
    private long expiredSeconds = 8 * 3600;
}