3

过去几天我一直在使用 Azure 缓存。好的做法是使用本地缓存功能来防止到分布式缓存的往返。

可以在文档中阅读;当您调用 dataCache.Get(...) 时,应用程序首先检查本地缓存中的版本是否可用,如果没有,则从分布式缓存中检索对象。问题是本地版本可能比对象的分布式版本更旧。为了解决这个问题,可以使用“dataCache.GetIfNewer(...)”方法来检查本地对象的版本是否与分布式版本不同,如果不同,则返回新对象。

到目前为止,一切都很好......现在我的问题; 我创建了两个单独的应用程序(应用程序 A 和应用程序 B)来测试 Azure 缓存机制。两个应用程序都在两个不同的(物理)位置上运行,因此它们都有自己的本地缓存,但它们都使用相同的分布式缓存。

在使本地缓存失效的过程中是否确实发生了某些变化?我测试了以下场景,发现本地缓存是自动更新的:

  • 应用 A 使用键“CacheTest”将值“123”存储在分布式缓存中
  • 应用 B 使用 dataCache.Get(...) 方法检索在本地缓存中找不到的键“CacheTest”的对象,因此从分布式缓存中检索它并返回值为“123”的对象。
  • 应用 A 将键为“CacheTest”的对象更改为值“456”
  • 应用 B 使用 datacache.Get(...) 方法(再次)检索对象。因为对象应该在本地缓存中,所以我期望值“123”,但它返回新值“456”!

这有多奇怪?最近 Azure 缓存有什么变化吗?是的...我确定我已打开本地缓存,是的,我已将本地缓存的超时设置为 3600 秒(1 小时)。

有人可以确认 Azure 缓存已更改吗?

为尼克编辑:所以你的意思是我在荷兰微软网站上找到的下一行代码是无稽之谈?当本地缓存自动更新时,无需调用“GetIfNewer”方法: http: //www.dotnetmag.nl/Artikel/1478/Windows-Azure-AppFabric-Caching

/// 
/// Ensures that the newest version of a cached object is returned 
/// from either the local cache or the distrbuted cache.
/// 
public TCachedObjectType GetNewest<TCachedObjectType>(string key) : 
   where TCachedObjectType : class
{
DataCacheItemVersion version;

// Gets cached object from local cache if it exists.
// Otherwise gets cached object from distributed cache and 
// adds it to local cache.
object cachedObject = cache.Get(key, out version); 

// Gets cached object from distributed cached if it is newer
// than given version. 
// If newer it adds it to local cache.
object possiblyNewerCachedObject = 
     cache.GetIfNewer(key, ref version);

if (possiblyNewerCachedObject != null)
{
    // Cached object from distributed cache is newer 
    // than cached object from local cache.
    cachedObject = possiblyNewerCachedObject;
}

return cachedObject as TCachedObjectType;
}
4

1 回答 1

1

如果描述的行为与 appfabric 速度相同,则描述的行为符合预期。当启用本地缓存时,这意味着当给定节点从分布式缓存请求缓存项时,它会询问分布式缓存当前版本是什么。如果本地缓存版本与分布式版本匹配,则从本地缓存返回数据。如果没有,它会从分布式缓存中检索最新的值,将其缓存在本地,然后返回。这个想法是,如果任何节点更新了密钥,即使 appfabric 已经在本地缓存了它们,也将始终确保所有节点都获得最新版本。分布式缓存跟踪最新版本及其数据的存储位置。

于 2012-01-21T14:24:16.047 回答