01: /*
02: * $Id: LeastRecentlyAccessedEvictionStrategy.java,v 1.1 2005/12/29 05:30:24
03: * jonathanlocke Exp $ $Revision: 460102 $ $Date: 2006-04-02 01:34:49 +0200 (Sun, 02 Apr 2006) $
04: *
05: * ==============================================================================
06: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
07: * use this file except in compliance with the License. You may obtain a copy of
08: * the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15: * License for the specific language governing permissions and limitations under
16: * the License.
17: */
18: package wicket.session.pagemap;
19:
20: import wicket.Page;
21: import wicket.PageMap;
22: import wicket.Session;
23:
24: /**
25: * A simple eviction strategy that evicts the least recently accessed page
26: * version from the given page map.
27: *
28: * @author Jonathan Locke
29: */
30: public class LeastRecentlyAccessedEvictionStrategy implements
31: IPageMapEvictionStrategy {
32: private static final long serialVersionUID = 1L;
33:
34: /** Maximum number of page versions in a page map before evictions start */
35: private int maxVersions;
36:
37: /**
38: * Constructor.
39: *
40: * @param maxVersions
41: * Maximum number of page versions before eviction occurs
42: */
43: public LeastRecentlyAccessedEvictionStrategy(int maxVersions) {
44: if (maxVersions < 1) {
45: throw new IllegalArgumentException(
46: "Value for maxVersions must be >= 1");
47: }
48: this .maxVersions = maxVersions;
49: }
50:
51: /**
52: * @see wicket.session.pagemap.IPageMapEvictionStrategy#evict(wicket.PageMap)
53: */
54: public void evict(final PageMap pageMap) {
55: synchronized (Session.get()) {
56: // Do we need to evict under this strategy?
57: if (pageMap.getVersions() > maxVersions) {
58: // Remove oldest entry from access stack
59: final PageMap.Access oldestAccess = (PageMap.Access) pageMap
60: .getAccessStack().remove(0);
61: final IPageMapEntry oldestEntry = pageMap
62: .getEntry(oldestAccess.getId());
63:
64: // If entry is a page (cannot be null if we're evicting)
65: if (oldestEntry instanceof Page) {
66: Page page = (Page) oldestEntry;
67:
68: // If there is more than one version of this page
69: if (page.getVersions() > 1) {
70: // expire the oldest version
71: page.expireOldestVersion();
72: } else {
73: // expire whole page
74: pageMap.removeEntry(page);
75: }
76: } else {
77: // If oldestEntry is not an instance of Page, then it is some
78: // custom, user-defined IPageMapEntry class and cannot contain
79: // versioning information, so we just remove the entry.
80: if (oldestEntry != null) {
81: pageMap.removeEntry(oldestEntry);
82: }
83: }
84: }
85: }
86: }
87:
88: /**
89: * @see java.lang.Object#toString()
90: */
91: public String toString() {
92: return "[LeastRecentlyAccessedEvictionStrategy maxVersions = "
93: + maxVersions + "]";
94: }
95: }
|