编程模型(Windows Server AppFabric 缓存)

为缓存端编程模式自定义了 Windows Server AppFabric 编程模型。这意味着如果您的数据不存在于缓存、您的应用程序和 AppFabric 分布式缓存中,则必须从原始数据源将数据重新加载到缓存中。应用程序代码使用 DataCache 类,在实例化后也称为缓存客户端。

缓存策略

应将应用程序代码设计为可独立于缓存而正常工作,并且不需要缓存数据始终可用。由于缓存中的数据不以持久形式暂留,因此在这种情况下数据不可用的可能性是存在的。

高可用性功能有助于在群集运行的同时,通过独立缓存主机防止计算机和进程出现故障。但可能出现整个群集故障的情况。例如,如果过多主要主机出现故障,整个群集将关闭。有关详细信息,请参阅主要主机和群集管理(Windows Server AppFabric 缓存)

还有许多其他原因导致您的代码可能遇到缓存未命中:缓存项可能已过期或已被逐出,缓存服务器可能已重新启动,缓存服务可能已重新启动,或缓存群集可能已意外重新启动。无论何种原因,如果缓存对象不可用,您的应用程序代码应该能够访问数据库(或其他数据源)。

缓存客户端

要将数据存储到缓存中,请使用 GetCache 方法或 GetDefaultCache 方法返回 DataCache 对象。实例化后,此 DataCache 对象称为缓存客户端。

备注

出于性能方面的原因,我们建议您尽量减少在启用缓存的应用程序中创建的 DataCacheFactory 对象的数量。将 DataCacheFactory 对象存储在可用于应用程序(使用缓存客户端)的所有部分的变量中。

存在可用于配置缓存客户端行为的许多选项。您能够以编程方式或使用应用程序配置文件或使用这两种方法指定这些配置设置。有关缓存客户端和可用的应用程序配置设置的详细信息,请参阅缓存客户端和本地缓存(Windows Server AppFabric 缓存)应用程序配置设置(Windows Server AppFabric 缓存)

多个缓存客户端可能同时访问一个缓存。不同计算机上运行的应用程序可以通过对缓存客户端(对其进行配置以使用相同缓存)进行实例化来实现此操作。以下代码示例介绍了这一概念。请注意,注释用于标识在不同缓存客户端实例上执行的代码。

'Each application has a similar GetCache method call
Dim myCacheFactory As DataCacheFactory = New DataCacheFactory()
Dim catalog As DataCache = myCacheFactory.GetCache("catalog")

'One cache client saves an object to the catalog named "toy101"
Call catalog.Put("toy101", New ToyObject("toy101", "Playschool"))

'The same or different cache client retrieves the object
Dim toy As ToyObject = CType(catalog.Get("toy101"), ToyObject)

'The same or a different cache client removes the object
catalog.Remove("toy101")
//Each application has a similar GetCache method call
DataCacheFactory myCacheFactory = new DataCacheFactory();
DataCache catalog = myCacheFactory.GetCache("catalog");

//One cache client saves an object to the catalog named "toy101"
catalog.Put("toy101", new ToyObject("toy101", "Playschool"));

//The same or different cache client retrieves the object
ToyObject toy = (ToyObject)catalog.Get("toy101");

//The same or a different cache client removes the object
catalog.Remove("toy101");

另请参阅

概念

Windows Server AppFabric 缓存物理体系结构示意图
Windows Server AppFabric 缓存逻辑体系结构示意图
开发缓存客户端(Windows Server AppFabric 缓存)

  2011-12-05