public override void Delete(CacheItemInfo item) { CacheItemInfo cachedItem; lock (_cache) { if (_cache.TryGetValue(item.UniqueName, out cachedItem) && Object.Equals(item, cachedItem)) { _cache.Remove(item.UniqueName); } } var fi = new FileItem(item); var fileName = fi.FileName; File.Delete(fileName); }
/// <summary> /// Delete's the given item from the store. /// </summary> /// <param name="item">The item to delete</param> public abstract void Delete(CacheItemInfo item);
/// <summary> /// Write the item's data to the store. /// </summary> /// <param name="info"></param> /// <param name="data"></param> public abstract void Write(CacheItemInfo info, byte[] data);
/// <summary> /// Read the data for the given item. /// </summary> /// <param name="item">The info describing the item to read</param> /// <returns>The data in the store for the specified item.</returns> public abstract byte[] Read(CacheItemInfo item);
/// <summary> /// Look in the store for a recent entry that we can load from. /// </summary> /// <returns></returns> private bool FindCacheItem() { lock (this) { if (_cacheItemInfo == null && !_thereIsNoCacheItem) { _cacheItemInfo = DataManager.StoreProvider.GetLastestExpiringItem(CacheEntry.UniqueName); } if (_cacheItemInfo == null) { // flat failure. if (!_thereIsNoCacheItem) { _thereIsNoCacheItem = true; Debug.WriteLine("No cache found for {0} (ID={1})", CacheEntry.ObjectType, CacheEntry.LoadContext.Identity); } return false; } return true; } }
/// <summary> /// Save the specified value back to the disk. /// </summary> /// <param name="uniqueName"></param> /// <param name="data"></param> /// <param name="updatedTime"></param> /// <param name="expirationTime"></param> /// <param name="isOptimized"></param> public void Save(string uniqueName, byte[] data, DateTime updatedTime, DateTime expirationTime, bool isOptimized) { if (data == null) { throw new ArgumentNullException("data"); } _cacheItemInfo = new CacheItemInfo(uniqueName, updatedTime, expirationTime); _cacheItemInfo.IsOptimized = isOptimized; Data = null; LoadState = DataLoadState.None; Debug.WriteLine("Writing cache for {0} (ID={3}), IsOptimized={1}, Will expire {2}", CacheEntry.ObjectType.Name, _cacheItemInfo.IsOptimized, _cacheItemInfo.ExpirationTime, CacheEntry.LoadContext.Identity.ToString()); DataManager.StoreProvider.Write(_cacheItemInfo, data); }
public override void Reset() { base.Reset(); _cacheItemInfo = null; _thereIsNoCacheItem = false; }
private static string ToFileName(CacheItemInfo item) { string name = EncodePathName(item.UniqueName); name = String.Format("{1}{0}{2}{0}{3}{0}{4}", FileNameSeparator, name, item.IsOptimized, item.ExpirationTime.Ticks, item.UpdatedTime.Ticks); name = Path.Combine(DirectoryHash(item.UniqueName), name); return name; }
public static CacheItemInfo FromFileName(string fileName) { if (!fileName.StartsWith(CacheDirectoryPrefix)) { fileName = Path.GetFileName(fileName); string[] parts = fileName .Split(FileNameSeparator); if (parts.Length == 4) { string uniqueKey = DecodePathName(parts[0]); var item = new CacheItemInfo(uniqueKey) { ExpirationTime = new DateTime(Int64.Parse(parts[2])), UpdatedTime = new DateTime(Int64.Parse(parts[3])), IsOptimized = Boolean.Parse(parts[1]) }; return item; } } return null; }
public FileItem(CacheItemInfo item) { Item = item; }
public override void Write(CacheItemInfo info, byte[] data) { var fi = new FileItem(info); PriorityQueue.AddStorageWorkItem(async () => { FileItem.EnsurePath(fi.FileName); File.WriteAllBytes(fi.FileName, data); }); }
public override byte[] Read(CacheItemInfo item) { var fi = new FileItem(item); byte[] bytes = null; bool fileExists = File.Exists(fi.FileName); if (!fileExists) return null; using (Stream stream = File.Open(fi.FileName,FileMode.Open)) { bytes = new byte[stream.Length]; stream.Read(bytes, 0, (int)stream.Length); } return bytes; }