如何更新企业库缓存数据



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

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

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

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);

最新更新