缓存管理

前言:
在C/S架构中,或者网络开发,安卓开发中。经常遇到要处理缓存的情况。根据职责分离原则,我们需要分离出缓存管理对象,必要时写个缓存接口有不同的实现,比如实现一个保存在数据库的缓存,或者物理文件的缓存。

在本系统中有一个DataCache 缓存管理类
key = 终端ID-DataId
value = <T>

在云端下载中自己是用dictionary<key,value> +lock or 安全字典来实现缓存
value 用基本数据类型或结构体或类

下面是缓存管理类代码:

   /// <summary>
    /// 数据缓存管理
    /// </summary>
    internal class DataCache
    {
        #region Init
        /// <summary>
        /// 任务处理数据集合
        /// </summary>
private static ConcurrentDictionary<string, object> taskData = new ConcurrentDictionary<string, object>();
  /// <summary>
        /// 任务处理数据集合
        /// </summary>
        private static ConcurrentDictionary<string, string> resourceData = new ConcurrentDictionary<string, string>();
/// <summary> /// /// </summary> static DataCache() { } #endregion #region ParameterData /// <summary> /// 获取任务处理的数据 /// </summary> /// <param name="clt"></param> /// <param name="dataId"></param> /// <param name="dataIndex">数据索引</param> /// <param name="dataType">序列化的数据类型</param> /// <returns></returns> public static T GetTaskCommonData<T>(IocpServer.WorkPool.Carrier clt, int data_Id, int dataIdx, Customer cust, ref int dataCount) { List<T> lstData = null; string key = GetDataKey(clt.TerminalNo, data_Id); try { //如果缓存中没有数据,则从数据库中读取 if (!taskData.ContainsKey(key)) { lstData = (List<T>) cust.GetCommonData(clt, data_Id, typeof (List<T>)); if (lstData != null) { //添加缓存数据 AddData(lstData, key); } } else { lstData = GetData<T>(key); } if (lstData != null) { //直接转换了 dataCount =lstData.Count; return lstData[dataIdx]; } } catch { } return default(T); } /// <summary> /// 添加二进制数据 /// </summary> /// <param name="data"></param> /// <param name="dataId"></param> public static void AddData(byte[] data, string key) { taskData[key] = data; } /// <summary> /// 获取二进制数据 /// </summary> /// <param name="key"></param> /// <returns></returns> public static byte[] GetData(IocpServer.WorkPool.Carrier clt, int dataId) { string key = GetDataKey(clt.TerminalNo, dataId); if (taskData.ContainsKey(key)) { return (byte[])taskData[key]; } return null; } /// <summary> /// 添加数据 /// </summary> /// <param name="data"></param> private static void AddData<T>(List<T> data, string key) { taskData[key] = data; } /// <summary> /// 从缓存中读取数据 /// </summary> /// <param name="key"></param> /// <returns></returns> private static List<T> GetData<T>(string key) { if (taskData.ContainsKey(key)) { return (List<T>)taskData[key]; } return null; } private static string GetDataKey(long terminalNo, int dataId) { return string.Format("{0}-{1}", terminalNo, dataId); } /// <summary> /// 获取资源名称 /// </summary> /// <param name="cmdName"></param> /// <returns></returns> public static string GetResourceValue(string cmdName) { string name = string.Empty; string keyName = string.Format("Bus_DataCmdType_{0}", cmdName); if (resourceData.ContainsKey(keyName)) { name = (string)resourceData[keyName]; } else { name = BaseFun.GetResourceValue(typeof(Resource.GlobalResource), keyName, ""); if (!string.IsNullOrEmpty(name)) { resourceData[keyName] = name; } } return name; } /// <summary> /// 移除数据 /// </summary> /// <param name="dataId"></param> public static void RemoveData(IocpServer.WorkPool.Carrier clt, int dataId) { string key = clt.TerminalNo + "-" + dataId; object obj; taskData.TryRemove(key, out obj); } /// <summary> /// 每个一个小时清空缓存数据 /// </summary> public static void ClearCacheData() { DateTime dt = DateTime.Now; if (dt.Minute == 1) { taskData.Clear(); upgradeData.Clear(); } } #endregion StringData #region 文件升级数据缓存 private static Hashtable upgradeData = new Hashtable(); /// <summary> /// 保存文件升级下一次数据 /// </summary> /// <param name="terminalId"></param> /// <param name="bResponeStatus"></param> /// <param name="nextData"></param> public static void SaveUpgradeData(int dataId, int startIndex, int blockSize, int dataTotal, bool bResponeStatus) { FileUpgradeDataCache fileUpgradeData = null; if (upgradeData.ContainsKey(dataId)) { fileUpgradeData = (FileUpgradeDataCache)upgradeData[dataId]; fileUpgradeData.StartIndex = startIndex; fileUpgradeData.HaveRecLastRespone = bResponeStatus; } else { fileUpgradeData = new FileUpgradeDataCache(dataId, startIndex, blockSize, dataTotal); upgradeData[dataId] = fileUpgradeData; } } /// <summary> /// 更新升级数据响应状态 /// </summary> /// <param name="dataId"></param> /// <param name="bResponeStatus"></param> public static void UpgradeDataStatus(int dataId, bool bResponeStatus) { FileUpgradeDataCache fileUpgradeData = null; if (upgradeData.ContainsKey(dataId)) { fileUpgradeData = (FileUpgradeDataCache)upgradeData[dataId]; fileUpgradeData.HaveRecLastRespone = bResponeStatus; } } /// <summary> /// 获取文件升级上一次的缓存数据 /// </summary> /// <param name="dataId"></param> /// <returns></returns> public static FileUpgradeDataCache GetUpgradeData(int dataId) { if (upgradeData.ContainsKey(dataId)) { return (FileUpgradeDataCache)upgradeData[dataId]; } return null; } /// <summary> /// 删除数据 /// </summary> /// <param name="dataId"></param> /// <returns></returns> public static void RemoveUpgradeData(int dataId) { if (upgradeData.ContainsKey(dataId)) { upgradeData.Remove(dataId); } } #endregion }

 

posted @ 2015-07-09 16:38  maanshancss  阅读(226)  评论(0编辑  收藏  举报