wangrong
2026-08-30 4c949c0dc9bcae785c281c9d8125295f09810b15
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
package io.dataease.cache.impl;
 
import io.dataease.cache.DECacheService;
import io.dataease.utils.CommonBeanFactory;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.TimeUnit;
 
@ConditionalOnExpression("'${spring.cache.type}'.equals('redis')")
@Component("dECacheService")
public class RedisCacheImpl implements DECacheService {
 
    private static final String SEPARATOR = "::";
 
    @Resource
    private RedisTemplate redisTemplate;
 
    private static CacheManager cacheManager;
 
 
    private static CacheManager getCacheManager() {
        if (cacheManager == null)
            cacheManager = CommonBeanFactory.getBean(CacheManager.class);
        return cacheManager;
    }
 
 
    private ValueOperations ops() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        return valueOperations;
    }
 
    @Override
    public void put(String cacheName, String key, Object value, Long expTime, TimeUnit unit) {
        ValueOperations ops = ops();
        String realKey = cacheName + SEPARATOR + key;
        if (expTime <= 0) {
            ops.set(realKey, value);
            return;
        }
        if (ObjectUtils.isEmpty(unit)) {
            unit = TimeUnit.SECONDS;
        }
        ops.set(realKey, value, expTime, unit);
    }
 
    @Override
    public Object get(String cacheName, String key) {
        ValueOperations ops = ops();
        return ops.get(cacheName + SEPARATOR + key);
    }
 
    @Override
    public boolean cacheExist(String cacheName) {
        return true;
    }
 
    @Override
    public boolean keyExist(String cacheName, String key) {
        return redisTemplate.hasKey(cacheName + SEPARATOR + key);
    }
 
    @Override
    public void keyRemove(String cacheName, String key) {
        Cache cache = getCacheManager().getCache(cacheName);
        if (null == cache) return;
        cache.evictIfPresent(key);
        // cache.clear();
        redisTemplate.delete(cacheName + SEPARATOR + key);
    }
 
    @PostConstruct
    public void init() {
    }
}