KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > cache > LazyExpiringCacheEntry


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.util.cache;
20
21 import java.io.Serializable JavaDoc;
22
23
24 /**
25  * A cache entry that is meant to expire in a lazy fashion.
26  *
27  * The way to use this class is to wrap the object you want to cache in an
28  * instance of this class and store that in your cache. Then when you want
29  * to retrieve this entry you must input a last-expired time which can be
30  * compared against the time this entry was cached to determine if the cached
31  * entry is "fresh". If the object is not fresh then we don't return it.
32  *
33  * This essentially allows us to track when an object is cached and then before
34  * we can retrieve that cached object we must compare it with it's last known
35  * invalidation time to make sure it hasn't expired. This is useful because
36  * instead of actively purging lots of cached objects from the cache at
37  * invalidation time, we can now be lazy and just invalidate them when we
38  * actually try to retrieve the cached object.
39  *
40  * This is useful for Roller because we will no longer have to iterate through
41  * the list of cached objects and inspect the keys to figure out what items to
42  * invalidate. Instead we can just sit back and let the items be invalidated as
43  * we try to use them.
44  */

45 public class LazyExpiringCacheEntry implements Serializable JavaDoc {
46     
47     private Object JavaDoc value = null;
48     private long timeCached = -1;
49     
50     
51     public LazyExpiringCacheEntry(Object JavaDoc item) {
52         this.value = item;
53         this.timeCached = System.currentTimeMillis();
54     }
55     
56     
57     /**
58      * Retrieve the value of this cache entry if it is still "fresh".
59      *
60      * If the value has expired then we return null.
61      */

62     public Object JavaDoc getValue(long lastInvalidated) {
63         if(this.isInvalid(lastInvalidated)) {
64             return null;
65         } else {
66             return this.value;
67         }
68     }
69     
70     
71     /**
72      * Determine if this cache entry has expired.
73      */

74     public boolean isInvalid(long lastInvalidated) {
75         
76         return (this.timeCached < lastInvalidated);
77     }
78
79     
80     public long getTimeCached() {
81         return timeCached;
82     }
83     
84 }
85
Popular Tags