/// <summary> /// 初始化缓存 /// </summary> private void initDataCache() { try { long nLenSize = DataHelper.StructSize <DataLenNode>(); long nDataSize = DataHelper.StructSize <T>(); long mapCacheSize = this.mMaxDataCount * nDataSize + nLenSize; var dbFilePath = string.Format("{0}/{1}.dat", YunMapCacheHelper.getMapCacheRoot(), this.mCacheFileName); var mapName = string.Format("YunMapCache_{0}", this.mCacheFileName); this.mMapFile = MemoryMappedFile.CreateFromFile(dbFilePath, FileMode.OpenOrCreate, mapName, mapCacheSize); this.mMapStream = this.mMapFile.CreateViewStream(); //读取最后一个写入位置 this.mLastWriteKey = this.ReadLen(); YunLib.LogWriter.Log("YunMapCacheServer InitCache dbFilePath={0}", Path.GetFullPath(dbFilePath)); YunLib.LogWriter.Log("YunMapCacheServer InitCache mapName={0} Success,WriteLen={1}", mapName, this.mLastWriteKey); Console.WriteLine("YunMapCacheServer InitCache dbFilePath={0}", Path.GetFullPath(dbFilePath)); Console.WriteLine("YunMapCacheServer InitCache mapName={0} Success,WriteLen={1}", mapName, this.mLastWriteKey); } catch (Exception ex) { YunLib.LogWriter.Log(ex.ToString()); } }
/// <summary> /// 彻底清理 /// </summary> public void Destory() { this.Close(); try { var dbFilePath = string.Format("{0}/{1}.dat", YunMapCacheHelper.getMapCacheRoot(), this.mCacheFileName); YunLib.LogWriter.Log("YunMapCacheServer Destory dbFilePath={0} Begin", dbFilePath); if (File.Exists(dbFilePath)) { dbFilePath = Path.GetFullPath(dbFilePath); try { File.Delete(dbFilePath); } catch (Exception eex) { YunLib.LogWriter.Log(eex.Message); } } YunLib.LogWriter.Log("YunMapCacheServer Destory dbFilePath={0} End", dbFilePath); } catch (Exception ex) { YunLib.LogWriter.Log(ex.ToString()); } }
/// <summary> /// 彻底清理 /// </summary> public void Destory() { this.Close(); try { var dbFilePath = string.Format("{0}/{1}.dat", YunMapCacheHelper.getMapCacheRoot(), this.mCacheFileName); if (File.Exists(dbFilePath)) { dbFilePath = Path.GetFullPath(dbFilePath); YunLib.LogWriter.Log("YunMapCacheClient Destory dbFilePath={0}", dbFilePath); File.Delete(dbFilePath); } } catch (Exception) { //YunLib.LogWriter.Log(ex.ToString()); } }
/// <summary> /// 获取存在的实例 /// </summary> /// <param name="mLastDateStr"></param> /// <param name="dbName"></param> /// <param name="maxCount"></param> /// <returns></returns> private static T getExistsInstance(string mLastDateStr, string dbName, long maxCount) { T mInstance = new T(); mInstanceListLock.EnterReadLock(); try { string dataKey = YunMapCacheHelper.getInstanceName(mLastDateStr, dbName, maxCount); if (mInstanceList.ContainsKey(dataKey)) { mInstance = mInstanceList[dataKey]; } } catch (Exception ex) { YunLib.LogWriter.Log(ex.ToString()); } mInstanceListLock.ExitReadLock(); return(mInstance); }
private static void clearOutDateFile(string mLastDateStr) { YunLib.LogWriter.Log("YunMapCacheManager.clearOutDateFile Begin"); string fileDir = YunMapCacheHelper.getMapCacheRoot(); try { var fileList = Directory.GetFiles(fileDir); foreach (string fileName in fileList) { if (fileName.Contains(mLastDateStr)) { continue; } try { File.Delete(fileName); } catch (Exception eex) { YunLib.LogWriter.Log(eex.Message); } } } catch (Exception ex) { YunLib.LogWriter.Log(ex.ToString()); } YunLib.LogWriter.Log("YunMapCacheManager.clearOutDateFile End"); }
/// <summary> /// 获取一个新的实例 /// </summary> /// <returns></returns> private static T getNewInstance(string mLastDateStr, string dbName, long maxCount) { T mInstance = new T(); mInstanceListLock.EnterWriteLock(); try { string dataKey = YunMapCacheHelper.getInstanceName(mLastDateStr, dbName, maxCount); //可能多个写锁触发,先判断 if (mInstanceList.ContainsKey(dataKey)) { mInstance = mInstanceList[dataKey]; } else { mInstance.InitCache(dataKey, maxCount); if (mInstance.IsCacheValid()) { mInstanceList.Add(dataKey, mInstance); } } //清理过期数据缓存 clearOutDateCache(mLastDateStr); } catch (Exception ex) { YunLib.LogWriter.Log(ex.ToString()); } mInstanceListLock.ExitWriteLock(); return(mInstance); }