Example #1
0
        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);
        }
Example #2
0
 /// <summary>
 /// Delete's the given item from the store.
 /// </summary>
 /// <param name="item">The item to delete</param>
 public abstract void Delete(CacheItemInfo item);
Example #3
0
 /// <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);
Example #4
0
 /// <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);
Example #5
0
            /// <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;
                }
            }
Example #6
0
            /// <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);
            }
Example #7
0
 public override void Reset()
 {
     base.Reset();
     _cacheItemInfo = null;
     _thereIsNoCacheItem = false;
 }
Example #8
0
 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;
 }
Example #9
0
            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;
            }
Example #10
0
 public FileItem(CacheItemInfo item)
 {
     Item = item;
 }
Example #11
0
        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);
            });
        }
Example #12
0
        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;
        }