Caching 当值为’;不在缓存中

Caching 当值为’;不在缓存中,caching,appfabric,Caching,Appfabric,我想编程缓存的基本CRUD操作(创建新对象、删除它、更新…)我首先创建对象并显示其名称这是我的代码: (我的缓存名称=“默认”) web.config: .......... <configuration> <configSections> <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsof

我想编程缓存的基本CRUD操作(创建新对象、删除它、更新…)我首先创建对象并显示其名称这是我的代码: (我的缓存名称=“默认”)

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>

         ......
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;
       }

         }
     }
网站主:

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

...............
运行之后,我得到的总是“dcf为空”,而不是对象的名称 有什么想法吗


提前感谢您

我认为您缺少作为DataCacheFactory初始化一部分的其他对象声明。 DataCacheFactory不知道应该从何处收集群集缓存。 请尝试下面的代码

DataCacheServerEndpoint[]服务器=新的DataCacheServerEndpoint[1]; 服务器[0]=新的DataCacheServerEndpoint(“localhost”,22233)

//设置DataCacheFactory配置

DataCacheFactoryConfiguration factoryConfig=新的DataCacheFactoryConfiguration(); factoryConfig.Servers=服务器

//创建已配置的DataCacheFactory对象

DataCacheFactory cacheFactory=新的DataCacheFactory(factoryConfig)

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