Пример #1
0
 /// <summary>
 /// 初始化
 /// </summary>
 public void Init()
 {
     lock (_cacheFileLibraryContent)
     {
         //todo 读取缓存库
         if (!_isInit)
         {
             _isInit = true;
             if (!Utility.FileUtil.IsFileExists(GetCacheLibraryFilePath()))
             {
                 return;
             }
             var libraryInfo = File.ReadAllLines(GetCacheLibraryFilePath());
             foreach (var line in libraryInfo)
             {
                 var entity = FileCacheEntity.Deserialization(line);
                 _cacheFileLibraryContent.Add(_getKey(entity.Url, entity.FilePath), entity);
             }
             _clearCacheLibrary();
         }
     }
 }
Пример #2
0
        private void _addOrUpdateCache(DownloadUnitInfo info)
        {
            _sb.Clear();
            var   cacheTime        = (ulong)DateTime.Now.Ticks;
            ulong cacheExpiredTime = 0;

            if (info.CacheExpiredDay > 0)
            {
                DateTime centuryBegin = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + CacheExpiredDay);
                cacheExpiredTime = (ulong)centuryBegin.Ticks;
            }

            var expiredTime = cacheExpiredTime;

            if (!_hasCache(info))
            {
                _cacheFileLibraryContent.Add(_getKey(info), new FileCacheEntity()
                {
                    Url         = info.Url,
                    FilePath    = Utility.FileUtil.UpdatePathDirectorySeparator(Path.Combine(info.SavePath, info.FileName)),
                    CacheTime   = cacheTime,
                    ExpiredTime = expiredTime
                });
            }
            else
            {
                //更新缓存的缓存时间和过期时间
                _cacheFileLibraryContent[_getKey(info)].CacheTime   = cacheTime;
                _cacheFileLibraryContent[_getKey(info)].ExpiredTime = expiredTime;
            }
            foreach (var cacheEntity in _cacheFileLibraryContent)
            {
                _sb.AppendLine(FileCacheEntity.Serialization(cacheEntity.Value));
            }
            File.WriteAllText(GetCacheLibraryFilePath(), _sb.ToString());
        }