提交 | 用户 | 时间
|
e57a89
|
1 |
package com.jcdm.common.core.redis; |
懒 |
2 |
|
|
3 |
import org.springframework.cache.Cache; |
|
4 |
import org.springframework.cache.support.SimpleValueWrapper; |
|
5 |
import org.springframework.stereotype.Component; |
|
6 |
|
|
7 |
import java.util.Collection; |
|
8 |
import java.util.Map; |
|
9 |
import java.util.Objects; |
|
10 |
import java.util.concurrent.Callable; |
|
11 |
import java.util.concurrent.ConcurrentHashMap; |
|
12 |
|
|
13 |
@Component |
|
14 |
public class MyCache implements Cache { |
|
15 |
|
|
16 |
// 使用ConcurrentHashMap作为数据的存储 |
|
17 |
private Map<String, Object> storage = new ConcurrentHashMap<>(); |
|
18 |
|
|
19 |
// getName获取cache的名称,存取数据的时候用来区分是针对哪个cache操作 |
|
20 |
@Override |
|
21 |
public String getName() { |
|
22 |
return null; |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public Object getNativeCache() { |
|
27 |
return null; |
|
28 |
} |
|
29 |
|
|
30 |
public boolean hasKey(String key){ |
|
31 |
return storage.containsKey(key); |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public ValueWrapper get(Object key) { |
|
36 |
String k = key.toString(); |
|
37 |
Object value = storage.get(k); |
|
38 |
|
|
39 |
// 注意返回的数据,要和存放时接收到数据保持一致,要将数据反序列化回来。 |
|
40 |
return Objects.isNull(value) ? null : new SimpleValueWrapper(value); |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public <T> T get(Object key, Class<T> type) { |
|
45 |
return null; |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
public <T> T get(Object key, Callable<T> valueLoader) { |
|
50 |
return null; |
|
51 |
} |
|
52 |
|
|
53 |
// put方法,就是执行将数据进行缓存 |
|
54 |
@Override |
|
55 |
public void put(Object key, Object value) { |
|
56 |
if (Objects.isNull(value)) { |
|
57 |
return; |
|
58 |
} |
|
59 |
//存值 |
|
60 |
storage.put(key.toString(), value); |
|
61 |
} |
|
62 |
|
|
63 |
// evict方法,是用来清除某个缓存项 |
|
64 |
@Override |
|
65 |
public void evict(Object key) { |
|
66 |
storage.remove(key.toString()); |
|
67 |
} |
|
68 |
|
|
69 |
// 删除集合 |
|
70 |
public boolean deleteObject(final Collection collection){ |
|
71 |
collection.forEach(o -> { |
|
72 |
storage.remove(o.toString()); |
|
73 |
} ); |
|
74 |
return true; |
|
75 |
} |
|
76 |
|
|
77 |
// 获取所有的keys |
|
78 |
public Collection<String> keys(final String pattern){ |
|
79 |
return storage.keySet(); |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public void clear() { |
|
84 |
|
|
85 |
} |
|
86 |
} |