以下示例介绍更新缓存中对象的方法。

有关这些示例中使用的方法的详细信息,请参见以下类库主题:

备注

这些过程假设您已设置缓存群集并且已准备好开发环境。有关详细信息,请参阅准备缓存客户端开发环境(Windows Server AppFabric 缓存)

更新缓存中的对象

  1. 确保 using 语句(在 Visual Basic 中为 Imports)位于应用程序代码的顶部以引用 Microsoft.ApplicationServer.Caching 命名空间。

  2. 创建一个需要缓存客户端的应用程序的所有部分都可以访问的 DataCacheFactory 对象。如果可能,存储并重复使用同一个 DataCacheFactory 对象,以节省内存并优化性能。

  3. 使用 DataCacheFactory 对象创建一个 DataCache 对象(也称为缓存客户端)。

  4. 拥有 DataCache 对象之后,使用 Put 方法或 Item 属性向缓存中添加对象。在以下示例中,将 DataCache 实例命名为 myCache

示例

以下示例使用 Put 方法向缓存中添加对象。如果调用此方法时对象不存在,则会向缓存中添加此对象。如果对象已存在,将替换此对象。

'add or replace object in cache using key "Key0"
myCache.Put("Key0", "object replaced or added using Key0")
//add or replace object in cache using key "Key0"
myCache.Put("Key0", "object replaced or added using Key0");

以下示例使用具有数组表示法的 Item 属性向缓存中添加对象。如果调用此方法时对象不存在,则会向缓存中添加该对象。如果对象已存在,将替换该对象。

'add or replace object in cache using array notation
myCache("Key0") = "object replaced or added using Key0"
//add or replace object in cache using array notation
myCache["Key0"] = "object replaced or added using Key0";

备注

还有许多其他参数可用于 Put 方法。有关详细信息,请参阅 DataCache 类。

另请参阅

概念

准备缓存客户端开发环境(Windows Server AppFabric 缓存)
向缓存添加对象(Windows Server AppFabric 缓存)
获取缓存中的对象(Windows Server AppFabric 缓存)
从缓存删除对象(Windows Server AppFabric 缓存)
Windows Server AppFabric 缓存概念
开发缓存客户端(Windows Server AppFabric 缓存)

  2011-12-05