【问题标题】:Why do I have multiple cache items in my AppFabric cache with the same key?为什么我的 AppFabric 缓存中有多个缓存项具有相同的键?
【发布时间】:2013-04-25 14:53:54
【问题描述】:

我的网站代码位于两台服务器上,我希望它们之间共享缓存,因此我使用的是 Microsoft AppFabric。我只希望缓存中有一个项目——不是每个站点访问者一个项目,总共只有一个项目。

我用来尝试完成此操作的代码如下,但是,它没有按照我想要的方式工作。我认为问题在于缓存中有很多项目,尽管它们都具有相同的密钥。但这可能是正在发生的其他事情。更多详细信息请参见代码 cmets。

Web.config:

<dataCacheClient>
    <hosts>
      <host name="MyHost1" cachePort="22233"/>
      <host name="MyHost2" cachePort="22233"/>
    </hosts>
    <securityProperties mode="None" protectionLevel="None" />
</dataCacheClient>
...
<system.web>
    <sessionState mode="InProc" customProvider="AppFabricCacheSessionStoreProvider">
        <providers>
            <add
                name="AppFabricCacheSessionStoreProvider"
                type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider"
                cacheName="MyCache"
                sharedId="MySharedApp"/>
        </providers>
    </sessionState>
    ...
</system.web>

Global.asax.cs:

private static DataCacheFactory _cacheFactory = new DataCacheFactory();
private static DataCache _cache;

private void RegisterCacheEntry(){
    try
    {
        // Set up cache
        _cache = _cacheFactory.GetCache("MyCache");

        // Prevent duplicate key addition
        if (_cache.GetCacheItem(CacheItemKey) != null || _cache.Get(CacheItemKey) != null) 
            return;

        // Add item to cache
        _cache.Add(CacheItemKey, CacheItem, TimeSpan.FromMinutes(10));

        // When the cache item is removed, the callback:
        // 1) sends me an email 
        // 2) adds the same item with the same key to the cache again.
        // So I SHOULD be getting one email every 10 minutes.
        // However, it starts with one, but the number of emails that I get increases every 10 minutes.
        // i.e., I get one email at time t=0, two emails at t=1, three at t=2, then four, etc.
        _cache.AddItemLevelCallback(CacheItemKey, DataCacheOperations.RemoveItem, CacheItemRemovedCallback);
    }
    catch (Exception e){}
}

如果您想了解更多关于我在做什么以及为什么做的信息,我正在尝试使用here 所述的缓存超时来模拟 Windows 服务。但我正试图让它在两台服务器上工作。

【问题讨论】:

    标签: session caching appfabric distributed-caching appfabric-cache


    【解决方案1】:

    您在 web.config 中添加了 AppFabric 会话状态提供程序。所有会话都将保留在集群中:这就是为什么您有多个缓存项。

    此外,您需要在系统中添加额外的错误管理,如 here 所说:

    缓存主机只能容纳一定数量的缓存操作 记忆。根据系统负载,可能会有一些缓存 客户端在被截断之前可能不会收到通知 缓存主机队列。缓存客户端也可能在数据时错过通知 由于缓存服务器故障而丢失,而集群的其余部分 保持运行。在这些情况下,您的缓存客户端可以发现 它通过使用失败错过了一些缓存通知 通知。您的应用程序可以添加回调以接收失败 使用 AddFailureNotificationCallback 方法通知。为了 更多信息,请参阅添加失败通知回调

    编辑: CacheItemRemovedCallback 提供从缓存中删除的密钥。检查它是否始终相同。我还建议不要将分布式缓存限制为单个项目:如果其他人在缓存中添加某些内容,您的系统将失败。

    【讨论】:

    • 我删除了整个&lt;sessionState&gt; 部分,但这似乎并没有改变任何东西。我还有多件物品。
    • 你怎么知道有多个项目?你在使用 PS cmdlet Get-Statistics 吗?
    • 我只是假设它是多个缓存项。我无权访问 PowerShell 也无法检查;但还有什么问题呢?一个缓存项多次过期?
    • Powershell 是 AppFabric 缓存的先决条件,因此您可以使用管理模块(在开始菜单中)轻松运行 cmdlet Get-CacheStatistics bery。首先运行它以获取项目数。
    • 查看编辑并显示更多代码:也许您在 CacheItemRemovedCallback 中重新添加一些内容,或者您​​多次调用 RegisterCacheEntry
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多