Knowledge is power. We love to share it.

News related to Mono products, services and latest developments in our community.

iruzak

Simple Azure Cache Provider for Monox

12/06/2013Categories: MonoX, ASP.NET, Azure
This blog post explains all the steps involved in creation of an Azure Cache Provider for your MonoX-based site or application.
Let's start with a basic question: what is Azure Cache? Let's see how Microsoft answers it:
Windows Azure Cache is a distributed, in-memory, scalable solution that enables you to build highly scalable and responsive applications by providing super-fast access to data.
Windows Azure Cache is available as a managed service, Cache Service (Preview). Or create and manage yourself using the Azure SDK (In-Role Cache).
More information on Windows Azure Cache can be found by following this link.
Now that we know what Azure Cache is, let's make sure that we meet all prerequisites before we can start writing our own Cache Provider code.

Step 1. Enabling Cache service on Azure

Log in into your Azure account at https://manage.windowsazure.com/. Click on New -> Data Services -> Cache (Preview) -> Quick Create.
Enter a few basic information about your new Cache Service.
Just click on "Create a new cache" and wait for Azure to do its job, and you'll end up with something like this:
Before we go to next step, we just need to take a look at one additional screen:

Step 2. Visual studio requirements

Go ahead and fire up Visual Studio. We will now install a few NuGet packages. Right click on your solution in the Solution Explorer and select "Manage NuGet Packages for Solution...".

Search for Azure Cache NuGet and install it.

This NuGet will install and include all .dlls and configuration data needed to use Azure Cache service. If we open our web.config file we can find new entries looking like this:
<dataCacheClients>
  <dataCacheClient name="default">
    <!--To use the in-role flavor of Windows Azure Caching,
        set identifier to be the cache cluster role name -->
    <!--To use the Windows Azure Caching Service,
        set identifier to be the endpoint of the cache cluster -->
    <autoDiscover isEnabled="true" identifier="[Cache role name or Service Endpoint]" />
    <!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->
    <!--Use this section to specify security settings for connecting to your cache.
        This section is not required if your cache is hosted on a role that is a part
        of your cloud service. -->
    <!--<securityProperties mode="Message" sslEnabled="false">
      <messageSecurity authorizationInfo="[Authentication Key]" />
    </securityProperties>-->
  </dataCacheClient>
</dataCacheClients>
You will need to paste your endpoint data along with your authentication key in this section.

Step 3. Writing our CacheProvider code

Let's finally write some code. First take a quick look at MonoX Cache provider. The default MonoX cache provider inherits ICacheProvider, so we'll also start there.
It will look like this:
namespace MonoSoftware.Web.Caching
{
    public interface ICacheProvider
    {
        CacheItemPriorityLevel Priortiy { get; set; }
        int Timeout { get; set; }
  
        T Get<T>(string key);
        object Get(string key);
        void Remove(string key);
        void RemoveAll(string key);
        void Store(string key, object data);
    }
}
Our SimpleAzureCacheProvider will inherit this interface and we will implement its properties and methods.
001.namespace MyProject
002.{
003.    public class SimpleAzureCacheProvider : ICacheProvider
004.    {
005.  
006.        #region Fields
007.        protected static object padLock = new object();
008.        protected static object padLockInit = new object();
009.        #endregion
010.  
011.        #region Properties
012.        public string CacheName { get; set; }
013.  
014.        protected static volatile DataCacheFactory _factory = null;
015.        protected static volatile DataCache _cache = null;
016. 
017.        protected virtual DataCache Cache
018.        {
019.            get
020.            {
021.                if (_cache == null)
022.                {
023.                    lock (padLockInit)
024.                    {
025.                        if (_cache == null)
026.                        {
027.                            DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
028.                            _factory = new DataCacheFactory(configuration);                
029.                            _cache = _factory.GetCache("cachename");
030.                        }
031.                    }
032.                }
033.                return _cache;
034.            }
035.            private set { _cache = value; }
036.        }
037.  
038.        private int _timeout = 10;
039.        public int Timeout
040.        {
041.            get { return _timeout; }
042.            set { _timeout = value; }
043.        }
044.  
045.        private CacheItemPriorityLevel _priortiy = CacheItemPriorityLevel.AboveNormal;
046.        public CacheItemPriorityLevel Priortiy
047.        {
048.            get { return _priortiy; }
049.            set {  _priortiy = value; }
050.        
051.        #endregion
052.  
053.        #region Constructor
054.        public AzureCacheProvider()
055.        {
056.  
057.        }
058.        #endregion
059.  
060.        #region Methods
061.  
062.        public T Get<T>(string key)
063.        {
064.            object item = Get(key);
065.            if (item != null)
066.            {
067.                return item is T ? (T)item : default(T);
068.            }
069.            else
070.                return default(T);
071.        }
072.  
073.        public object Get(string key)
074.        {
075.            return Cache.Get(key);
076.        }
077.  
078.        public void Store(string key, object data)
079.        {
080.            if (data != null)
081.                Cache.Put(key, data);
082.        }
083.  
084.        public void Remove(string key)
085.        {
086.            lock (padLock)
087.            {
088.                Cache.Remove(key);
089.            }
090.        }
091.  
092.        public void RemoveAll(string key)
093.        {
094.            lock (padLock)
095.            {
096.                Remove(key);
097.            }
098.        }
099.  
100.        #endregion
101.  
102.    }
103.}
As you can see, the implementation part is really simple. If you need more information on Azure Cache, please follow this link.
Rated 5.00, 3 vote(s).