2021与蓝度共同重构项目,服务端
liuhaonan
2022-11-07 ddfbc40f9ca8546a2c34865abef51630f054d5e9
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
package com.sandu.common.redis;
 
import cn.hutool.core.util.StrUtil;
import org.springframework.lang.Nullable;
 
import java.util.Optional;
 
/**
 * @author chenjiantian
 */
public abstract class BasePrefix implements KeyPrefix {
 
    private final int expireSeconds;
 
    private final String prefix;
 
    public BasePrefix(int expireSeconds, String prefix) {
        this.expireSeconds = expireSeconds;
        this.prefix = prefix;
    }
 
    @Override
    public int expireSeconds() {//默认0代表永不过期
        return expireSeconds;
    }
 
    @Override
    public String key(@Nullable String uniqueId) {
        String modelName = getModelName();
        if(StrUtil.isEmpty(modelName)){
            throw new IllegalArgumentException("modelName 不能为空");
        }
        return modelName + ":" + prefix + ":" + Optional.ofNullable(uniqueId).orElse("");
    }
}