1 package org.apache.commons.jcs3.engine.memory.shrinking;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Set;
23
24 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
25 import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
26 import org.apache.commons.jcs3.engine.control.CompositeCache;
27 import org.apache.commons.jcs3.engine.control.event.behavior.ElementEventType;
28 import org.apache.commons.jcs3.engine.memory.behavior.IMemoryCache;
29 import org.apache.commons.jcs3.log.Log;
30 import org.apache.commons.jcs3.log.LogManager;
31
32
33
34
35
36 public class ShrinkerThread<K, V>
37 implements Runnable
38 {
39
40 private static final Log log = LogManager.getLog( ShrinkerThread.class );
41
42
43 private final CompositeCache<K, V> cache;
44
45
46 private final long maxMemoryIdleTime;
47
48
49 private final int maxSpoolPerRun;
50
51
52 private boolean spoolLimit;
53
54
55
56
57
58
59 public ShrinkerThread( final CompositeCache<K, V> cache )
60 {
61 this.cache = cache;
62
63 final long maxMemoryIdleTimeSeconds = cache.getCacheAttributes().getMaxMemoryIdleTimeSeconds();
64
65 if ( maxMemoryIdleTimeSeconds < 0 )
66 {
67 this.maxMemoryIdleTime = -1;
68 }
69 else
70 {
71 this.maxMemoryIdleTime = maxMemoryIdleTimeSeconds * 1000;
72 }
73
74 this.maxSpoolPerRun = cache.getCacheAttributes().getMaxSpoolPerRun();
75 if ( this.maxSpoolPerRun != -1 )
76 {
77 this.spoolLimit = true;
78 }
79
80 }
81
82
83
84
85 @Override
86 public void run()
87 {
88 shrink();
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 protected void shrink()
107 {
108 log.debug( "Shrinking memory cache for: {0}", this.cache::getCacheName);
109
110 final IMemoryCache<K, V> memCache = cache.getMemoryCache();
111
112 try
113 {
114 final Set<K> keys = memCache.getKeySet();
115 final int size = keys.size();
116 log.debug( "Keys size: {0}", size );
117
118 int spoolCount = 0;
119
120 for (final K key : keys)
121 {
122 final ICacheElement<K, V> cacheElement = memCache.getQuiet( key );
123
124 if ( cacheElement == null )
125 {
126 continue;
127 }
128
129 final IElementAttributes attributes = cacheElement.getElementAttributes();
130
131 boolean remove = false;
132
133 final long now = System.currentTimeMillis();
134
135
136
137 if ( !attributes.getIsEternal() )
138 {
139 remove = cache.isExpired( cacheElement, now,
140 ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND,
141 ElementEventType.EXCEEDED_IDLETIME_BACKGROUND );
142
143 if ( remove )
144 {
145 memCache.remove( key );
146 }
147 }
148
149
150
151
152 if ( !remove && maxMemoryIdleTime != -1 )
153 {
154 if ( !spoolLimit || spoolCount < this.maxSpoolPerRun )
155 {
156 final long lastAccessTime = attributes.getLastAccessTime();
157
158 if ( lastAccessTime + maxMemoryIdleTime < now )
159 {
160 log.debug( "Exceeded memory idle time: {0}", key );
161
162
163
164
165
166
167
168 spoolCount++;
169
170 memCache.remove( key );
171 memCache.waterfal( cacheElement );
172 }
173 }
174 else
175 {
176 log.debug( "spoolCount = \"{0}\"; maxSpoolPerRun = \"{1}\"",
177 spoolCount, maxSpoolPerRun );
178
179
180 if ( spoolLimit && spoolCount >= this.maxSpoolPerRun )
181 {
182 return;
183 }
184 }
185 }
186 }
187 }
188 catch ( final Throwable t )
189 {
190 log.info( "Unexpected trouble in shrink cycle", t );
191
192
193
194
195
196 return;
197 }
198 }
199 }