A day with .Net

My day to day experince in .net

Access Windows Server AppFabric Cache through c# code

Posted by vivekcek on November 12, 2012

In this post i will explain how to add data to Windows Server AppFabric Cache.

1. Create a console application.

2. Add reference to Caching dll’s.

3. Use the below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ApplicationServer.Caching;

namespace AppFabricCache
{
    class Program
    {
        static DataCacheFactory _cacheFactory = Initialize();  
        static void Main(string[] args)
        {
            var cache = _cacheFactory.GetDefaultCache();
            var Key = "mykey";
            var obj = cache[Key];
            if (obj == null)
            {
                obj = "hai";
                cache.Add(Key, obj);
                Console.WriteLine("Object added");

            }
            else
            {
                Console.WriteLine("Object was already in cache");
            }
            Console.ReadLine();
        }
        static DataCacheFactory Initialize()
        {
            var Config = new DataCacheFactoryConfiguration();
            var Servers = new List<DataCacheServerEndpoint>();
            Servers.Add(new DataCacheServerEndpoint("Poomala",22233));
            Config.Servers = Servers;
            var temp=new DataCacheFactory(Config);

            return temp;
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s