1 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 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 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 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 key, Cacheable object) { 78 caches[cacheType].add(key, object); 79 } 80 81 public Cacheable get(int cacheType, Object key) { 82 if (!cacheEnabled) { 83 return null; 84 } 85 return caches[cacheType].get(key); 86 } 87 88 public void remove(int cacheType, Object key) { 89 caches[cacheType].remove(key); 90 } 93 94 public void removeUserPerm(Object userID) { 95 Object [] 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 } 103 104 public void removeUserPerm(Object userID, Object forumID) { 105 Cache cache = (Cache) caches[USER_PERMS_CACHE].get(forumID); 106 if (cache != null) { 107 cache.remove(userID); 108 } 109 } 112 113 public void clear(int cacheType) { 114 caches[cacheType].clear(); 115 } 118 119 public boolean isCacheEnabled() { 120 return cacheEnabled; 121 } 122 123 public void setCacheEnabled(boolean cacheEnabled) { 124 this.cacheEnabled = cacheEnabled; 125 } 126 } 127 128 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 key) { 140 Cache subCache = (Cache) super.get(key); 141 if (subCache == null) { 142 subCache = new Cache(2 * 1024, 30 * 1000 * 60); 146 add(key, subCache); 147 } 148 return subCache; 149 } 150 151 public synchronized void remove(Object key) { 152 CacheObject cacheObject = (CacheObject) cachedObjectsHash.get(key); 153 if (cacheObject == null) { 155 return; 156 } 157 cachedObjectsHash.remove(key); 159 cacheObject.lastAccessedListNode.remove(); 161 cacheObject.ageListNode.remove(); 162 cacheObject.ageListNode = null; 164 cacheObject.lastAccessedListNode = null; 165 size -= cacheObject.size; 167 168 ((Cache) cacheObject.object).clear(); 170 } 171 172 180 public int getSize() { 181 int size = 0; 182 Object [] 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 |