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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package io.dataease.cache.impl;
 
import io.dataease.cache.DECacheService;
import io.dataease.utils.LogUtil;
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.jcache.JCacheCacheManager;
import org.springframework.stereotype.Component;
 
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ModifiedExpiryPolicy;
import java.util.concurrent.TimeUnit;
 
@ConditionalOnExpression("'${spring.cache.type}'.equals('jcache')")
@Component("dECacheService")
public class DefaultCacheImpl implements DECacheService {
 
 
    private CacheManager cacheManager;
 
    @Resource
    private org.springframework.cache.CacheManager jcacheManager;
 
 
    @Override
    public void put(String cacheName, String key, Object value, Long expTime, TimeUnit unit) {
        Cache<String, Object> cache = null;
        if (ObjectUtils.isEmpty(cache = cacheManager.getCache(cacheName))) {
            try {
                cache = cacheManager.createCache(cacheName, configuration(expTime, unit));
            } catch (Exception e) {
                LogUtil.error(e.getMessage(), e);
                cache = cacheManager.getCache(cacheName);
            }
 
        }
        if (ObjectUtils.isNotEmpty(cache)) {
            cache.put(key, value);
        }
 
    }
 
    @Override
    public Object get(String cacheName, String key) {
        Cache<Object, Object> cache = null;
        if (ObjectUtils.isEmpty(cache = cacheManager.getCache(cacheName))) {
            return null;
        }
        return cache.get(key);
    }
 
    @Override
    public boolean cacheExist(String cacheName) {
        return ObjectUtils.isNotEmpty(cacheManager.getCache(cacheName));
    }
 
    @Override
    public boolean keyExist(String cacheName, String key) {
        Cache<Object, Object> cache = null;
        if (ObjectUtils.isEmpty(cache = cacheManager.getCache(cacheName))) {
            return false;
        }
        return cache.containsKey(key);
    }
 
    private MutableConfiguration<String, Object> defaultConfiguration() {
        MutableConfiguration<String, Object> configuration =
                new MutableConfiguration<String, Object>()
                        .setTypes(String.class, Object.class)
                        .setStoreByValue(false)
                        .setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
        return configuration;
    }
 
    private MutableConfiguration<String, Object> configuration(Long expTime, TimeUnit unit) {
        if (expTime <= 0) return defaultConfiguration();
        if (ObjectUtils.isEmpty(unit)) {
            unit = TimeUnit.SECONDS;
        }
        MutableConfiguration<String, Object> configuration =
                new MutableConfiguration<String, Object>()
                        .setTypes(String.class, Object.class)
                        .setStoreByValue(false)
                        .setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(new Duration(unit, expTime)));
        return configuration;
    }
 
    @Override
    public void keyRemove(String cacheName, String key) {
        Cache<Object, Object> cache = cacheManager.getCache(cacheName);
        if(cache != null){
            cache.remove(key);
        }
    }
 
    @PostConstruct
    public void init() {
        cacheManager = ((JCacheCacheManager) jcacheManager).getCacheManager();
    }
 
}