Azure本地缓存与分布式缓存

时间:2012-01-21 13:59:41

标签: caching azure distributed-caching

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

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

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

在使本地缓存失效的过程中发生了什么变化是真的吗?我测试了以下场景,发现本地缓存是自动更新的:

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

有多奇怪? Azure Caching最近有什么变化吗?是的...我确定我已经启用了本地缓存,是的,我已将本地缓存的超时设置为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;
}

1 个答案:

答案 0 :(得分:1)

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

相关问题