KickJava   Java API By Example, From Geeks To Geeks.

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


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 expires.
26  *
27  * We use this class to wrap objects being cached and associate a timestamp
28  * and timeout period with them so we can know when they expire.
29  */

30 public class ExpiringCacheEntry implements Serializable JavaDoc {
31     
32     private Object JavaDoc value;
33     private long timeCached = -1;
34     private long timeout = 0;
35     
36     
37     public ExpiringCacheEntry(Object JavaDoc value, long timeout) {
38         this.value = value;
39         
40         // make sure that we don't support negative values
41
if(timeout > 0) {
42             this.timeout = timeout;
43         }
44         
45         this.timeCached = System.currentTimeMillis();
46     }
47     
48     
49     public long getTimeCached() {
50         return this.timeCached;
51     }
52     
53     
54     public long getTimeout() {
55         return this.timeout;
56     }
57     
58     
59     /**
60      * Retrieve the value of this cache entry.
61      *
62      * If the value has expired then we return null.
63      */

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

76     public boolean hasExpired() {
77         
78         long now = System.currentTimeMillis();
79         
80         return ((this.timeCached + this.timeout) < now);
81     }
82     
83 }
84
Popular Tags