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.core.collection.CollectionUtil; |
|
28 |
import cn.hutool.core.util.StrUtil; |
|
29 |
import org.springframework.data.redis.core.RedisTemplate; |
|
30 |
import vip.xiaonuo.core.cache.CacheOperator; |
|
31 |
import vip.xiaonuo.core.consts.SymbolConstant; |
|
32 |
|
|
33 |
import java.util.*; |
|
34 |
import java.util.concurrent.TimeUnit; |
|
35 |
import java.util.stream.Collectors; |
|
36 |
|
|
37 |
/** |
|
38 |
* 基于redis的缓存封装 |
|
39 |
* |
|
40 |
* @author yubaoshan |
|
41 |
* @date 2020/7/9 10:09 |
|
42 |
*/ |
|
43 |
public abstract class AbstractRedisCacheOperator<T> implements CacheOperator<T> { |
|
44 |
|
|
45 |
private final RedisTemplate<String, T> redisTemplate; |
|
46 |
|
|
47 |
public AbstractRedisCacheOperator(RedisTemplate<String, T> redisTemplate) { |
|
48 |
this.redisTemplate = redisTemplate; |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public void put(String key, T value) { |
|
53 |
redisTemplate.boundValueOps(getCommonKeyPrefix() + key).set(value); |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
57 |
public void put(String key, T value, Long timeoutSeconds) { |
|
58 |
redisTemplate.boundValueOps(getCommonKeyPrefix() + key).set(value, timeoutSeconds, TimeUnit.SECONDS); |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public T get(String key) { |
|
63 |
return redisTemplate.boundValueOps(getCommonKeyPrefix() + key).get(); |
|
64 |
} |
|
65 |
|
|
66 |
@Override |
|
67 |
public void remove(String... key) { |
|
68 |
ArrayList<String> keys = CollectionUtil.toList(key); |
|
69 |
List<String> withPrefixKeys = keys.stream().map(i -> getCommonKeyPrefix() + i).collect(Collectors.toList()); |
|
70 |
redisTemplate.delete(withPrefixKeys); |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public Collection<String> getAllKeys() { |
|
75 |
Set<String> keys = redisTemplate.keys(getCommonKeyPrefix() + SymbolConstant.ASTERISK); |
|
76 |
if (keys != null) { |
|
77 |
// 去掉缓存key的common prefix前缀 |
|
78 |
return keys.stream().map(key -> StrUtil.removePrefix(key, getCommonKeyPrefix())).collect(Collectors.toSet()); |
|
79 |
} else { |
|
80 |
return CollectionUtil.newHashSet(); |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
@Override |
|
85 |
public Collection<T> getAllValues() { |
|
86 |
Set<String> keys = redisTemplate.keys(getCommonKeyPrefix() + SymbolConstant.ASTERISK); |
|
87 |
if (keys != null) { |
|
88 |
return redisTemplate.opsForValue().multiGet(keys); |
|
89 |
} else { |
|
90 |
return CollectionUtil.newArrayList(); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
@Override |
|
95 |
public Map<String, T> getAllKeyValues() { |
|
96 |
Collection<String> allKeys = this.getAllKeys(); |
|
97 |
HashMap<String, T> results = CollectionUtil.newHashMap(); |
|
98 |
for (String key : allKeys) { |
|
99 |
results.put(key, this.get(key)); |
|
100 |
} |
|
101 |
return results; |
|
102 |
} |
|
103 |
} |