Caching question
Carlos Villela <[email protected]> Wed, 16 Apr 2003 02:28:41 -0300
| Newsgroups | gmane.comp.java.aspectwerkz |
|---|---|
| Organization | Bluebox Technologies |
| Message-ID | <[email protected]> |
Hi folks!
After reading about AOP being extremely good for caching objects, I've
decided I should give it a try on some expensive O(n^2) methods I have
here. It's a very AOP-newbie question, I'll understand if you want to
ignore it or just tell me to RTFM :) - but here comes an important
problem: how to invalidate the cache in some scenarios? Here's some code
(i've removed some assertions to clear things out a little):
/** Iterates over the root folder, and if the folder is not found,
recurses through subfolders to find the uuid */
public static Folder findFolder(String uuid) {
Folder root = ((HasRootFolder) new EntryPoint()).getRootFolder();
Folder f = findFolder(uuid, root);
if (f == null) {
List folders = ((HasFolders) root).getFolders();
if (folders == null)
return null;
for (Iterator i = folders.iterator(); i.hasNext();) {
f = findFolder(uuid, (Folder) i.next());
if (f != null)
break;
}
}
return f;
}
/** Recurses through subfolders to find a Folder with a given uuid */
public static Folder findFolder(String uuid, Folder folder) {
List folders = ((HasFolders) folder).getFolders();
if (folders == null)
return null;
for (Iterator i = folders.iterator(); i.hasNext();) {
Folder f = (Folder) i.next();
if (((Identifiable) f).getUuid().equals(uuid)) {
return f;
}
}
return null;
}
Ok, I can cache these methods with a simple advice (again, assertions
removed):
public class FinderCachingAdvice extends MethodAdvice {
private Map cache = new StaticBucketMap(1000);
public Object execute(final JoinPoint joinPoint) throws Throwable {
String uuid = (MethodJoinPoint) joinPoint.getParameters()[0].toString();
final Long hash = new Long.parseLong(uuid);
final Object cachedResult = cache.get(hash);
// if we have a cached result; return the cache
if (cachedResult != null) return cachedResult;
// else, proceed with the method invocation and store the result in the cache
final Object result = joinPoint.proceed();
cache.put(hash, result);
return result;
}
}
Then, I get to the problem: when do I know I need to invalidate the
cache for a given UUID? When the object is moved in the folder structure
or it's deleted, sure. But how do to it with aspects? Any enlightening
ideas? :)
[]'s
-cv
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf