Example #1
0
        public override object Get(string key)
        {
            LogAction("Get", string.Format("Key: {0}", key));

            DiskOutputCacheItem item = null;

            CacheItems.TryGetValue(key, out item);

            // Was the item found?
            if (item == null)
            {
                return(null);
            }

            // Has the item expired?
            if (item.UtcExpiry < DateTime.UtcNow)
            {
                // Item has expired
                this.Remove(key);

                return(null);
            }

            return(GetCacheData(item));
        }
Example #2
0
        protected virtual void RemoveCacheData(DiskOutputCacheItem item)
        {
            var fileToRetrieve = Path.Combine(this.CacheFolder, item.FileName);

            if (File.Exists(fileToRetrieve))
            {
                File.Delete(fileToRetrieve);
            }
        }
Example #3
0
        protected virtual void WriteCacheData(DiskOutputCacheItem item, object entry)
        {
            var fileToWrite = Path.Combine(this.CacheFolder, item.FileName);

            var formatter = new BinaryFormatter();

            using (var stream = new FileStream(fileToWrite, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                formatter.Serialize(stream, entry);
            }
        }
Example #4
0
        protected virtual object GetCacheData(DiskOutputCacheItem item)
        {
            var fileToRetrieve = Path.Combine(this.CacheFolder, item.FileName);

            if (File.Exists(fileToRetrieve))
            {
                var formatter = new BinaryFormatter();
                using (var stream = new FileStream(fileToRetrieve, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    return(formatter.Deserialize(stream));
                }
            }
            else
            {
                // Eep, could not find the file!
                return(null);
            }
        }
Example #5
0
        public override void Remove(string key)
        {
            LogAction("Remove", string.Format("Key: {0}", key));

            DiskOutputCacheItem item = null;

            this.CacheItems.TryGetValue(key, out item);

            if (item != null)
            {
                // Attempt to delete the cached content on disk and then remove the item from CacheItems...
                // If there is a problem, fail silently
                //try
                //{
                RemoveCacheData(item);
                CacheItems.Remove(key);
                //}
                //catch { }
            }
        }
Example #6
0
        public override void Set(string key, object entry, DateTime utcExpiry)
        {
            LogAction("Set", string.Format("Key: {0} | UtcExpiry: {1}", key, utcExpiry.ToString()));

            // Create a DiskOutputCacheItem object
            var item = new DiskOutputCacheItem(this.GetSafeFileName(key), utcExpiry);

            WriteCacheData(item, entry);

            // Add item to CacheItems, if needed, or update the existing key, if it already exists
            lock (_lockObject)
            {
                if (this.CacheItems.ContainsKey(key))
                {
                    this.CacheItems[key] = item;
                }
                else
                {
                    this.CacheItems.Add(key, item);
                }
            }
        }