1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.cache.decorators;
17
18 import java.util.Deque;
19 import java.util.LinkedList;
20
21 import org.apache.ibatis.cache.Cache;
22
23
24
25
26
27
28 public class FifoCache implements Cache {
29
30 private final Cache delegate;
31 private final Deque<Object> keyList;
32 private int size;
33
34 public FifoCache(Cache delegate) {
35 this.delegate = delegate;
36 this.keyList = new LinkedList<>();
37 this.size = 1024;
38 }
39
40 @Override
41 public String getId() {
42 return delegate.getId();
43 }
44
45 @Override
46 public int getSize() {
47 return delegate.getSize();
48 }
49
50 public void setSize(int size) {
51 this.size = size;
52 }
53
54 @Override
55 public void putObject(Object key, Object value) {
56 cycleKeyList(key);
57 delegate.putObject(key, value);
58 }
59
60 @Override
61 public Object getObject(Object key) {
62 return delegate.getObject(key);
63 }
64
65 @Override
66 public Object removeObject(Object key) {
67 keyList.remove(key);
68 return delegate.removeObject(key);
69 }
70
71 @Override
72 public void clear() {
73 delegate.clear();
74 keyList.clear();
75 }
76
77 private void cycleKeyList(Object key) {
78 keyList.addLast(key);
79 if (keyList.size() > size) {
80 Object oldestKey = keyList.removeFirst();
81 delegate.removeObject(oldestKey);
82 }
83 }
84
85 }