我刚刚为 .NET 安装了 membase 和 enyim 客户端,并遇到了一篇文章,其中提到了这种集成 linq 的技术:
public static IEnumerable<T> CachedQuery<T>
(this IQueryable<T> query, MembaseClient cache, string key) where T : class
{
object result;
if (cache.TryGet(key, out result))
{
return (IEnumerable<T>)result;
}
else
{
IEnumerable<T> items = query.ToList();
cache.Store(StoreMode.Set, key, items);
return items;
}
}
它将首先检查所需的数据是否在缓存中,如果没有缓存则返回。
目前我 Dictionary<'String, List'>
在我的应用程序中使用 a 并希望用 membase/memcached 类型的方法替换它。
将项目添加到 List<'T'> 或在缓存列表上使用 Linq 运算符的类似模式怎么样?在我看来,将整个 List<'T'> 存储在缓存中的单个键下并且必须检索它,添加到它,然后在每次要添加时重新设置它可能是一个坏主意元素。或者这是一种可以接受的做法?
public bool Add(T item)
{
object list;
if (cache.TryGet(this.Key, out list))
{
var _list = list as List<T>;
_list.Add(item);
return cache.Store(StoreMode.Set, this.Key, _list);
}
else
{
var _list = new List<T>(new T[] { item });
return cache.Store(StoreMode.Set, this.Key, _list);
}
}
在这样的缓存情况下,通常如何处理集合?通常是使用散列算法,还是使用某种键前缀系统来识别缓存的键值存储中类型为 T 的“列表”?