15

The application we are building sends out different kind of emails regularly. I stored the email templates in an Azure blob storage and the methods responsible for sending emails pull the appropriate email templates from there. I want the templates to be outside of the hosted service in case I want to update it, I can do that simply by uploading new templates to the blob.

The problem I'm having, from performance and cost perspective, is that the email templates rarely change within a 24hr period. So caching the method in a way akin to [OutputCache(Duration = duration, VaryByParam = "id")] in ASP.NET MVC will be an ideal solution in order to increase the worker role performance. How to do this is now a problem. I learnt of PostSharp but our budget didn't take PostSharp's licencing fee into consideration from the beginning!

Any other free alternatives? Thanks for helping out.

svick
  • 236,525
  • 50
  • 385
  • 514
olatunjee
  • 351
  • 2
  • 3
  • 12
  • I might be missing something, so I'll ask "why do you think postsharp would help you solve this problem?" – JerKimball Jan 02 '13 at 01:49
  • There is a thread [here](http://stackoverflow.com/questions/4929540/is-there-anyway-to-cache-function-method-in-c-sharp) suggesting PostSharp as viable option. I've searched the net. Would appreciate it if you have any other option. – olatunjee Jan 02 '13 at 02:08
  • Ok, I got ya now - you're wanting to use it for AOP; your caching logic is your "cross cutting concern". Yeah, PostSharp might be a viable solution here. I'm not aware of any freeware alternatives, however. Lemme poke around a bit. – JerKimball Jan 02 '13 at 02:12
  • It *appears* as if you can achieve the same end-result with some clever usage of Mono.Cecil, but I'm not in a position to confirm this, hence no answer - but something to look into? – JerKimball Jan 02 '13 at 02:17
  • I'll give it a check right away. – olatunjee Jan 02 '13 at 02:24
  • 1
    Postsharp does have a free version, have you tried that? – svick Jan 02 '13 at 03:28
  • Wasn't there an AOP alternative as part of the Castle Project? http://docs.castleproject.org/Windsor.Interceptors.ashx – cory-fowler Jan 02 '13 at 03:56
  • [CodeCop](http://getcodecop.com/) is a library that builds on top of JSON to provide a fast unobtrusive way to write method interceptors for your .NET apps. [Here](https://bitbucket.org/codecop_team/codecop/wiki/Home) is a wiki. You can try this :) – Dumitru Chirutac Sep 03 '15 at 14:24

6 Answers6

3

PostSharp Starter Edition is free and would meet your requirements.

Gael Fraiteur
  • 6,759
  • 2
  • 24
  • 31
  • 20
    It was before the max 10 class per project restriction. – Carra Nov 07 '16 at 09:08
  • I m trying to create a postsharp alternative based on MrAdvice. Maybe you can give a try. You can use MrAdvice.Sharp and please do not hesitate to send me some feedback. – MSK Dec 11 '18 at 09:24
  • 5
    Remember fody, best open source alternative: https://github.com/Fody/Fody – Jalal Mar 26 '19 at 18:55
2

I have implemented method level caching in the past, using the following combination:

  1. Autofac as IoC container
  2. Autofac's MVC3 integration package
  3. Autofac's DynamicProxy2 (castle) integration for interception support
  4. Custom Attribute to decorate classes that require caching support
  5. Custom Interceptor to add method level caching

The custom attribute and interceptor is quite easy to set up. The main problem with method level caching is, I believe, how you determine cache hits and misses in an optimal, yet precise way.

In my case it needed to be generic (support any type of method call and parameters), so I had to create a flexible way to hash all method parameter values in order to distinguish one call from another. But in your case, this could actually be a very specific interceptor, that already knows the structure of your method call, which would make things a lot easier.

Now about the actual caching, you can take advantage of .NET's caching support, available in the System.Runtime.Caching namespace, which already provides a MemoryCache if that's suitable for you.

Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58
1

I've used SNAP a few times. It's free and very easy to setup and use with a number of IoC containers

MarkG
  • 1,859
  • 1
  • 19
  • 21
0

Have you considered using local storage resources to pull the templates out of storage? You could configure an interval in the Run method of the RoleEntryPoint.

[Update]

I might not have been clear above. The template should be stored in Blob Storage. Local Storage would be used to cache a copy of the template locally on each instance of the service. If you store the ETag in a separate file this will allow you to verify the template has changed before transferring the blob i.e.:

  • welcome.tpl
  • welcome.tag.tpl

In the RoleEntryPoint, read the etag from the *.tag.tpl file. Submitting your request against blob storage with a conditional headers passing the Etag with an If-None-Match access condition. If the blob hasn't been updated it will return an HTTP 304 Not Modified response code, if it was updated, the blob will be downloaded.

[/Update]

cory-fowler
  • 4,020
  • 2
  • 18
  • 29
  • Thanks. I guess when there is need to update/change the templates, we'll have to redeploy the application should we opt for local storage. We are trying to avoid this. – olatunjee Jan 02 '13 at 02:12
  • Store the templates in blob storage, but have the RoleEntryPoint keep the cached copy which is in localstorage up to date. Any updates made to the templates in blob storage will be updated as soon as the thread wakes up in the Run method and the code is executed to overwrite the copy in localstorage. – cory-fowler Jan 02 '13 at 03:26
0

If you just want AOP functionality, you can use something like Ninject Interception.

http://codepyre.com/2010/03/using-ninject-extensions-interception-part-1-the-basics/

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

You can try to use KingAOP - easy for such tasks. The main difference is:
1) generating code in runtime without wasting additional compile time.
2) based on "dynamic" - it's mean that your code will generated via official C# compiler.

It's open source project: https://github.com/AntyaDev/KingAOP

Take a look for article:
http://www.codeproject.com/Tips/624586/Introducing-the-KingAOP-Framework-Part-1

Lubch
  • 57
  • 1
  • 9