Postsharp & Cache engine

Technical questions about PostSharp Laos.

Postsharp & Cache engine

Postby Pierrus on Tue Apr 15, 2008 4:37 pm

Hello everyone,

Postsharp and AOP are great! I just implemented a cache engine for a Web application, on the basis of the example Gaël provided in VB, and it just works fine.

However, I have another request for you guys. The cache engine is cool, but what if I want some method calls to bypass the cache engine?

For instance, you have a website with a front and a backoffice that run under the same application. You want the front office to use the cache, but you want the backoffice to always bypass the cache engine in order to always display data freshly fetched from the database. In other word, I need a mean for aspects to choose, I don't know on what basis, if the data should be retrieved from the cache or if it should be bypasses.

Got any ideas? I maybe thought about using Http Context (by injecting some data in it before my method call), but I don't think it's a clean solution.

See you soon!
Pierre Murasso
Pierrus
 
Posts: 8
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Pierre Murasso

Re: Postsharp & Cache engine

Postby gfraiteur on Tue Apr 15, 2008 4:49 pm

Hi,

What you can do in that case is an OnMethodInvocation aspect with call-site weaving (the call instruction will be intercepted), and apply this aspect to the frontoffice assembly.

It will work, but it is not that elegant.

If the behavior depends on the calling context, you could also put the context parameters in a thread-static variable. You can eventually make an aspect that configures the context (like an EnableCacheAspect).

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 674
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Postsharp & Cache engine

Postby Pierrus on Tue Apr 15, 2008 5:37 pm

Thanks Gael.

I don't think that we can filter on an assembly basis as the front and the back runs with the same assembly.

But we could use the context (for instance the HttpContext). However, the calling instance should inject data in the context, and I don't think that's very clean.

Another idea: what do you think about using the StackTrace to bypass the cache according to the calling instance/method.

Code: Select all
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(2);
MethodBase methodBase = stackFrame.GetMethod();
System.Web.HttpContext.Current.Response.Write(methodBase.Name);


The cache aspect could get a pointer to the calling method/instance and choose whether to return a value from the cache or not on this basis.

Thanks!
Pierre Murasso
Pierrus
 
Posts: 8
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Pierre Murasso

Re: Postsharp & Cache engine

Postby gfraiteur on Tue Apr 15, 2008 5:43 pm

But we could use the context (for instance the HttpContext). However, the calling instance should inject data in the context, and I don't think that's very clean.


Why not? You can encapsulate this behavior as an aspect. For instance:

Code: Select all

public static class TheLayer
{
  [Cacheable]
  public static int TheMethod() { return 0; }
}

[Context(CacheEnabled=true)]
public class FrontOffice
{
  public void MyMethod()
  {
   int a = TheLayer.TheMethod();
  }
}

public class BackOffice
{
  public void MyMethod()
  {
   int a = TheLayer.TheMethod();
  }
}
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 674
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Postsharp & Cache engine

Postby Pierrus on Wed Apr 16, 2008 11:01 am

Wow! Good idea!

It will work fine with all the libraries I compile myself (i.e.: a generic handler I created in a DLL). But will I be able to inject this kind of aspect in an ASPX page (i.e. I want cache to be disabled on a particular aspx page)?

Actually, I never thought about adding aspects to an ASPX page, is it possible?
Pierre Murasso
Pierrus
 
Posts: 8
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Pierre Murasso

Re: Postsharp & Cache engine

Postby gfraiteur on Wed Apr 16, 2008 11:06 am

Yes, it can (or could) work also, but it's cumbersome to deploy on the server (it's better to use it with precompiled web sites).

See http://www.postsharp.org/blog/2008/02/e ... spnet.html.

Yep, it's experimental.
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 674
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Postsharp & Cache engine

Postby Pierrus on Wed Apr 16, 2008 3:09 pm

Thanks Gael, I'm gonna try it rightaway.

Is it available for download? I can access the Postsharp.Aspnet wiki page, but cannot find a download link.
Pierre Murasso
Pierrus
 
Posts: 8
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Pierre Murasso

Re: Postsharp & Cache engine

Postby gfraiteur on Wed Apr 16, 2008 3:21 pm

You have to use an SVN client. As I said, it's experimental, so support is poor. Your feedback is welcome!

G
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 674
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org


Return to PostSharp Laos


cron