20
20
package org .dcache .nfs .util ;
21
21
22
22
import java .time .Clock ;
23
+ import java .time .Duration ;
24
+ import java .time .Instant ;
23
25
import java .util .ArrayList ;
24
26
import java .util .HashMap ;
25
27
import java .util .Iterator ;
26
28
import java .util .List ;
27
29
import java .util .Map ;
28
30
import java .util .MissingResourceException ;
29
- import java .util .concurrent .atomic .AtomicLong ;
31
+ import java .util .concurrent .atomic .AtomicReference ;
30
32
import java .util .concurrent .locks .StampedLock ;
31
33
32
34
import org .slf4j .Logger ;
40
42
*
41
43
* Typical usage is:
42
44
* <pre>
43
- * Cache<String, String> cache = new Cache<>("test cache", 10, TimeUnit.HOURS.toMillis (1),
44
- * TimeUnit.MINUTES.toMillis(5) );
45
+ * Cache<String, String> cache = new Cache<>("test cache", 10, Duration.ofHours (1),
46
+ * Duration.ofMinutes(2 );
45
47
*
46
48
* cache.put("key", "value");
47
49
* String value = cache.get("key");
@@ -65,17 +67,16 @@ public class Cache<K, V> {
65
67
private final String _name ;
66
68
67
69
/**
68
- * Maximum allowed time, in milliseconds, that an object is allowed to be cached.
70
+ * Maximum amount of time that an object is allowed to be cached.
69
71
* After expiration of this time cache entry invalidated.
70
72
*/
71
-
72
- private final long _defaultEntryMaxLifeTime ;
73
+ private final Duration _defaultEntryMaxLifeTime ;
73
74
74
75
/**
75
- * Time in milliseconds since last use of the object. After expiration of this
76
+ * Time amount since last use of the object. After expiration of this
76
77
* time cache entry is invalidated.
77
78
*/
78
- private final long _defaultEntryIdleTime ;
79
+ private final Duration _defaultEntryIdleTime ;
79
80
80
81
/**
81
82
* Maximum number of entries in cache.
@@ -105,18 +106,18 @@ public class Cache<K, V> {
105
106
/**
106
107
* Last cleanup time
107
108
*/
108
- private final AtomicLong _lastClean = new AtomicLong ( System . currentTimeMillis ()) ;
109
+ private final AtomicReference < Instant > _lastClean ;
109
110
110
111
/**
111
112
* Create new cache instance with default {@link CacheEventListener} and
112
113
* default cleanup period.
113
114
*
114
115
* @param name Unique id for this cache.
115
116
* @param size maximal number of elements.
116
- * @param entryLifeTime maximal time in milliseconds .
117
- * @param entryIdleTime maximal idle time in milliseconds .
117
+ * @param entryLifeTime maximal time that an entry allowed to stay in the cache after creation .
118
+ * @param entryIdleTime maximal time that an entry allowed to stay in the cache after last access .
118
119
*/
119
- public Cache (String name , int size , long entryLifeTime , long entryIdleTime ) {
120
+ public Cache (String name , int size , Duration entryLifeTime , Duration entryIdleTime ) {
120
121
this (name , size , entryLifeTime , entryIdleTime ,
121
122
new NopCacheEventListener <K , V >());
122
123
}
@@ -126,11 +127,11 @@ public Cache(String name, int size, long entryLifeTime, long entryIdleTime) {
126
127
*
127
128
* @param name Unique id for this cache.
128
129
* @param size maximal number of elements.
129
- * @param entryLifeTime maximal time in milliseconds .
130
- * @param entryIdleTime maximal idle time in milliseconds .
130
+ * @param entryLifeTime maximal time that an entry allowed to stay in the cache after creation .
131
+ * @param entryIdleTime maximal time that an entry allowed to stay in the cache after last access .
131
132
* @param eventListener {@link CacheEventListener}
132
133
*/
133
- public Cache (final String name , int size , long entryLifeTime , long entryIdleTime ,
134
+ public Cache (final String name , int size , Duration entryLifeTime , Duration entryIdleTime ,
134
135
CacheEventListener <K , V > eventListener ) {
135
136
this (name , size , entryLifeTime , entryIdleTime , eventListener , Clock .systemDefaultZone ());
136
137
}
@@ -140,16 +141,16 @@ public Cache(final String name, int size, long entryLifeTime, long entryIdleTime
140
141
*
141
142
* @param name Unique id for this cache.
142
143
* @param size maximal number of elements.
143
- * @param entryLifeTime maximal time in milliseconds .
144
- * @param entryIdleTime maximal idle time in milliseconds .
144
+ * @param entryLifeTime maximal time that an entry allowed to stay in the cache after creation .
145
+ * @param entryIdleTime maximal time that an entry allowed to stay in the cache after last access .
145
146
* @param eventListener {@link CacheEventListener}
146
147
* @param clock {@link Clock} to use
147
148
* <code>timeValue</code> parameter.
148
149
*/
149
- public Cache (final String name , int size , long entryLifeTime , long entryIdleTime ,
150
+ public Cache (final String name , int size , Duration entryLifeTime , Duration entryIdleTime ,
150
151
CacheEventListener <K , V > eventListener , Clock clock ) {
151
152
152
- checkArgument (entryLifeTime >= entryIdleTime , "Entry life time cant be smaller that idle time" );
153
+ checkArgument (entryLifeTime . compareTo ( entryIdleTime ) >= 0 , "Entry life time cant be smaller that idle time" );
153
154
154
155
_name = name ;
155
156
_size = size ;
@@ -159,6 +160,7 @@ public Cache(final String name, int size, long entryLifeTime, long entryIdleTime
159
160
_eventListener = eventListener ;
160
161
_mxBean = new CacheMXBeanImpl <>(this );
161
162
_timeSource = clock ;
163
+ _lastClean = new AtomicReference <>(_timeSource .instant ());
162
164
}
163
165
164
166
/**
@@ -186,12 +188,12 @@ public void put(K k, V v) {
186
188
*
187
189
* @param k key associated with the value.
188
190
* @param v value associated with key.
189
- * @param entryMaxLifeTime maximal life time in milliseconds .
190
- * @param entryIdleTime maximal idle time in milliseconds .
191
+ * @param entryMaxLifeTime maximal time that an entry allowed to stay in the cache after creation .
192
+ * @param entryIdleTime maximal time that an entry allowed to stay in the cache after last access .
191
193
*
192
194
* @throws MissingResourceException if Cache limit is reached.
193
195
*/
194
- public void put (K k , V v , long entryMaxLifeTime , long entryIdleTime ) {
196
+ public void put (K k , V v , Duration entryMaxLifeTime , Duration entryIdleTime ) {
195
197
_log .debug ("Adding new cache entry: key = [{}], value = [{}]" , k , v );
196
198
197
199
long stamp = _accessLock .writeLock ();
@@ -229,8 +231,7 @@ public V get(K k) {
229
231
return null ;
230
232
}
231
233
232
- long now = _timeSource .millis ();
233
- valid = element .validAt (now );
234
+ valid = element .validAt (_timeSource .instant ());
234
235
v = element .getObject ();
235
236
236
237
if ( !valid ) {
@@ -278,7 +279,7 @@ public V remove(K k) {
278
279
try {
279
280
CacheElement <V > element = _storage .remove (k );
280
281
if ( element == null ) return null ;
281
- valid = element .validAt (_timeSource .millis ());
282
+ valid = element .validAt (_timeSource .instant ());
282
283
v = element .getObject ();
283
284
} finally {
284
285
_accessLock .unlock (stamp );
@@ -310,18 +311,18 @@ int size() {
310
311
/**
311
312
* Get maximal idle time until entry become unavailable.
312
313
*
313
- * @return time in milliseconds .
314
+ * @return default amount of an entry's maximal idle time .
314
315
*/
315
- public long getEntryIdleTime () {
316
+ public Duration getEntryIdleTime () {
316
317
return _defaultEntryIdleTime ;
317
318
}
318
319
319
320
/**
320
321
* Get maximal total time until entry become unavailable.
321
322
*
322
- * @return time in milliseconds .
323
+ * @return default amount of an entry's live time .
323
324
*/
324
- public long getEntryLiveTime () {
325
+ public Duration getEntryLiveTime () {
325
326
return _defaultEntryMaxLifeTime ;
326
327
}
327
328
@@ -348,7 +349,7 @@ public void cleanUp() {
348
349
349
350
long stamp = _accessLock .writeLock ();
350
351
try {
351
- long now = _timeSource .millis ();
352
+ Instant now = _timeSource .instant ();
352
353
Iterator <Map .Entry <K , CacheElement <V >>> entries = _storage .entrySet ().iterator ();
353
354
while (entries .hasNext ()) {
354
355
Map .Entry <K , CacheElement <V >> entry = entries .next ();
@@ -386,7 +387,7 @@ public List<CacheElement<V>> entries() {
386
387
return entries ;
387
388
}
388
389
389
- public long lastClean () {
390
+ public Instant lastClean () {
390
391
return _lastClean .get ();
391
392
}
392
393
}
0 commit comments