//-------------------------------------------------- /// 构造函数 //-------------------------------------------------- public CHttpFileCacher(int max, string folderName, string fileExtension) { m_maxCnt = max; m_dir = GetCacheDirectory(folderName.ToLower()); m_metaFilePath = CFileManager.CombinePath(m_dir, folderName.ToLower() + ".bytes"); m_fileExtension = fileExtension; m_cachedFileInfoSet = new CCachedFileInfoSet(); MakeDirReady(); if (CFileManager.IsFileExist(m_metaFilePath)) { byte[] buffer = CFileManager.LockFileBuffer(); try { uint fileLength = CFileManager.ReadFile(m_metaFilePath, buffer, (uint)buffer.Length); int offset = 0; int ret = m_cachedFileInfoSet.Read(buffer, offset, fileLength); if (ret < 0) //读取出错时,删除所有文件 { Debug.LogError("读取出错,删除所有文件"); CFileManager.ClearDirectory(m_dir); } } finally { CFileManager.UnLockFileBuffer(); } } }
//-------------------------------------------------- /// 添加缓存文件 /// @url /// @cacheType /// @width /// @height /// @isGif /// @data //-------------------------------------------------- public void AddFile(string url, byte[] data, int tagInt1 = 0, int tagInt2 = 0, bool tagBool = false) { string key = CFileManager.GetMd5(url.ToLower()); List <CCachedFileInfo> cachedFileInfoList = m_cachedFileInfoSet.m_cachedFileInfos; if (cachedFileInfoList == null) { return; } if (m_cachedFileInfoSet.m_cachedFileInfoMap.ContainsKey(key)) { CCachedFileInfo cachedFileInfo = null; m_cachedFileInfoSet.m_cachedFileInfoMap.TryGetValue(key, out cachedFileInfo); Debug.Assert(cachedFileInfoList != null && cachedFileInfoList.Contains(cachedFileInfo), "zen me ke neng?"); //修改信息 cachedFileInfo.m_fileLength = data.Length; cachedFileInfo.m_lastModifyTime = DateTime.Now; cachedFileInfo.m_tagInt1 = tagInt1; cachedFileInfo.m_tagInt2 = tagInt2; cachedFileInfo.m_tagBool = tagBool; } else { //如果数量达到上限,移除排在最前面的文件 if (cachedFileInfoList.Count >= m_maxCnt) { string removeKey = m_cachedFileInfoSet.RemoveEarliestFileInfo(); //删除缓存文件 if (!string.IsNullOrEmpty(removeKey)) { string removeCachedFileFullPath = CFileManager.CombinePath(m_dir, removeKey + m_fileExtension); if (CFileManager.IsFileExist(removeCachedFileFullPath)) { CFileManager.DeleteFile(removeCachedFileFullPath); } } } CCachedFileInfo cachedFileInfo = new CCachedFileInfo(); cachedFileInfo.m_key = key; cachedFileInfo.m_fileLength = data.Length; cachedFileInfo.m_lastModifyTime = DateTime.Now; cachedFileInfo.m_tagInt1 = tagInt1; cachedFileInfo.m_tagInt2 = tagInt2; cachedFileInfo.m_tagBool = tagBool; m_cachedFileInfoSet.AddFileInfo(key, cachedFileInfo); } //排序 m_cachedFileInfoSet.m_cachedFileInfos.Sort(); //写入信息文件 byte[] buffer = CFileManager.LockFileBuffer(); try { MakeDirReady(); int offset = 0; m_cachedFileInfoSet.Write(buffer, ref offset); if (CFileManager.IsFileExist(m_metaFilePath)) { CFileManager.DeleteFile(m_metaFilePath); } CFileManager.WriteFile(m_metaFilePath, buffer, 0, offset); } finally { CFileManager.UnLockFileBuffer(); } //写入数据文件 string cachedFileFullPath = CFileManager.CombinePath(m_dir, key + m_fileExtension); if (CFileManager.IsFileExist(cachedFileFullPath)) { CFileManager.DeleteFile(cachedFileFullPath); } CFileManager.WriteFile(cachedFileFullPath, data); }