0

我有一个场景,我们需要请求 4 年的数据。我已经设法将企业库与内存缓存连接起来。

问题是请求 4 年的数据并在本地存储需要很长时间。另一种方法是根据需要请求 1 年的数据和另外 3 年的数据,并添加本地缓存。

有人可以帮助我了解如何将数据添加到现有的缓存数据以及如何更新缓存的键吗?

4

1 回答 1

2

Enterprise Library 不知道也不知道如何将数据附加到您的对象。为此,您需要从缓存中获取对象,将新数据添加到对象中,然后使用相同的键将对象添加回缓存中。现有的缓存对象将被新的对象替换。它看起来像下面的代码。

string key = "key";

// get the existing cached data
var list = (List<object>) cacheManager.GetData(key);

// if there was no existing data, list will be null, so initialize it
if (list == null)
    list = new List<object>();

// add the new data
list.Add(new object());

// add the data back to the cache
cacheManager.Add(key, list);
于 2012-12-11T19:22:38.740 回答