For anyone wanting to build a cache for an application, there are several guidelines(may be rules) you want to beware of and more than likely abid by.
Firstly, more than likely you are going to have an in-memory representation of the cache as well as a persisted format if the cache is to survive application restarts. Now an in-memory representation is more than likely going to be a hash-table - especially if you want acceptable retrieval time. Now this is where 'GetHashCode' comes into play and the importance of this is explained perfectly by Eric Lippert,
Secondly, and this follows on from a statement in Eric's post - 'the integer returned by GetHashCode should never change', if you're using the internal state of a class to calculate the hash code, you can't allow that state to change overtime. So this means you are more than likely going to have create a copy of the instance before using it as a 'key' when adding to the cache, because this will then negate this issue,
Thirdly, the data in the cache will be become stale at some point, so expirying data out of the cache will be a requirement, a cache is not a replacement for permanent persistence store like a database or files etc,
Finally, the cache should be transparent to the application, what I mean by this is if the cache fails during execution the only observable difference should be in application performance, the application should still be fully functional. This means if an exception occurs inside the cache it shouldn't be propergated to the host application.
So when we defined the cache providers in WP7Contrib we added an extra interface to the 'Common' project - ICloneable<T>. This is there to help define how an object will be copied\cloned, it defines methods for both shallow and deep copying. We use this interface for any class we wish to use as a 'key' when adding to a cache provider.
Firstly, more than likely you are going to have an in-memory representation of the cache as well as a persisted format if the cache is to survive application restarts. Now an in-memory representation is more than likely going to be a hash-table - especially if you want acceptable retrieval time. Now this is where 'GetHashCode' comes into play and the importance of this is explained perfectly by Eric Lippert,
Secondly, and this follows on from a statement in Eric's post - 'the integer returned by GetHashCode should never change', if you're using the internal state of a class to calculate the hash code, you can't allow that state to change overtime. So this means you are more than likely going to have create a copy of the instance before using it as a 'key' when adding to the cache, because this will then negate this issue,
Thirdly, the data in the cache will be become stale at some point, so expirying data out of the cache will be a requirement, a cache is not a replacement for permanent persistence store like a database or files etc,
Finally, the cache should be transparent to the application, what I mean by this is if the cache fails during execution the only observable difference should be in application performance, the application should still be fully functional. This means if an exception occurs inside the cache it shouldn't be propergated to the host application.
So when we defined the cache providers in WP7Contrib we added an extra interface to the 'Common' project - ICloneable<T>. This is there to help define how an object will be copied\cloned, it defines methods for both shallow and deep copying. We use this interface for any class we wish to use as a 'key' when adding to a cache provider.
Comments
Post a Comment