예제 #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));
        }
예제 #2
0
        protected virtual void RemoveCacheData(DiskOutputCacheItem item)
        {
            var fileToRetrieve = Path.Combine(this.CacheFolder, item.FileName);

            if (File.Exists(fileToRetrieve))
            {
                File.Delete(fileToRetrieve);
            }
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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 { }
            }
        }
예제 #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);
                }
            }
        }