KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > impl > DbCacheManager


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.impl;
26
27 import org.nemesis.forum.util.cache.Cache;
28 import org.nemesis.forum.util.cache.CacheObject;
29 import org.nemesis.forum.util.cache.Cacheable;
30
31 /**
32  * Central cache management of all caches.
33  */

34 public class DbCacheManager {
35
36     public static int USER_CACHE = 0;
37     public static int USER_ID_CACHE = 1;
38     public static int GROUP_CACHE = 2;
39     public static int GROUP_ID_CACHE = 3;
40     public static int FORUM_CACHE = 4;
41     public static int FORUM_ID_CACHE = 5;
42     public static int THREAD_CACHE = 6;
43     public static int MESSAGE_CACHE = 7;
44     public static int USER_PERMS_CACHE = 8;
45
46     protected Cache[] caches;
47
48     private boolean cacheEnabled = true;
49
50     public DbCacheManager() {
51         int MINUTE = 1000 * 60;
52         int HOUR = MINUTE * 60;
53
54         caches = new Cache[9];
55
56         //Initialize all cache structures
57
caches[USER_CACHE] = new Cache(256 * 1024, 6 * HOUR);
58         caches[USER_ID_CACHE] = new Cache(128 * 1024, 6 * HOUR);
59         caches[GROUP_CACHE] = new Cache(128 * 1024, 6 * HOUR);
60         caches[GROUP_ID_CACHE] = new Cache(128 * 1024, 6 * HOUR);
61         caches[FORUM_CACHE] = new Cache(128 * 1024, 6 * HOUR);
62         caches[FORUM_ID_CACHE] = new Cache(128 * 1024, 6 * HOUR);
63         caches[THREAD_CACHE] = new Cache(128 * 1024, 6 * HOUR);
64         caches[MESSAGE_CACHE] = new Cache(512 * 1024, 6 * HOUR);
65         //The user permissions cache is a special one. It's actually a Cache
66
//of Cache objects. Each of the cache objects in the main cache
67
//corresponds to a particular forum, and is used to cache the
68
//permissions that a user has for a forum. In order to handle this
69
//requirement, we use a special subclass of Cache.
70
caches[USER_PERMS_CACHE] = new UserPermsCache(256 * 1024, 24 * HOUR);
71     }
72
73     public Cache getCache(int cacheType) {
74         return caches[cacheType];
75     }
76
77     public void add(int cacheType, Object JavaDoc key, Cacheable object) {
78         caches[cacheType].add(key, object);
79     }
80
81     public Cacheable get(int cacheType, Object JavaDoc key) {
82         if (!cacheEnabled) {
83             return null;
84         }
85         return caches[cacheType].get(key);
86     }
87
88     public void remove(int cacheType, Object JavaDoc key) {
89         caches[cacheType].remove(key);
90         //when cache becomes distributed, we'd send out an expire message
91
//here to all other yazd servers.
92
}
93
94     public void removeUserPerm(Object JavaDoc userID) {
95         Object JavaDoc[] values = caches[USER_PERMS_CACHE].values().toArray();
96         for (int i = 0; i < values.length; i++) {
97             Cache cache = (Cache) ((CacheObject) values[i]).object;
98             cache.remove(userID);
99         }
100         //when cache becomes distributed, we'd send out an expire message
101
//here to all other yazd servers.
102
}
103
104     public void removeUserPerm(Object JavaDoc userID, Object JavaDoc forumID) {
105         Cache cache = (Cache) caches[USER_PERMS_CACHE].get(forumID);
106         if (cache != null) {
107             cache.remove(userID);
108         }
109         //when cache becomes distributed, we'd send out an expire message
110
//here to all other yazd servers.
111
}
112
113     public void clear(int cacheType) {
114         caches[cacheType].clear();
115         //when cache becomes distributed, we'd send out an expire message
116
//here to all other yazd servers.
117
}
118
119     public boolean isCacheEnabled() {
120         return cacheEnabled;
121     }
122
123     public void setCacheEnabled(boolean cacheEnabled) {
124         this.cacheEnabled = cacheEnabled;
125     }
126 }
127
128 /**
129  * Special purpose Cache to hold all of the different user permission cache
130  * objects. The main feature is that new caches are automatically created so
131  * that calling get() never returns null.
132  */

133 class UserPermsCache extends Cache {
134
135     public UserPermsCache(int size, long expireTime) {
136         super(size, expireTime);
137     }
138
139     public synchronized Cacheable get(Object JavaDoc key) {
140         Cache subCache = (Cache) super.get(key);
141         if (subCache == null) {
142             //cache has expired, or is not there, so put a new one in there.
143
//Cache objects only need to last as long as a user's session
144
//does. Half an hour is a reasonable amount of time for this.
145
subCache = new Cache(2 * 1024, 30 * 1000 * 60);
146             add(key, subCache);
147         }
148         return subCache;
149     }
150
151     public synchronized void remove(Object JavaDoc key) {
152         CacheObject cacheObject = (CacheObject) cachedObjectsHash.get(key);
153         //If the object is not in cache, stop trying to remove it.
154
if (cacheObject == null) {
155             return;
156         }
157         //remove from the hash map
158
cachedObjectsHash.remove(key);
159         //remove from the cache order list
160
cacheObject.lastAccessedListNode.remove();
161         cacheObject.ageListNode.remove();
162         //remove references to linked list nodes
163
cacheObject.ageListNode = null;
164         cacheObject.lastAccessedListNode = null;
165         //removed the object, so subtract its size from the total.
166
size -= cacheObject.size;
167
168         //Finally, clear the sub-cache to make sure memory is released.
169
((Cache) cacheObject.object).clear();
170     }
171
172     /**
173      * Returns the current size in bytes of the cache. The base getSize() method
174      * does not work correctly because the sub-caches are empty when we first
175      * add them rather than the normal cache assumption that objects are near
176      * the size that they will always be.
177      *
178      * @return the size of the cache in bytes.
179      */

180     public int getSize() {
181         int size = 0;
182         Object JavaDoc[] values = values().toArray();
183         for (int i = 0; i < values.length; i++) {
184             Cache cache = (Cache) values[i];
185             size += cache.getSize();
186         }
187         return size;
188     }
189 }
190
Popular Tags