01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
22:
23: /**
24: * A cache entry that expires.
25: *
26: * We use this class to wrap objects being cached and associate a timestamp
27: * and timeout period with them so we can know when they expire.
28: */
29: public class ExpiringCacheEntry implements Serializable {
30:
31: private Object value;
32: private long timeCached = -1;
33: private long timeout = 0;
34:
35: public ExpiringCacheEntry(Object value, long timeout) {
36: this .value = value;
37:
38: // make sure that we don't support negative values
39: if (timeout > 0) {
40: this .timeout = timeout;
41: }
42:
43: this .timeCached = System.currentTimeMillis();
44: }
45:
46: public long getTimeCached() {
47: return this .timeCached;
48: }
49:
50: public long getTimeout() {
51: return this .timeout;
52: }
53:
54: /**
55: * Retrieve the value of this cache entry.
56: *
57: * If the value has expired then we return null.
58: */
59: public Object getValue() {
60: if (this .hasExpired()) {
61: return null;
62: } else {
63: return this .value;
64: }
65: }
66:
67: /**
68: * Determine if this cache entry has expired.
69: */
70: public boolean hasExpired() {
71:
72: long now = System.currentTimeMillis();
73:
74: return ((this.timeCached + this.timeout) < now);
75: }
76:
77: }
|