Example #1
0
        public FileCacheItem Deserialize(Stream stream)
        {
            var surrogateSelector = new AnonymousTypeSurrogateSelector();

            surrogateSelector.AddSurrogate(typeof(CacheItemPolicy), new StreamingContext(StreamingContextStates.All), new CacheItemPolicySurrogate());

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.SurrogateSelector = surrogateSelector;
            formatter.Binder            = _binder;

            FileCacheItem item = null;

            try
            {
                string          key     = (string)formatter.Deserialize(stream);
                CacheItemPolicy policy  = (CacheItemPolicy)formatter.Deserialize(stream);
                object          payload = formatter.Deserialize(stream);

                item = new FileCacheItem(key, policy, payload);
            }
            catch (SerializationException)
            {
            }

            return(item);
        }
Example #2
0
        public FileCacheItem Deserialize(Stream stream)
        {
            var surrogateSelector = new AnonymousTypeSurrogateSelector();
            surrogateSelector.AddSurrogate(typeof(CacheItemPolicy), new StreamingContext(StreamingContextStates.All), new CacheItemPolicySurrogate());

            BinaryFormatter formatter = new BinaryFormatter();
            formatter.SurrogateSelector = surrogateSelector;
            formatter.Binder = _binder;

            FileCacheItem item = null;

            try
            {
                string key = (string)formatter.Deserialize(stream);
                CacheItemPolicy policy = (CacheItemPolicy)formatter.Deserialize(stream);
                object payload = formatter.Deserialize(stream);

                item = new FileCacheItem(key, policy, payload);
            }
            catch (SerializationException)
            {

            }

            return item;
        }
Example #3
0
        /// <summary>
        /// Returns the policy attached to a given cache item.
        /// </summary>
        /// <param name="key">The key of the item.</param>
        /// <param name="regionName">The region in which the key exists.</param>
        public CacheItemPolicy GetPolicy(string key, string regionName = null)
        {
            string cacheItemPath = GetItemPath(key, regionName);

            FileCacheItem item = ReadFile(cacheItemPath);

            return(item.Policy);
        }
Example #4
0
        public void Serialize(Stream stream, FileCacheItem cacheItem)
        {
            var surrogateSelector = new AnonymousTypeSurrogateSelector();
            surrogateSelector.AddSurrogate(typeof(CacheItemPolicy), new StreamingContext(StreamingContextStates.All), new CacheItemPolicySurrogate());

            BinaryFormatter formatter = new BinaryFormatter();
            formatter.SurrogateSelector = surrogateSelector;

            formatter.Serialize(stream, cacheItem.Key);
            formatter.Serialize(stream, cacheItem.Policy);
            formatter.Serialize(stream, cacheItem.Payload);
        }
Example #5
0
        public void Serialize(Stream stream, FileCacheItem cacheItem)
        {
            var surrogateSelector = new AnonymousTypeSurrogateSelector();

            surrogateSelector.AddSurrogate(typeof(CacheItemPolicy), new StreamingContext(StreamingContextStates.All), new CacheItemPolicySurrogate());

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.SurrogateSelector = surrogateSelector;

            formatter.Serialize(stream, cacheItem.Key);
            formatter.Serialize(stream, cacheItem.Policy);
            formatter.Serialize(stream, cacheItem.Payload);
        }
Example #6
0
        /// <summary>
        /// This function serves to centralize file reads within this class.
        /// </summary>
        private FileCacheItem ReadFile(string filePath)
        {
            FileCacheItem item = null;

            if (File.Exists(filePath))
            {
                using (FileStream stream = GetStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    item = _serializer.Deserialize(stream);
                }
            }

            return(item);
        }
Example #7
0
        public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // Clone policy object
            policy = new CacheItemPolicy
            {
                AbsoluteExpiration = policy.AbsoluteExpiration,
                SlidingExpiration  = policy.SlidingExpiration
            };

            ValidatePolicy(policy);

            object oldData       = null;
            string cacheItemPath = GetItemPath(key, regionName);

            // Pull old value if it exists
            if (File.Exists(cacheItemPath))
            {
                try
                {
                    oldData = Get(key, regionName);
                }
                catch
                {
                    oldData = null;
                }
            }

            // Does the item have a sliding expiration?
            if (policy.SlidingExpiration > ObjectCache.NoSlidingExpiration)
            {
                policy.AbsoluteExpiration = DateTime.Now.Add(policy.SlidingExpiration);
            }

            FileCacheItem newItem = new FileCacheItem(key, policy, value);

            WriteFile(cacheItemPath, newItem);

            return(oldData);
        }
Example #8
0
        public override object Get(string key, string regionName = null)
        {
            string cacheItemPath = GetItemPath(key, regionName);

            FileCacheItem item = ReadFile(cacheItemPath);

            if (item == null)
            {
                return(null);
            }

            // Did the item expire?
            if (item.Policy.AbsoluteExpiration < DateTime.Now)
            {
                // Set the item to null
                item.Payload = null;

                FileInfo fi = new FileInfo(cacheItemPath);

                CurrentCacheSize -= fi.Length;

                // Delete the file from the cache
                fi.Delete();
            }
            else
            {
                // Does the item have a sliding expiration?
                if (item.Policy.SlidingExpiration > ObjectCache.NoSlidingExpiration)
                {
                    item.Policy.AbsoluteExpiration = DateTime.Now.Add(item.Policy.SlidingExpiration);;

                    WriteFile(cacheItemPath, item);
                }
            }

            return(item.Payload);
        }
Example #9
0
        /// <summary>
        /// This function serves to centralize file writes within this class
        /// </summary>
        private void WriteFile(string filePath, FileCacheItem data)
        {
            // Remove current item from cache size calculations
            if (File.Exists(filePath))
            {
                CurrentCacheSize -= new FileInfo(filePath).Length;
            }
            else
            {
                var directoryPath = Path.GetDirectoryName(filePath);
                var di            = new DirectoryInfo(directoryPath);

                if (!di.Exists)
                {
                    di.Create();
                }
            }

            // Write the object payload
            using (FileStream stream = GetStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                _serializer.Serialize(stream, data);
            }

            // Adjust cache size
            CurrentCacheSize += new FileInfo(filePath).Length;

            // Check to see if limit was reached
            if (CurrentCacheSize > MaxCacheSize)
            {
                if (MaxCacheSizeReached != null)
                {
                    MaxCacheSizeReached(this, new FileCacheEventArgs(CurrentCacheSize, MaxCacheSize));
                }
            }
        }