1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.ashkay.strategies;
17
18 import net.sf.ashkay.CacheEntry;
19 import net.sf.ashkay.CachingStrategy;
20
21
22 /***
23 * TimeExpirationCachingStrategy expires objects in the cache after a set
24 * amount of time.
25 * @author <a href="mailto:bangroot@users.sf.net">Dave Brown</a>
26 */
27
28 public class TimeExpirationCachingStrategy implements CachingStrategy
29 {
30 private static final String ENTRY_KEY =
31 "com.cepm_us.util.cache.TimeExpirationCachingStrategy.Timeout";
32
33 private long expirationTime;
34
35 /***
36 * Creates the caching strategy with the specified timeout.
37 * @param timeOut - the time out length for the cache entries
38 */
39 public TimeExpirationCachingStrategy(long timeOut)
40 {
41 expirationTime = timeOut;
42 }
43
44 public CacheEntry prepare(CacheEntry entry)
45 {
46 entry.addProperty(ENTRY_KEY, new Long(System.currentTimeMillis()));
47 return entry;
48 }
49
50 public boolean validate(CacheEntry entry)
51 {
52 boolean val = true;
53 Object temp = entry.getProperty(ENTRY_KEY);
54 if ((temp != null) && (temp instanceof Long))
55 {
56 long cachedTime = ((Long) temp).longValue();
57 long currentTime = System.currentTimeMillis();
58 if (cachedTime + expirationTime <= currentTime)
59 {
60 val = false;
61 }
62
63 }
64 else
65 {
66 val = false;
67 }
68
69 return val;
70 }
71 }