Updates to the CacheAdapter Package

Published Sunday, August 21, 2011 2:12 PM

Note: This post was originally going to detail the changes from 2.0 to 2.1, however in between the time of release and this post, I released 2.2 so this post will detail all the changes up to 2.2.

In the last update to my CacheAdapter library (you can view it here), I mentioned the support for memcached cache engine. That brought the version to 2.0.

As previously mentioned, you can get this package from here.

I have recently updated the version of the library to 2.1 2.2 which now includes the following changes:

  • The ability provide specific configuration data to a particular cache engine without introducing a bunch of specific configuration tags that are only relevant to a particular cache mechanism.
    • Addition of a CacheSpecificData configuration element.
  • The addition of 2 new simpler API methods to get data from the cache.
    • T Get<T>(DateTime absoluteExpiryDate, Func<T> getData)
    • T Get<T>(TimeSpan slidingExpiryWindow, Func<T> getData)

Notice that there is no cache key required to be specified. It is auto generated from the Func<T> delegate. (Thanks to Corneliu for this great idea). So you can write really simple code to get data from the cache like so:

var myData = cacheProvider.Get<SomeData>(DateTime.Now.AddSeconds(2), () =>
                                                                          {
                                                                             var somedata = new SomeData();
                                                                             // populate data from somewhere (db, code etc..)
                                                                             return somedata;
                                                                          });
  • Simplification of the interface
    • Replacing the GetDataToCacheDelegate<T>() delegate with a Func<T>
    • Removal of the bool addToPerRequestCache = false parameter from the Get<T> methods.

So why the new configuration element? Well, previous versions of this library supported Windows Azure AppFabric however it was only in a non Azure scenario. When Windows AppFabric is used within Azure, a security mode and authentication key is required. This is easily achieved with config, but I wanted a way that did not introduce redundant elements that were not applicable to other cache mechanisms AND that may be useful for other cache mechanisms if they require it. Basically, a generic way of providing configuration data to cache mechanisms, that could be used by any cache mechanism and not pollute the configuration with lots of items specific to only 1 method.

To that end, this release has introduced a “CacheSpecificData” configuration element as shown below:

<setting name="CacheSpecificData" serializeAs="String">
  <value>UseSsl=false;SecurityMode=Message;MessageSecurityAuthorizationInfo=your_secure_key_from_azure_dashboard</value>
</setting>

Currently, this element is only of use to AppFabric cache mechanism, but as support for more cache types grow, this item will be used more and more.

This element is a simple series of key/value pairs that are separated by a semi-colon. In the example above, the following values are defined:

UseSsl = false

SecurityMode = Message

MessageSecurityAuthorizationInfo = your_secure_key_from_azure_dashboard

This equates to the following configuration for Windows AppFabric in Azure (Specifically the <SecurityProperties> element):

<dataCacheClients>
  <dataCacheClient name="default">
    <hosts>
      <host name="somehost.com" cachePort="22233" />
    </hosts>
    <securityProperties mode="Message">
      <messageSecurity authorizationInfo="your_authorisation_key"></messageSecurity>
    </securityProperties>
  </dataCacheClient>
</dataCacheClients>

There are also a few bug fixes and code cleanup in there. Thanks to Corneliu Tusnea (creator of OneSaas) for submitting some really useful changes and bug fixes.

Hope you enjoy the new features.

Go get the Nuget package from here.

Or download the source code from here.

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

Syndication