|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package com.opensymphony.oscache.base.algorithm; |
|
6 |
| |
|
7 |
| import org.apache.commons.logging.Log; |
|
8 |
| import org.apache.commons.logging.LogFactory; |
|
9 |
| |
|
10 |
| import java.util.*; |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| public class LRUCache extends AbstractConcurrentReadCache { |
|
32 |
| |
|
33 |
| private static final Log log = LogFactory.getLog(LRUCache.class); |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| private Collection list = new LinkedHashSet(); |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| private volatile boolean removeInProgress = false; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
60
| public LRUCache() {
|
|
49 |
60
| super();
|
|
50 |
| } |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
36
| public LRUCache(int capacity) {
|
|
58 |
36
| this();
|
|
59 |
36
| maxEntries = capacity;
|
|
60 |
| } |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
8258
| protected void itemRetrieved(Object key) {
|
|
69 |
| |
|
70 |
8258
| while (removeInProgress) {
|
|
71 |
0
| try {
|
|
72 |
0
| Thread.sleep(5);
|
|
73 |
| } catch (InterruptedException ie) { |
|
74 |
| } |
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
8258
| synchronized (list) {
|
|
80 |
8258
| list.remove(key);
|
|
81 |
8258
| list.add(key);
|
|
82 |
| } |
|
83 |
| } |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
12303
| protected void itemPut(Object key) {
|
|
92 |
| |
|
93 |
12303
| synchronized (list) {
|
|
94 |
12303
| list.remove(key);
|
|
95 |
12303
| list.add(key);
|
|
96 |
| } |
|
97 |
| } |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
7228
| protected Object removeItem() {
|
|
107 |
7228
| removeInProgress = true;
|
|
108 |
| |
|
109 |
7228
| Object toRemove;
|
|
110 |
| |
|
111 |
7228
| try {
|
|
112 |
7228
| toRemove = removeFirst();
|
|
113 |
| } catch (Exception e) { |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
0
| do {
|
|
119 |
0
| try {
|
|
120 |
0
| Thread.sleep(5);
|
|
121 |
| } catch (InterruptedException ie) { |
|
122 |
| } |
|
123 |
0
| } while (list.size() == 0);
|
|
124 |
| |
|
125 |
0
| toRemove = removeFirst();
|
|
126 |
| } |
|
127 |
| |
|
128 |
7228
| removeInProgress = false;
|
|
129 |
| |
|
130 |
7228
| return toRemove;
|
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
90
| protected void itemRemoved(Object key) {
|
|
139 |
90
| list.remove(key);
|
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
7228
| private Object removeFirst() {
|
|
148 |
7228
| Iterator it = list.iterator();
|
|
149 |
7228
| Object toRemove = it.next();
|
|
150 |
7228
| it.remove();
|
|
151 |
| |
|
152 |
7228
| return toRemove;
|
|
153 |
| } |
|
154 |
| } |