You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Main/Mesnac.Basic/CacheFactory.cs

113 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mesnac.Basic
{
/// <summary>
/// 全局缓存工厂类
/// </summary>
public class CacheFactory
{
#region 单例实现
private static CacheFactory _instance = null; //静态成员
/// <summary>
/// 私有构造方法
/// </summary>
private CacheFactory()
{
}
/// <summary>
/// 缓存工厂静态实例
/// </summary>
public static CacheFactory Instance
{
get
{
lock(String.Empty)
{
if (_instance == null)
{
_instance = new CacheFactory();
}
return _instance;
}
}
}
#endregion
#region 字段定义
private Dictionary<string, object> dic = new Dictionary<string, object>();
#endregion
#region 方法定义
/// <summary>
/// 判断缓存工厂中是否存在指定的缓存项
/// </summary>
/// <param name="key">缓存的key值</param>
/// <returns>存在返回true不存在返回false</returns>
public bool ContainsKey(string key)
{
return this.dic.ContainsKey(key);
}
/// <summary>
/// 缓存方法
/// </summary>
/// <typeparam name="T">缓存对象的类型</typeparam>
/// <param name="key">缓存的key值</param>
/// <param name="value">缓存的对象值</param>
public void Cache<T>(string key, T value)
{
this.dic[key] = value;
}
/// <summary>
/// 从缓存中获取对象
/// </summary>
/// <typeparam name="T">缓存对象的类型</typeparam>
/// <param name="key">缓存的key值</param>
/// <returns>返回缓存对象值</returns>
public T Get<T>(string key)
{
if (this.dic.ContainsKey(key))
{
object value = this.dic[key];
return (T)value;
}
return default(T);
}
/// <summary>
/// 清空指定的缓存对象
/// </summary>
/// <param name="key">缓存的key值</param>
public void ClearByKey(string key)
{
if (this.dic.ContainsKey(key))
{
this.dic.Remove(key);
}
}
/// <summary>
/// 清空所有缓存对象
/// </summary>
public void ClearAll()
{
this.dic.Clear();
}
#endregion
}
}