There are situations which when arises we need to store the cache objects separately other then web-Server and Sql server.It is during such situtations decision of storing the cache objects on Memcached server comes into our mind and in C# world Enyim caching is one of the most matured api available for storing and retrieving the objects from Memcached server.
Let's start with it for working with MemCached we will first need the MemCached Server.
Links to download the memcache server are :
As you can see in the above code List of contact is first converted to bytearray object and then stored in the Memcached server with the key "ContactList",once the list is stored in the cache as bytearray the same can retrieved using the key and can be converted from bytearray to object.
Hope it would help someone :)
Let's start with it for working with MemCached we will first need the MemCached Server.
Links to download the memcache server are :
Once you have downloaded the files,you can either run MemCached as console application or install it as a Windows service(This is the recommended option).In order to install it as a service,run the following from the command line
C:\Path\memcached -d install
we will need client api which can store and retrieve objects or the one who can interact with the server.
For C# there are two most known client api's available
- Enyim Caching
- BeIT
Well I prefer Enyim caching as it is more mature then other.So for downloading Enyim caching api you can go to nugget manager and type Enyim caching and add it to your project.I am creating a sample console application for the same as shown below
Fig:1.0
Fig.1.1
Once client api is added to the reference we need to update our web.config or app.config file as shown
After adding the above configuration to config-sections add the below configuration for server where Memcached server is running
Since Memcached server is running on local instance so the address given as "127.0.0.1" and port "11211" is default on which server is listening
Now using the library is simple enough as shown is below fig for storing and retrieving the string value.
Above code will simply store the string and retrieve the same using the key("first").
But in real life there will be requirement to store the List type of objects in cache retrieve it,so below code will help in doing the same.
Fig:1.0
Fig.1.1
Once client api is added to the reference we need to update our web.config or app.config file as shown
![]() |
Config sections |
After adding the above configuration to config-sections add the below configuration for server where Memcached server is running
![]() |
Server Configuration |
Since Memcached server is running on local instance so the address given as "127.0.0.1" and port "11211" is default on which server is listening
Now using the library is simple enough as shown is below fig for storing and retrieving the string value.
But in real life there will be requirement to store the List type of objects in cache retrieve it,so below code will help in doing the same.
As you can see in the above code List of contact is first converted to bytearray object and then stored in the Memcached server with the key "ContactList",once the list is stored in the cache as bytearray the same can retrieved using the key and can be converted from bytearray to object.
Hope it would help someone :)
Work great for me :) thanks for sharing
ReplyDelete