1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.caches.caffeine;
17
18 import com.github.benmanes.caffeine.cache.Caffeine;
19
20 import java.util.concurrent.locks.ReadWriteLock;
21
22 import org.apache.ibatis.cache.Cache;
23
24
25
26
27
28
29 public final class CaffeineCache implements Cache {
30
31 private com.github.benmanes.caffeine.cache.Cache<Object, Object> cache;
32
33 private String id;
34
35
36
37
38
39
40
41 public CaffeineCache(String id) {
42 if (id == null) {
43 throw new IllegalArgumentException("Cache instances require an ID");
44 }
45
46 this.cache = Caffeine.newBuilder().build();
47 this.id = id;
48 }
49
50 @Override
51 public String getId() {
52 return this.id;
53 }
54
55 @Override
56 public void putObject(Object key, Object value) {
57
58 if (key != null && value != null) {
59 this.cache.put(key, value);
60 }
61 }
62
63 @Override
64 public Object getObject(Object key) {
65 return this.cache.getIfPresent(key);
66 }
67
68 @Override
69 public Object removeObject(Object key) {
70 return this.cache.asMap().remove(key);
71 }
72
73 @Override
74 public void clear() {
75 this.cache.invalidateAll();
76 }
77
78 @Override
79 public int getSize() {
80 return (int) this.cache.estimatedSize();
81 }
82
83 @Override
84 public ReadWriteLock getReadWriteLock() {
85 return null;
86 }
87
88 }