001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/site/tags/sakai_2-4-1/site-impl/impl/src/java/org/sakaiproject/site/impl/SiteCacheImpl.java $
003: * $Id: SiteCacheImpl.java 12713 2006-07-24 04:10:41Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.site.impl;
021:
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.sakaiproject.memory.api.Cache;
026: import org.sakaiproject.memory.api.DerivedCache;
027: import org.sakaiproject.memory.api.MemoryService;
028: import org.sakaiproject.site.api.Group;
029: import org.sakaiproject.site.api.Site;
030: import org.sakaiproject.site.api.SitePage;
031: import org.sakaiproject.site.api.ToolConfiguration;
032:
033: import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
034:
035: /**
036: * <p>
037: * SiteCacheImpl is a cache tuned for Site (and page / tool) access.
038: * </p>
039: */
040: public class SiteCacheImpl implements DerivedCache {
041: /** Map of a tool id to a cached site's tool configuration instance. */
042: protected Map m_tools = new ConcurrentReaderHashMap();
043:
044: /** Map of a page id to a cached site's SitePage instance. */
045: protected Map m_pages = new ConcurrentReaderHashMap();
046:
047: /** Map of a group id to a cached site's Group instance. */
048: protected Map m_groups = new ConcurrentReaderHashMap();
049:
050: /** The base cache. */
051: protected Cache m_cache = null;
052:
053: /**
054: * Construct the Cache. No automatic refresh: expire only, from time and events.
055: *
056: * @param sleep
057: * The number of seconds to sleep between expiration checks.
058: * @param pattern
059: * The "startsWith()" string for all resources that may be in this cache - if null, don't watch events for updates.
060: */
061: public SiteCacheImpl(MemoryService memoryService, long sleep,
062: String pattern) {
063: m_cache = memoryService.newHardCache(sleep, pattern);
064:
065: // setup as the derived cache
066: m_cache.attachDerivedCache(this );
067: }
068:
069: /**
070: * Cache an object
071: *
072: * @param key
073: * The key with which to find the object.
074: * @param payload
075: * The object to cache.
076: * @param duration
077: * The time to cache the object (seconds).
078: */
079: public void put(Object key, Object payload, int duration) {
080: m_cache.put(key, payload, duration);
081: }
082:
083: /**
084: * Test for a non expired entry in the cache.
085: *
086: * @param key
087: * The cache key.
088: * @return true if the key maps to a non-expired cache entry, false if not.
089: */
090: public boolean containsKey(Object key) {
091: return m_cache.containsKey(key);
092: }
093:
094: /**
095: * Get the non expired entry, or null if not there (or expired)
096: *
097: * @param key
098: * The cache key.
099: * @return The payload, or null if the payload is null, the key is not found, or the entry has expired (Note: use containsKey() to remove this ambiguity).
100: */
101: public Object get(Object key) {
102: return m_cache.get(key);
103: }
104:
105: /**
106: * Clear all entries.
107: */
108: public void clear() {
109: m_cache.clear();
110: }
111:
112: /**
113: * Remove this entry from the cache.
114: *
115: * @param key
116: * The cache key.
117: */
118: public void remove(Object key) {
119: m_cache.remove(key);
120: }
121:
122: /**
123: * Access the tool that is part of a cached site, by tool Id.
124: *
125: * @param toolId
126: * The tool's id.
127: * @return The ToolConfiguration that has this id, from a cached site.
128: */
129: public ToolConfiguration getTool(String toolId) {
130: return (ToolConfiguration) m_tools.get(toolId);
131: }
132:
133: /**
134: * Access the page that is part of a cached site, by page Id.
135: *
136: * @param pageId
137: * The page's id.
138: * @return The SitePage that has this id, from a cached site.
139: */
140: public SitePage getPage(String pageId) {
141: return (SitePage) m_pages.get(pageId);
142: }
143:
144: /**
145: * Access the group that is part of a cached site, by group Id.
146: *
147: * @param id
148: * The group id.
149: * @return The Group that has this id, from a cached site.
150: */
151: public Group getGroup(String groupId) {
152: return (Group) m_groups.get(groupId);
153: }
154:
155: /**
156: * {@inheritDoc}
157: */
158: public void notifyCacheClear() {
159: // clear the tool ids
160: m_tools.clear();
161:
162: // clear the pages
163: m_pages.clear();
164:
165: // clear the groups
166: m_groups.clear();
167: }
168:
169: /**
170: * {@inheritDoc}
171: */
172: public void notifyCachePut(Object key, Object payload) {
173: // add the payload (Site) tool ids
174: if (payload instanceof Site) {
175: Site site = (Site) payload;
176:
177: // get the pages and tools, groups and propeties all loaded efficiently
178: site.loadAll();
179:
180: // add the pages and tools to the cache
181: for (Iterator pages = site.getPages().iterator(); pages
182: .hasNext();) {
183: SitePage page = (SitePage) pages.next();
184: m_pages.put(page.getId(), page);
185: for (Iterator tools = page.getTools().iterator(); tools
186: .hasNext();) {
187: ToolConfiguration tool = (ToolConfiguration) tools
188: .next();
189: m_tools.put(tool.getId(), tool);
190: }
191: }
192:
193: // add the groups to the cache
194: for (Iterator groups = site.getGroups().iterator(); groups
195: .hasNext();) {
196: Group group = (Group) groups.next();
197: m_groups.put(group.getId(), group);
198: }
199: }
200: }
201:
202: /**
203: * {@inheritDoc}
204: */
205: public void notifyCacheRemove(Object key, Object payload) {
206: // clear the tool ids for this site
207: if ((payload != null) && (payload instanceof Site)) {
208: Site site = (Site) payload;
209: for (Iterator pages = site.getPages().iterator(); pages
210: .hasNext();) {
211: SitePage page = (SitePage) pages.next();
212: m_pages.remove(page.getId());
213: for (Iterator tools = page.getTools().iterator(); tools
214: .hasNext();) {
215: ToolConfiguration tool = (ToolConfiguration) tools
216: .next();
217: m_tools.remove(tool.getId());
218: }
219: }
220:
221: for (Iterator groups = site.getGroups().iterator(); groups
222: .hasNext();) {
223: Group group = (Group) groups.next();
224: m_groups.remove(group.getId());
225: }
226: }
227: }
228: }
|