1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.cache;
17
18 import java.util.NoSuchElementException;
19 import java.io.Serializable;
20
21
22
23
24
25
26 public class StaleObjectEvictor implements Runnable, Serializable {
27 public transient static final int DEFAULT_OBJECTS_BETWEEN_NAPS;
28 static {
29 int objsTweenNaps = 10;
30 try {
31 objsTweenNaps = Integer.parseInt(System.getProperty("org.apache.commons.cache.StaleObjectEvictor.objs-between-naps","10"));
32 } catch(Exception e) {
33 objsTweenNaps = 10;
34 }
35 DEFAULT_OBJECTS_BETWEEN_NAPS = objsTweenNaps;
36 }
37
38 public transient static final long DEFAULT_SLEEP_TIME_MILLIS;
39 static {
40 long sleepTime = 10000;
41 try {
42 sleepTime = Long.parseLong(System.getProperty("org.apache.commons.cache.StaleObjectEvictor.sleep-time-millis","10000"));
43 } catch(Exception e) {
44 sleepTime = 10000;
45 }
46 DEFAULT_SLEEP_TIME_MILLIS = sleepTime;
47 }
48
49 protected int _objsBetweenNaps = DEFAULT_OBJECTS_BETWEEN_NAPS;
50 protected long _sleepTimeMillis = DEFAULT_SLEEP_TIME_MILLIS;
51 protected CachedObjectIterator _iterator;
52 protected boolean _cancelled = false;
53
54 public StaleObjectEvictor(CachedObjectIterator it) {
55 this(it,DEFAULT_OBJECTS_BETWEEN_NAPS,DEFAULT_SLEEP_TIME_MILLIS);
56 }
57
58 public StaleObjectEvictor(CachedObjectIterator it, int objsBetweenNaps) {
59 this(it,objsBetweenNaps,DEFAULT_SLEEP_TIME_MILLIS);
60 }
61
62 public StaleObjectEvictor(CachedObjectIterator it, long sleepTimeMillis) {
63 this(it,DEFAULT_OBJECTS_BETWEEN_NAPS,sleepTimeMillis);
64 }
65
66 public StaleObjectEvictor(CachedObjectIterator it, int objsBetweenNaps, long sleepTimeMillis) {
67 _iterator = it;
68 _objsBetweenNaps = objsBetweenNaps;
69 _sleepTimeMillis = sleepTimeMillis;
70 }
71
72 public void cancel() {
73 _cancelled = true;
74 }
75
76 public void run() {
77 while(!_cancelled) {
78 for(int i=0;i<_objsBetweenNaps;i++) {
79 CachedObjectInfo info = null;
80 try {
81 info = _iterator.getNext();
82 } catch(NoSuchElementException e) {
83 info = null;
84 }
85 if(null != info) {
86 Long expiry = info.getExpirationTs();
87 if(null != expiry) {
88 if(System.currentTimeMillis() >= expiry.longValue()) {
89 try {
90 _iterator.remove();
91 } catch(Exception e) {
92
93 }
94 }
95 }
96 } else {
97 break;
98 }
99 }
100 try {
101 Thread.sleep(_sleepTimeMillis);
102 } catch(InterruptedException e) {
103
104 }
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119 }