Using AOP / PostSharp to implement a function cache

by Tom 7. December 2007 08:48

I just posted this comment on codeproject :

I just discovered this a few hours ago, and I allready wrote a small but very helpfull attribute : a attribute to cache constant but timeconsuming function calls.
If you have a function that takes a lot of time to generate, but the result set is actually the same each time for a certain parameter set, you simply need to define it as follows :
(code is written in VB since we're using vb @ work here)

Module Module1
  Sub Main()
    Console.WriteLine("calling Div2 for i = 1", DoDiv2(1))
    Console.WriteLine("calling Div2 for i = 2", DoDiv2(2))
    Console.WriteLine("calling Div2 for i = 1", DoDiv2(1))
    Console.ReadLine()
   End Sub


  <Cacheable()> _
  Function DoDiv2(ByVal i As Integer) As Double
     Console.WriteLine("running Dodiv2 for " + i.ToString())
     Return i / 2
  End Function
End Module


If you run this code, you will see that the function only gets executed once for parameter value 1; the second time it just searches the cached dictionary. This avoids having the same code all over the project caching calculated values !!! It is a huge timesaver and makes you code much more maintainable...

The attribute itself was written like this :
Imports ps = PostSharp.Laos

<Serializable()> _
<System.AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=False)> _
Public Class CacheableAttribute
  Inherits ps.OnMethodInvocationAspect

  Shared x As New Hashtable

  Public Overrides Sub OnInvocation(ByVal pEventArgs As PostSharp.Laos.MethodInvocationEventArgs)
    Dim o() As Object = pEventArgs.GetArguments()
    Dim i As Integer = 0, obj As Object
    For Each obj In o
      i += obj.GetHashCode()
    Next
    If Not x.Contains(i) Then
      x(i) = pEventArgs.Delegate.DynamicInvoke(o)
    End If
    pEventArgs.ReturnValue = x(i)
  End Sub
End Class


And this in such a short time period. Thanks a lot !!!
BTW(Youve got my 5 on both articles)

 

Bookmark and Share

About Tom

Tom Janssens op LinkedIn

Tom Janssens op twitter

Core bvba RSS

 

Tom Janssens is an independent freelance ICT consultant that has been "into computers" ever since the age of 7.

Typing source code from a book evolved into exploring the limits of coding in procedural, assembly and object-oriented languages.
As he matured in software coding, he started focussing on the problems surrounding software development, and learned that software development is usually about people and interactions first, and about technology second.

Due to his diverse track record he gained insights in a lot of aspects of the software development process. Currently his main focus is on strategic ICT advice, lean product/project development and improving the software development process and architecture.

He avoids ivory-tower-approaches by applying and verifying the applicability of the latest tech buzz in software experiments.

He is also the founder of the following LinkedIn groups:

CQRS Professional
BDD Professional
Asp.Net MVC professional

More info about Tom and his company...


Advertisement

Forget all your SCRUM -, Kanban - and other Agile and Lean certificates

Here is the only true AGILE and LEAN certificate you will ever need:

The Creative Recursive Analysis Process Certificate
(CRAP Certificate for short)

More info can be found at the official CRAP certificate website:
http://bit.ly/CRAPCertificate