C# 多线程缓存
我正在尝试为C#Portable Library(.Net 4及更高版本(Asp.Net MVC、Winodws 8、Widows Phone 8、Silver light,将来可能用于WPF)实现自定义缓存,低延迟代码,这将是多线程缓存,尝试在singleton上实现。如何实现 如何使我的实现是单例和线程安全的。C# 多线程缓存,c#,asp.net,multithreading,caching,C#,Asp.net,Multithreading,Caching,我正在尝试为C#Portable Library(.Net 4及更高版本(Asp.Net MVC、Winodws 8、Widows Phone 8、Silver light,将来可能用于WPF)实现自定义缓存,低延迟代码,这将是多线程缓存,尝试在singleton上实现。如何实现 如何使我的实现是单例和线程安全的。 interface ICustomCache { bool IsFound(string key, out value); //returns true if found
interface ICustomCache
{
bool IsFound(string key, out value); //returns true if found the object
void Set(string key, object value); //If there is already an object with such a key, then the set should replace the old object
}
public class Cache : ICustomCache
{
private readonly Dictionary<string, object> _cacheDictionary = new Dictionary<string, object>();
public bool IsFound(string key, out object value)
{
if (_cacheDictionary.ContainsKey(key))
{
return _cacheDictionary.TryGetValue(key, out value);
}
value = null;
return false;
}
public void SetCachedObject(string key, object value)
{
if (_cacheDictionary.ContainsKey(key))
{
_cacheDictionary.Remove(key);
_cacheDictionary.Add(key,value);
}
else
{
_cacheDictionary.Add(key, value);
}
}
}
接口ICustomCache
{
bool IsFound(字符串键,out值);//如果找到对象,则返回true
void Set(字符串键,对象值);//如果已经有一个对象具有这样的键,那么该集合应该替换旧对象
}
公共类缓存:ICustomCache
{
专用只读词典_cacheDictionary=新词典();
public bool IsFound(字符串键,out对象值)
{
if(_cacheDictionary.ContainsKey(键))
{
返回_cacheDictionary.TryGetValue(key,out值);
}
值=空;
返回false;
}
public void SetCachedObject(字符串键,对象值)
{
if(_cacheDictionary.ContainsKey(键))
{
_cacheDictionary.Remove(key);
_cacheDictionary.Add(键、值);
}
其他的
{
_cacheDictionary.Add(键、值);
}
}
}
有很多假设和很多方法,下面只是一段代码片段,希望能给你一些指导:-
interface ICustomCache
{
bool IsFound(string key, out object value);
void Set(string key, object value); the set should replace the old object
}
public class SQLCustomCache:ICustomCache
{
#region ICustomCache Members
public bool IsFound(string key, out object value)
{
"Your Implementation for cheking in SQl";
}
public void Set(string key, object value)
{
"Your Implementation for Storing it in SQl";
}
#endregion
}
public class FileSystemCustomCache : ICustomCache
{
#region ICustomCache Members
public bool IsFound(string key, out object value)
{
"Your Implementation for cheking in FileSystem";
}
public void Set(string key, object value)
{
"Your Implementation for Storing it in FileSystem";
}
#endregion
}
public class CustomCache
{
static ICustomCache mycacheObject = null;
#region ICustomCache Members
private CustomCache()
{
}
public ICustomCache GetCustomCacheInstance(string Type)
{
if(mycacheObject==null)
{
if (Type == "SQL")
{
mycacheObject = new SQLCustomCache();
}
else
{
mycacheObject = new FileSystemCustomCache();
}
}
return mycacheObject;
}
}
请尝试以下代码以使缓存管理器保持单例和线程安全
/// <summary>
/// Custom cache interface
/// </summary>
interface ICustomCache
{
bool IsFound(string key, out object value); //returns true if found the object
void Set(string key, object value); //If there is already an object with such a key, then the set should replace the old object
}
/// <summary>
/// In memory cache implementation
/// </summary>
public class CustomCache : ICustomCache
{
// use thread safe dictionary
private readonly ConcurrentDictionary<string, object> _cacheDictionary = new ConcurrentDictionary<string, object>();
public bool IsFound(string key, out object value)
{
if (_cacheDictionary.ContainsKey(key))
{
return _cacheDictionary.TryGetValue(key, out value);
}
value = null;
return false;
}
public void Set(string key, object value)
{
if (_cacheDictionary.ContainsKey(key))
{
object dummy;
if (_cacheDictionary.TryRemove(key, out dummy))
{
_cacheDictionary.TryAdd(key, value);
}
}
else
{
_cacheDictionary.TryAdd(key, value);
}
}
}
/// <summary>
/// Cache manager
/// </summary>
public static class CacheManager
{
private static ICustomCache _cache = null;
static CacheManager()
{
// alternatly use Ioc container like Unity to create the object
_cache = new CustomCache();
}
public bool IsFound(string key, out object value)
{
return _cache.IsFound(key, out value);
}
public void Set(string key, object value)
{
_cache.Set(key, value);
}
}
// usage
CacheManager.Set("test1", "hello world!");
//
///自定义缓存接口
///
接口ICustomCache
{
bool IsFound(字符串键,out对象值);//如果找到对象,则返回true
void Set(字符串键,对象值);//如果已经有一个对象具有这样的键,那么该集合应该替换旧对象
}
///
///内存缓存实现
///
公共类CustomCache:ICustomCache
{
//使用线程安全字典
私有只读ConcurrentDictionary _cacheDictionary=新ConcurrentDictionary();
public bool IsFound(字符串键,out对象值)
{
if(_cacheDictionary.ContainsKey(键))
{
返回_cacheDictionary.TryGetValue(key,out值);
}
值=空;
返回false;
}
公共无效集(字符串键、对象值)
{
if(_cacheDictionary.ContainsKey(键))
{
物体假人;
if(_cacheDictionary.TryRemove(key,out dummy))
{
_cacheDictionary.TryAdd(键,值);
}
}
其他的
{
_cacheDictionary.TryAdd(键,值);
}
}
}
///
///缓存管理器
///
公共静态类缓存管理器
{
私有静态ICustomCache _cache=null;
静态缓存管理器()
{
//交替使用Ioc容器(如Unity)来创建对象
_cache=新的CustomCache();
}
public bool IsFound(字符串键,out对象值)
{
返回_cache.IsFound(key,out值);
}
公共无效集(字符串键、对象值)
{
_cache.Set(键、值);
}
}
//用法
Set(“test1”,“helloworld!”);
这是一个非常简单的实现,为了完整性,您可能需要考虑添加驱逐策略来控制缓存对象增长/应用程序内存、缓存统计等,
这是一个练习吗?如果不是,您可以重用现有的实现,比如java语言的加载缓存。ot练习,我正在为低延迟代码的可移植库实现。到目前为止,您有一个接口。对于实现:您尝试了什么?另外:可移植类库不明确:它需要支持什么框架?这可能很重要。