1 53 54 106 107 package com.Yasna.forum.database; 108 109 import com.Yasna.util.*; 110 111 114 public class DbCacheManager { 115 116 public static int USER_CACHE = 0; 117 public static int USER_ID_CACHE = 1; 118 public static int GROUP_CACHE = 2; 119 public static int GROUP_ID_CACHE = 3; 120 public static int FORUM_CACHE = 4; 121 public static int FORUM_ID_CACHE = 5; 122 public static int THREAD_CACHE = 6; 123 public static int MESSAGE_CACHE = 7; 124 public static int USER_PERMS_CACHE = 8; 125 public static int CATEGORY_CACHE = 9; 126 public static int CATEGORY_ID_CACHE = 10; 127 public static int FORUM_GROUP_CACHE = 11; 128 public static int THREAD_TYPE_ID_CACHE = 12; 129 130 protected Cache[] caches; 131 132 private boolean cacheEnabled = true; 133 134 public DbCacheManager() { 135 int MINUTE = 1000*60; 136 int HOUR = MINUTE*60; 137 138 caches = new Cache[13]; 139 140 caches[USER_CACHE] = new Cache(256*1024, 10*MINUTE); 142 caches[USER_ID_CACHE] = new Cache(128*1024, 10*MINUTE); 143 caches[GROUP_CACHE] = new Cache(128*1024, 1*HOUR); 144 caches[GROUP_ID_CACHE] = new Cache(128*1024, 1*HOUR); 145 caches[FORUM_CACHE] = new Cache(128*1024, 20*MINUTE); 146 caches[FORUM_ID_CACHE] = new Cache(128*1024, 20*MINUTE); 147 caches[THREAD_CACHE] = new Cache(128*1024, 20*MINUTE); 148 caches[MESSAGE_CACHE] = new Cache(512*1024, 1*HOUR); 149 caches[CATEGORY_CACHE] = new Cache(128*1024, 1*HOUR); 150 caches[CATEGORY_ID_CACHE] = new Cache(128*1024, 1*HOUR); 151 caches[FORUM_GROUP_CACHE] = new Cache(128*1024, 1*HOUR); 152 caches[USER_PERMS_CACHE] = new UserPermsCache(256*1024, 2*HOUR); 158 caches[THREAD_TYPE_ID_CACHE] = new Cache(128*1024, 1*HOUR); 159 } 160 161 public Cache getCache(int cacheType) { 162 return caches[cacheType]; 163 } 164 165 public void add(int cacheType, Object key, Cacheable object) { 166 caches[cacheType].add(key, object); 167 } 168 169 public Cacheable get(int cacheType, Object key) { 170 if (!cacheEnabled) { 171 return null; 172 } 173 return caches[cacheType].get(key); 174 } 175 176 public void remove(int cacheType, Object key) { 177 caches[cacheType].remove(key); 178 } 181 182 public void removeUserPerm(Object userID) { 183 Object [] values = caches[USER_PERMS_CACHE].values().toArray(); 184 for (int i=0; i<values.length; i++) { 185 Cache cache = (Cache)((CacheObject)values[i]).object; 186 cache.remove(userID); 187 } 188 } 191 192 public void removeUserPerm(Object userID, Object forumID) { 193 Cache cache = (Cache)caches[USER_PERMS_CACHE].get(forumID); 194 if (cache != null) { 195 cache.remove(userID); 196 } 197 } 200 201 public void clear(int cacheType) { 202 caches[cacheType].clear(); 203 } 206 207 public boolean isCacheEnabled() { 208 return cacheEnabled; 209 } 210 211 public void setCacheEnabled(boolean cacheEnabled) { 212 this.cacheEnabled = cacheEnabled; 213 } 214 } 215 216 221 class UserPermsCache extends Cache { 222 223 public UserPermsCache(int size, long expireTime) { 224 super(size, expireTime); 225 } 226 227 public synchronized Cacheable get(Object key) { 228 Cache subCache = (Cache)super.get(key); 229 if (subCache == null) { 230 subCache = new Cache(2*1024, 30*1000*60); 234 add(key, subCache); 235 } 236 return subCache; 237 } 238 239 public synchronized void remove(Object key) { 240 CacheObject cacheObject = (CacheObject)cachedObjectsHash.get(key); 241 if (cacheObject == null) { 243 return; 244 } 245 cachedObjectsHash.remove(key); 247 cacheObject.lastAccessedListNode.remove(); 249 cacheObject.ageListNode.remove(); 250 cacheObject.ageListNode = null; 252 cacheObject.lastAccessedListNode = null; 253 size -= cacheObject.size; 255 256 ((Cache)cacheObject.object).clear(); 258 } 259 260 268 public int getSize() { 269 int size = 0; 270 Object [] values = values().toArray(); 271 for (int i=0; i<values.length; i++) { 272 Cache cache = (Cache)values[i]; 273 size += cache.getSize(); 274 } 275 return size; 276 } 277 } 278 279 | Popular Tags |