Re: Caching question

Jonas BonĂ©r <[email protected]> Thu, 17 Apr 2003 07:46:41 +0200
Newsgroups gmane.comp.java.aspectwerkz
Message-ID <[email protected]>
Hi Carlos.

I have not tried anything of this but the first thing that comes to my 
mind is something like this:

Change the FinderCachingAdvice so that the HashMap holding the caches is 
static.

private static Map cache = new StaticBucketMap(1000);

Add a method to the advice similar to this:

public static void remove(final String uuid) {
     // removes the object with the uuid from the cache
}

Then if f.e. your Folder have some methods like 
create/remove/update/move or so then you could create an Advice that 
advises those methods and transparently removes the Folder from the 
cache when these methods are called:

// some advice, that f.e advises the remove or move methods on Folder
...
     public Object execute(final JoinPoint joinPoint) throws Throwable {
     final Object result = joinPoint.proceed();

         MethodJoinPoint jp = (MethodJoinPoint)joinPoint;
         String uuid = ((Identifiable)jp.getTargetObject()).getUuid();
         FinderCachingAdvice.remove(uuid); // or look it up through some 
Registry

         return result;
     }
...

As I said this is the first thing that comes to my mind, might not work, 
I'll have to think some more about this.
Pretty interesting problem.

Sincerely, Jonas.


Carlos Villela wrote:

 > 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(n2) 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
 > _______________________________________________
 > aspectwerkz-developer mailing list
 > [email protected]
 > https://lists.sourceforge.net/lists/listinfo/aspectwerkz-developer
 >
 >


-- 
Jonas
http://freeroller.net/page/jboner






-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf