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 return delegate.removeObject(key);
68 }
69
70 @Override
71 public void clear() {
72 delegate.clear();
73 keyList.clear();
74 }
75
76 private void cycleKeyList(Object key) {
77 keyList.addLast(key);
78 if (keyList.size() > size) {
79 Object oldestKey = keyList.removeFirst();
80 delegate.removeObject(oldestKey);
81 }
82 }
83
84 }