• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-wings / #131

16 Jan 2025 02:20PM UTC coverage: 63.526% (+0.03%) from 63.495%
#131

push

trydofor
⬆️ 💥 up deps, boot 3.3, mirana new i18n #309

30 of 68 new or added lines in 28 files covered. (44.12%)

36 existing lines in 3 files now uncovered.

12920 of 20338 relevant lines covered (63.53%)

0.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.33
/wings/slardar/src/main/java/pro/fessional/wings/slardar/cache/WingsCacheHelper.java
1
package pro.fessional.wings.slardar.cache;
2

3
import lombok.extern.slf4j.Slf4j;
4
import org.jetbrains.annotations.NotNull;
5
import org.jetbrains.annotations.Nullable;
6
import org.springframework.cache.Cache;
7
import org.springframework.cache.CacheManager;
8
import org.springframework.cache.interceptor.CacheOperation;
9
import pro.fessional.mirana.best.AssertState;
10
import pro.fessional.mirana.data.Null;
11

12
import java.lang.reflect.Method;
13
import java.util.Collection;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.concurrent.ConcurrentHashMap;
19

20
import static java.util.Collections.emptyMap;
21
import static java.util.Collections.emptySet;
22

23
/**
24
 * @author trydofor
25
 * @since 2021-03-08
26
 */
27
@Slf4j
1✔
28
public class WingsCacheHelper {
29

30
    private static final Map<String, CacheManager> NameManager = new ConcurrentHashMap<>();
1✔
31
    private static final Map<CacheManager, Set<String>> ManagerName = new ConcurrentHashMap<>();
1✔
32
    private static CacheManager MemoryManager = null;
1✔
33
    private static CacheManager ServerManager = null;
1✔
34
    private static boolean helperPrepared = false;
1✔
35

36

37
    /**
38
     * Set CacheManager name and its Resolver
39
     */
40
    protected WingsCacheHelper(Map<String, CacheManager> mngs) {
1✔
41
        NameManager.putAll(mngs);
1✔
42

43
        MemoryManager = NameManager.get(WingsCache.Manager.Memory);
1✔
44
        ServerManager = NameManager.get(WingsCache.Manager.Server);
1✔
45

46
        ManagerName.clear();
1✔
47
        for (Map.Entry<String, CacheManager> en : NameManager.entrySet()) {
1✔
48
            ManagerName.computeIfAbsent(en.getValue(), k -> new HashSet<>())
1✔
49
                       .add(en.getKey());
1✔
50
        }
1✔
51
        helperPrepared = true;
1✔
52
    }
1✔
53

54
    /**
55
     * whether this helper is prepared
56
     */
57
    public static boolean isPrepared() {
58
        return helperPrepared;
×
59
    }
60

61
    @Nullable
62
    public static CacheManager getCacheManager(String name) {
63
        if (WingsCache.Manager.Memory.equalsIgnoreCase(name)) {
1✔
64
            return MemoryManager;
1✔
65
        }
66
        if (WingsCache.Manager.Server.equalsIgnoreCase(name)) {
×
67
            return ServerManager;
×
68
        }
69
        return NameManager.get(name);
×
70
    }
71

72
    public static Set<String> getManagerNames(CacheManager manage) {
73
        if (manage == null) return emptySet();
1✔
74
        return ManagerName.getOrDefault(manage, emptySet());
1✔
75
    }
76

77
    @Nullable
78
    public static Cache getCache(String manager, String cache) {
79
        final CacheManager cm = getCacheManager(manager);
×
80
        return cm == null ? null : cm.getCache(cache);
×
81
    }
82

83
    @NotNull
84
    public static Cache getMemoryCache(String name) {
85
        final Cache cache = MemoryManager.getCache(name);
×
NEW
86
        AssertState.notNull(cache, "cacheName", "memory cache is null, name={}", name);
×
87
        return cache;
×
88
    }
89

90
    @NotNull
91
    public static Cache getServerCache(String name) {
92
        final Cache cache = ServerManager.getCache(name);
×
NEW
93
        AssertState.notNull(cache, "cacheName", "server cache is null, name={}", name);
×
94
        return cache;
×
95
    }
96

97
    @NotNull
98
    public static CacheManager getMemory() {
99
        AssertState.notNull(MemoryManager, "cacheManager", "Memory CacheManager is null");
1✔
100
        return MemoryManager;
1✔
101
    }
102

103
    @NotNull
104
    public static CacheManager getServer() {
105
        AssertState.notNull(ServerManager, "cacheManager", "Server CacheManager is null");
1✔
106
        return ServerManager;
1✔
107
    }
108

109
    ///
110
    private static final Map<Class<?>, Map<String, Meta>> ClassCacheMeta = new ConcurrentHashMap<>();
1✔
111

112
    public static class Meta {
1✔
113
        private final Map<String, Set<String>> MngRlvCache = new HashMap<>();
1✔
114

115
        private final Map<CacheManager, Set<String>> managerName = new HashMap<>(); // lazy
1✔
116
        private final Map<String, Set<Cache>> managerCache = new HashMap<>(); // lazy
1✔
117

118
        public void initOperation(String cr, String cm, Set<String> cs) {
119
            if (!cr.isEmpty()) {
1✔
120
                MngRlvCache.computeIfAbsent(cr, k -> new HashSet<>()).addAll(cs);
×
121
            }
122
            if (!cm.isEmpty()) {
1✔
123
                MngRlvCache.computeIfAbsent(cm, k -> new HashSet<>()).addAll(cs);
1✔
124
            }
125
        }
1✔
126

127
        /**
128
         * CacheManager and its names
129
         */
130
        public Map<CacheManager, Set<String>> getManagers() {
131
            if (MngRlvCache.isEmpty()) return managerName;
1✔
132

133
            if (managerName.isEmpty()) {
1✔
134
                for (String nm : MngRlvCache.keySet()) {
1✔
135
                    final CacheManager m = getCacheManager(nm);
1✔
136
                    AssertState.notNull(m, "cacheManager", "no CacheManager for {}", nm);
1✔
137
                    managerName.put(m, getManagerNames(m));
1✔
138
                }
1✔
139
            }
140
            return managerName;
1✔
141
        }
142

143
        /**
144
         * manager/resolver and its Caches
145
         */
146
        public Map<String, Set<Cache>> getCaches() {
147
            if (MngRlvCache.isEmpty()) return managerCache;
×
148

149
            if (managerCache.isEmpty()) {
×
150
                for (Map.Entry<String, Set<String>> en : MngRlvCache.entrySet()) {
×
151
                    String k = en.getKey();
×
152
                    final CacheManager m = getCacheManager(k);
×
NEW
153
                    AssertState.notNull(m, "cacheManager", "no CacheManager for {}", k);
×
154
                    Set<Cache> st = new HashSet<>();
×
155
                    for (String c : en.getValue()) {
×
156
                        st.add(m.getCache(c));
×
157
                    }
×
158
                    managerCache.put(k, st);
×
159
                }
×
160
            }
161
            return managerCache;
×
162
        }
163
    }
164

165
    /**
166
     * manager/resolver name and its caches name
167
     */
168
    @NotNull
169
    public static Map<String, Set<String>> getCacheMeta(Class<?> clz) {
170
        return getCacheMeta(clz, Null.Str);
×
171
    }
172

173
    /**
174
     * manager/resolver name and its caches name
175
     */
176
    @NotNull
177
    public static Map<String, Set<String>> getCacheMeta(Class<?> claz, String method) {
178
        final Map<String, Meta> map = ClassCacheMeta.get(claz);
1✔
179
        if (map == null) return emptyMap();
1✔
180
        if (method == null) method = Null.Str;
1✔
181
        final Meta mt = map.get(method);
1✔
182
        return mt == null ? emptyMap() : mt.MngRlvCache;
1✔
183
    }
184

185
    /**
186
     * CacheManager and its names
187
     */
188
    @NotNull
189
    public static Map<CacheManager, Set<String>> getManager(Class<?> clz) {
190
        return getManager(clz, Null.Str);
1✔
191
    }
192

193
    /**
194
     * CacheManager and its names
195
     */
196
    @NotNull
197
    public static Map<CacheManager, Set<String>> getManager(Class<?> claz, String method) {
198
        final Map<String, Meta> map = ClassCacheMeta.get(claz);
1✔
199
        if (map == null) return emptyMap();
1✔
200
        if (method == null) method = Null.Str;
1✔
201
        final Meta mt = map.get(method);
1✔
202
        return mt == null ? emptyMap() : mt.getManagers();
1✔
203
    }
204

205
    /**
206
     * manager/resolver and its Caches
207
     */
208
    @NotNull
209
    public static Map<String, Set<Cache>> getCaches(Class<?> clz) {
210
        return getCaches(clz, Null.Str);
×
211
    }
212

213
    /**
214
     * manager/resolver and its Caches
215
     */
216
    @NotNull
217
    public static Map<String, Set<Cache>> getCaches(Class<?> claz, String method) {
218
        final Map<String, Meta> map = ClassCacheMeta.get(claz);
×
219
        if (map == null) return emptyMap();
×
220
        if (method == null) method = Null.Str;
×
221
        final Meta mt = map.get(method);
×
222
        return mt == null ? emptyMap() : mt.getCaches();
×
223
    }
224

225
    public static void prepareOperation(Method method, Collection<CacheOperation> opr) {
226
        if (opr == null || opr.isEmpty()) return;
1✔
227

228
        final Map<String, Meta> entry = ClassCacheMeta.computeIfAbsent(method.getDeclaringClass(), k -> new ConcurrentHashMap<>());
1✔
229
        final Meta top = entry.computeIfAbsent(Null.Str, k -> new Meta());
1✔
230
        final Meta mod = entry.computeIfAbsent(method.getName(), k -> new Meta());
1✔
231

232
        for (CacheOperation op : opr) {
1✔
233
            final String cr = op.getCacheResolver();
1✔
234
            final String cm = op.getCacheManager();
1✔
235
            final Set<String> cs = op.getCacheNames();
1✔
236
            top.initOperation(cr, cm, cs);
1✔
237
            mod.initOperation(cr, cm, cs);
1✔
238
        }
1✔
239
    }
1✔
240
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc