inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.core.cache.base;
26
27 import cn.hutool.cache.impl.CacheObj;
28 import cn.hutool.cache.impl.TimedCache;
29 import cn.hutool.core.collection.CollectionUtil;
30 import cn.hutool.core.util.StrUtil;
31 import vip.xiaonuo.core.cache.CacheOperator;
32
33 import java.util.*;
34
35 /**
36  * 基于内存的缓存封装
37  *
38  * @author yubaoshan
39  * @date 2020/7/9 10:09
40  */
41 public abstract class AbstractMemoryCacheOperator<T> implements CacheOperator<T> {
42
43     private final TimedCache<String, T> timedCache;
44
45     public AbstractMemoryCacheOperator(TimedCache<String, T> timedCache) {
46         this.timedCache = timedCache;
47     }
48
49     @Override
50     public void put(String key, T value) {
51         timedCache.put(getCommonKeyPrefix() + key, value);
52     }
53
54     @Override
55     public void put(String key, T value, Long timeoutSeconds) {
56         timedCache.put(getCommonKeyPrefix() + key, value, timeoutSeconds * 1000);
57     }
58
59     @Override
60     public T get(String key) {
61         // 如果用户在超时前调用了get(key)方法,会重头计算起始时间,false的作用就是不从头算
62         return timedCache.get(getCommonKeyPrefix() + key, true);
63     }
64
65     @Override
66     public void remove(String... key) {
67         if (key.length > 0) {
68             for (String itemKey : key) {
69                 timedCache.remove(getCommonKeyPrefix() + itemKey);
70             }
71         }
72     }
73
74     @Override
75     public Collection<String> getAllKeys() {
76         Iterator<CacheObj<String, T>> cacheObjIterator = timedCache.cacheObjIterator();
77         ArrayList<String> keys = CollectionUtil.newArrayList();
78         while (cacheObjIterator.hasNext()) {
79             // 去掉缓存key的common prefix前缀
80             String key = cacheObjIterator.next().getKey();
81             keys.add(StrUtil.removePrefix(key, getCommonKeyPrefix()));
82         }
83         return keys;
84     }
85
86     @Override
87     public Collection<T> getAllValues() {
88         Iterator<CacheObj<String, T>> cacheObjIterator = timedCache.cacheObjIterator();
89         ArrayList<T> values = CollectionUtil.newArrayList();
90         while (cacheObjIterator.hasNext()) {
91             values.add(cacheObjIterator.next().getValue());
92         }
93         return values;
94     }
95
96     @Override
97     public Map<String, T> getAllKeyValues() {
98         Collection<String> allKeys = this.getAllKeys();
99         HashMap<String, T> results = CollectionUtil.newHashMap();
100         for (String key : allKeys) {
101             results.put(key, this.get(key));
102         }
103         return results;
104     }
105 }