Search code examples
cachingappfabric

adding a value into the cache when the value isn’t in the cache


I want to program the basic CRUD operations of caching (creating new object , remove it , update...) i start by creating the object and displaying its name this is my code: (My cache name="default")

web.config:

..........

<configuration>
<configSections>
<section name="dataCacheClient"
   type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
    Microsoft.ApplicationServer.Caching.Core,
    Version=1.0.0.0,  Culture=neutral,PublicKeyToken=31bf3856ad364e35" />
     </configSections>
     <dataCacheClient>
      <hosts>
        <host name="Amira-PC" cachePort="22233" />

        </hosts>
       </dataCacheClient>

         ......

Global.asax.cs :

     using Microsoft.ApplicationServer.Caching;

      namespace AppFabricCachingTest{
        public class Global : System.Web.HttpApplication
         {
         public static string CacheFactoryName = "CacheFactory";
          void Application_Start(object sender, EventArgs e)
          {
        // Code qui s'exécute au démarrage de l'application


        var dcf = new DataCacheFactory();
        Application[CacheFactoryName] = dcf;
        DataCache myCache = dcf.GetCache("default");// cache name=mycache

        object myCachedItem = new Object();
        string myCachedItemKey = "MyCacheKey";
        myCache.Add(myCachedItemKey, myCachedItem);



     }
    .....

     `  

Site.Master.cs

       `  
        using Microsoft.ApplicationServer.Caching;

         namespace AppFabricCachingTest
       {
           public partial class SiteMaster : System.Web.UI.MasterPage
               {
           protected void Page_Load(object sender, EventArgs e)
              {

                }

    public string GetCachedName()
     {
       string name = null;
       string key ="MyCacheKey";

        var dcf = Application[Global.CacheFactoryName] as DataCacheFactory;

          if (dcf != null)
            {
    var cache = dcf.GetCache("default");
    name = cache.Get(key) as string;
    if (name == null)
    {
        name = "Windows Server App Fabric Cache ";
        cache.Put(key, name);

    }
    else
    {
        name += " From Cache!";

            }
             }
        else name = "dcf is NULL";
           return name;
       }

         }
     }

Site.Master :

 <body>
        <form runat="server">
             <div class="page">
                <div class="header">
                   <div class="title">
            <h1>
               <%= GetCachedName() %>
            </h1>
        </div>
            ...............

After running I got always "The dcf is NULL" and not the name of the object Any idea please,

Thank you in advance


Solution

  • I think you're missing declaration of other object as part of DataCacheFactory initialization. The DataCacheFactory does not know, where it should gather cluster cache from. Please try the below code

    DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1]; servers[0] = new DataCacheServerEndpoint("localhost", 22233);

    // Setup the DataCacheFactory configuration.

    DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration(); factoryConfig.Servers = servers;

    // Create a configured DataCacheFactory object.

    DataCacheFactory cacheFactory = new DataCacheFactory(factoryConfig);