IntroductionFrequently your objects implements a function, which does stateless, but timeconsuming computations. If this function is applied more than once on the same values, a cache memory could help raising the performance. PostSharp lets build a reusable cache object, which could be applied without changing the methods body. DetailsFirst we need a cache memory, which is actually a shared dictionary. The key is build using the input data, so we can easily get the right result out, if there is one instead of calculating it. Each instance of the enhanced object has now access to all calculated values. But be careful, maybe different threads using instances of this object at the same time. Our code must be threadsafe, otherwise unpredictable results will occure. Careful locking of all shared data is needed, but the implementation is straight forward. This implementation is just a sample, there is no build in limitation of the element count in the dictionary and the calculation of the key is quite simple and doesnt care about all situations of real world programming.
|