1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.cache.impl;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.apache.ibatis.cache.Cache;
22 import org.apache.ibatis.cache.CacheException;
23
24
25
26
27 public class PerpetualCache implements Cache {
28
29 private final String id;
30
31 private final Map<Object, Object> cache = new HashMap<>();
32
33 public PerpetualCache(String id) {
34 this.id = id;
35 }
36
37 @Override
38 public String getId() {
39 return id;
40 }
41
42 @Override
43 public int getSize() {
44 return cache.size();
45 }
46
47 @Override
48 public void putObject(Object key, Object value) {
49 cache.put(key, value);
50 }
51
52 @Override
53 public Object getObject(Object key) {
54 return cache.get(key);
55 }
56
57 @Override
58 public Object removeObject(Object key) {
59 return cache.remove(key);
60 }
61
62 @Override
63 public void clear() {
64 cache.clear();
65 }
66
67 @Override
68 public boolean equals(Object o) {
69 if (getId() == null) {
70 throw new CacheException("Cache instances require an ID.");
71 }
72 if (this == o) {
73 return true;
74 }
75 if (!(o instanceof Cache)) {
76 return false;
77 }
78
79 Cache otherCache = (Cache) o;
80 return getId().equals(otherCache.getId());
81 }
82
83 @Override
84 public int hashCode() {
85 if (getId() == null) {
86 throw new CacheException("Cache instances require an ID.");
87 }
88 return getId().hashCode();
89 }
90
91 }