Filter posts by Category Or Tag of the Blog section!

Appfabric cache and benefits

Saturday, 14 January 2023

Microsoft AppFabric Cache is a distributed in-memory cache platform that can be used to improve the performance and scalability of .NET applications. To use AppFabric Cache in your application, you can follow these steps:


  1. Install the AppFabric Cache client: You can install the AppFabric Cache client using the Microsoft Web Platform Installer or by downloading the installer from the Microsoft website.
  2. Configure the cache cluster: You need to configure a cache cluster before you can start using AppFabric Cache. A cache cluster is a group of one or more cache hosts that work together to provide a distributed cache. You can configure the cache cluster using the AppFabric Cache Administration tool.
  3. Create a cache client: To interact with the cache cluster, you need to create a cache client object. The cache client object is responsible for managing the connections to the cache cluster and performing cache operations. You can create a cache client using the DataCacheFactory class:


DataCacheFactory factory = new DataCacheFactory();


  1. Create a cache object: To store and retrieve data from the cache, you need to create a cache object. A cache object is an instance of a named cache that resides on the cache cluster. You can create a cache object using the DataCache class:


DataCache cache = factory.GetCache("MyCache");


  1. Store data in the cache: To store data in the cache, you can use the Put method of the cache object. The Put method takes a key-value pair and a set of optional parameters:


cache.Put("myKey", "myValue");


  1. Retrieve data from the cache: To retrieve data from the cache, you can use the Get method of the cache object. The Get method takes a key and returns the corresponding value:


string value = (string)cache.Get("myKey");


  1. Remove data from the cache: To remove data from the cache, you can use the Remove method of the cache object:




As you saw, the basic using of appAbout is like other cache libraries. Out of the box, AppFabric provides more features, of course, cache notifications:


using System;

using Microsoft.ApplicationServer.Caching;

class CacheNotification


    static void Main(string[] args)


        // Create a new instance of DataCacheFactoryConfiguration

        DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();

        // Define the server(s) on which the cache is hosted

        factoryConfig.Servers = new List<DataCacheServerEndpoint>()


            new DataCacheServerEndpoint("cacheServer1", 22233),

            new DataCacheServerEndpoint("cacheServer2", 22233)


        // Create a new instance of DataCacheFactory

        DataCacheFactory factory = new DataCacheFactory(factoryConfig);

        // Get the default cache instance

        DataCache cache = factory.GetDefaultCache();

        // Create a new CacheItemPolicy with cache notifications enabled

        CacheItemPolicy policy = new CacheItemPolicy();

        policy.NotifyOnChanged = true;

        // Add an item to the cache with the specified key, value, and policy

        cache.Add("myKey", "myValue", policy);

        // Register for cache change notifications

        cache.AddItemLevelCallback("myKey", DataCacheOperations.RemoveItem, OnCacheChange);

        Console.WriteLine("Press any key to exit...");



    static void OnCacheChange(string cacheName, string key, CacheItemVersion version, DataCacheOperations operation, DataCacheNotificationDescriptor nd)


        Console.WriteLine("Cache item with key {0} has been {1} from cache {2}", key, operation.ToString(), cacheName);




In this example, we created a new instance of DataCacheFactory with the configuration settings for the cache servers. We then got the default cache instance and added an item to the cache with a CacheItemPolicy that has cache notifications enabled. We registered a callback function to be called when the cache item with the specified key is removed from the cache. The OnCacheChange function simply prints a message to the console indicating the cache item that was removed and the operation that caused the removal.

AppFabric Cache is an in-memory distributed cache service that is designed to help improve the performance and scalability of applications. Here are some of the features and benefits of using AppFabric Cache:

  1. Distributed caching: AppFabric Cache allows you to store data in a distributed cache that can be shared across multiple servers or applications. This helps to improve application performance and reduce the load on the backend data store.
  2. Scalability: AppFabric Cache provides horizontal scalability by allowing you to add additional cache servers as your application grows. This helps to ensure that your application can handle increasing traffic and data volumes.
  3. High availability: AppFabric Cache supports high availability by replicating data across multiple cache servers. This helps to ensure that your application remains available even if one or more cache servers fail.
  4. Performance: AppFabric Cache provides fast read and writes performance by storing data in memory. This helps to reduce the response times of your application and improve the user experience.
  5. Security: AppFabric Cache provides security features such as encryption and authentication to help ensure that your data is secure.
  6. Integration with .NET: AppFabric Cache provides a .NET client library that makes it easy to integrate caching into your .NET applications.
  7. Flexible configuration: AppFabric Cache provides a flexible configuration model that allows you to customize caching settings such as cache size, expiration policies, and eviction policies.

Using Distributed caching is straightforward as well:


using Microsoft.Extensions.Caching.Distributed;

using System;

public class MyService


    private readonly IDistributedCache _cache;

    public MyService(IDistributedCache cache)


        _cache = cache;


    public string GetCachedData(string key)


        string data = null;

        // Try to get the data from the cache.

        byte[] cacheData = _cache.Get(key);

        if (cacheData != null)


            data = System.Text.Encoding.UTF8.GetString(cacheData);




            // If the data is not in the cache, retrieve it from the database.

            data = RetrieveDataFromDatabase();

            // Cache the data.

            byte[] encodedData = System.Text.Encoding.UTF8.GetBytes(data);

            _cache.Set(key, encodedData, new DistributedCacheEntryOptions


                AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)



        return data;


    private string RetrieveDataFromDatabase()


        // Retrieve data from database.

        return "Data retrieved from database.";




I personally haven’t used AppFabric in real-world applications but I think it's a powerful library and has a wide range of community and it is worth giving it a try. 


Category: Software

Tags: Asp.Net

comments powered by Disqus