C# 从AppDomain.CurrentDomain.SetData()获取密钥
我使用AppDomainC# 从AppDomain.CurrentDomain.SetData()获取密钥,c#,appdomain,C#,Appdomain,我使用AppDomainSetData()和GetData()在C#中实现了一个非常简单的对象缓存,如下所示(以减少对不经常更改的数据的DB调用次数): Program.cs-用法 class Program { static void Main(string[] args) { List<string> users = GetUserList<List<string>>(); ObjectCache.Purg
SetData()
和GetData()
在C#中实现了一个非常简单的对象缓存,如下所示(以减少对不经常更改的数据的DB调用次数):
Program.cs-用法
class Program
{
static void Main(string[] args)
{
List<string> users = GetUserList<List<string>>();
ObjectCache.Purge(); // Removes Cache
Console.ReadKey();
}
private static T GetUserList<T>()
{
var users = ObjectCache.GetValue("GetUserList");
if (null == users) // No Cache
{
users = new List<string>() { "adam", "smith" }; // Dummy DB Results
ObjectCache.SetValue("GetUserList", users);
}
return (T)users;
}
}
类程序
{
静态void Main(字符串[]参数)
{
List users=GetUserList();
ObjectCache.Purge();//删除缓存
Console.ReadKey();
}
私有静态T GetUserList()
{
var users=ObjectCache.GetValue(“GetUserList”);
if(null==users)//无缓存
{
users=new List(){“adam”,“smith”};//虚拟数据库结果
SetValue(“GetUserList”,用户);
}
返回(T)个用户;
}
}
AppDomain.GetData
不应用于缓存目的。相反,使用类似的解决方案。甚至仅仅是一个静态ConcurrentDictionary
也会更好
如果您坚持使用AppDomain
来存储值,则永远不要删除其中的所有内容-它存储.NET framework正常运行所需的信息
将值存储在AppDomain
对象内的字典中,或者自己保存一个键列表
使用静态字典的简单内存缓存(第二种方法适用于带有显式锁的.NET 2.0-请注意,这是一种非常简单的解决方案,有更好的锁定替代方案):
使用系统;
使用System.Collections.Concurrent;
名称空间YourNamespace
{
公共静态类ObjectCache
{
私有只读静态ConcurrentDictionary数据=新ConcurrentDictionary();
公共静态void SetValue(字符串键、对象值)
{
数据[键]=值;
}
公共静态对象GetValue(字符串键)
{
对象t;
如果(!Data.TryGetValue(键,out t))
返回null;
返回t;
}
公共静态无效清除()
{
Data.Clear();
}
}
公共静态类ObjectCache2
{
私有只读静态字典数据=新字典();
公共静态void SetValue(字符串键、对象值)
{
锁(数据)
数据[键]=值;
}
公共静态对象GetValue(字符串键)
{
对象t;
锁(数据)
{
如果(!Data.TryGetValue(键,out t))
返回null;
}
返回t;
}
公共静态无效清除()
{
锁(数据)
Data.Clear();
}
}
}
Aha!我怎么没想到呢。我将使用AppDomain
中的词典。谢谢我将在一个静态类中总结这一点。woo-hoo@LatheesanKanes-与简单的静态属性相比,将字典放入AppDomain
有什么好处?我已经更新了我的第一篇文章。我希望使用如上所示的缓存。我这样做错了吗?@LatheesanKanes-您仍然没有回答为什么需要将值存储在AppDomain
中?我用一个简单的内存缓存实现更新了答案,它使用了相同的方法名——这要简单得多,而且您当前的方法在并发性方面会有很多问题。我的项目在C#.NET 2.0中——遗憾的是,我没有访问System.Collections.Concurrent(它似乎适用于.NET 4.0及更高版本)。有没有类似ConcurrentDictionary的.NET2.0内存缓存替代方案?
class ObjectCache
{
private const string CacheName = "ObjectCache";
private static Dictionary<String, Object> Load()
{
Dictionary<string, object> myObjectCache = AppDomain.CurrentDomain.GetData(CacheName) as Dictionary<string, object>;
if (null == myObjectCache)
{
myObjectCache = new Dictionary<string, object>();
AppDomain.CurrentDomain.SetData(CacheName, myObjectCache);
}
return myObjectCache;
}
private static void Save(Object myObjectCache)
{
AppDomain.CurrentDomain.SetData(CacheName, myObjectCache);
}
public static void Purge()
{
Dictionary<string, object> myObjectCache = ObjectCache.Load();
myObjectCache.Clear();
ObjectCache.Save(myObjectCache);
}
public static void SetValue(String myKey, Object myValue)
{
Dictionary<string, object> myObjectCache = ObjectCache.Load();
myObjectCache[myKey] = myValue;
ObjectCache.Save(myObjectCache);
}
public static Object GetValue(String myKey)
{
Dictionary<string, object> myObjectCache = ObjectCache.Load();
return myObjectCache.ContainsKey(myKey) ? myObjectCache[myKey] : null;
}
}
class Program
{
static void Main(string[] args)
{
List<string> users = GetUserList<List<string>>();
ObjectCache.Purge(); // Removes Cache
Console.ReadKey();
}
private static T GetUserList<T>()
{
var users = ObjectCache.GetValue("GetUserList");
if (null == users) // No Cache
{
users = new List<string>() { "adam", "smith" }; // Dummy DB Results
ObjectCache.SetValue("GetUserList", users);
}
return (T)users;
}
}
using System;
using System.Collections.Concurrent;
namespace YourNamespace
{
public static class ObjectCache
{
private readonly static ConcurrentDictionary<string, object> Data = new ConcurrentDictionary<string, object>();
public static void SetValue(string key, object value)
{
Data[key] = value;
}
public static object GetValue(string key)
{
object t;
if (!Data.TryGetValue(key, out t))
return null;
return t;
}
public static void Purge()
{
Data.Clear();
}
}
public static class ObjectCache2
{
private readonly static Dictionary<string, object> Data = new Dictionary<string, object>();
public static void SetValue(string key, object value)
{
lock (Data)
Data[key] = value;
}
public static object GetValue(string key)
{
object t;
lock (Data)
{
if (!Data.TryGetValue(key, out t))
return null;
}
return t;
}
public static void Purge()
{
lock (Data)
Data.Clear();
}
}
}