Exemplo n.º 1
0
        /// <summary>
        /// Attempts to load a byte array from the medium.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="persistedItem">The persisted item.</param>
        /// <returns>true if successful, false otherwise.</returns>
        public bool TryLoad(string cacheKey, out PersistedItem persistedItem)
        {
            // Sanitize
            if (string.IsNullOrWhiteSpace(cacheKey))
            {
                throw new ArgumentException("cannot be null, empty, or white space", "cacheKey");
            }

            // Find the right file
            var cacheKeyHashCode = CalculateHash(cacheKey);

            foreach (var fileName in Directory.GetFiles(_persistenceDirectory, string.Format("{0}-*", cacheKeyHashCode)))
            {
                byte[] file = null;

                // Get the lock
                var cacheKeyLock = _modulusLockDictionary[cacheKeyHashCode % _modulusLockDictionary.Count];
                cacheKeyLock.EnterReadLock();
                try
                {
                    file = File.ReadAllBytes(fileName);
                }
                finally
                {
                    cacheKeyLock.ExitReadLock();
                }

                var deserialized = (PersistedItem)Deserialize(file);
                if (string.Equals(cacheKey, deserialized.CacheKey))
                {
                    persistedItem = deserialized;
                    return(true);
                }
            }

            // Failed
            persistedItem = null;
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to load a byte array from the medium.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="persistedItem">The persisted item.</param>
        /// <returns>true if successful, false otherwise.</returns>
        public bool TryLoad(string cacheKey, out PersistedItem persistedItem)
        {
            // Sanitize
            if (string.IsNullOrWhiteSpace(cacheKey))
            {
                throw new ArgumentException("cannot be null, empty, or white space", "cacheKey");
            }

            // Find the right file
            var cacheKeyHashCode = CalculateHash(cacheKey);
            foreach (var fileName in Directory.GetFiles(_persistenceDirectory, string.Format("{0}-*", cacheKeyHashCode)))
            {
                byte[] file = null;

                // Get the lock
                var cacheKeyLock = _modulusLockDictionary[cacheKeyHashCode % _modulusLockDictionary.Count];
                cacheKeyLock.EnterReadLock();
                try
                {
                    file = File.ReadAllBytes(fileName);    
                }
                finally
                {
                    cacheKeyLock.ExitReadLock();
                }
                
                var deserialized = (PersistedItem)Deserialize(file);
                if (string.Equals(cacheKey, deserialized.CacheKey))
                {
                    persistedItem = deserialized;
                    return true;
                }
            }

            // Failed
            persistedItem = null;
            return false;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Persists a byte array to the medium.
        /// </summary>
        /// <param name="persistedItem">The persisted item.</param>
        public void Persist(PersistedItem persistedItem)
        {
            // Sanitize
            if (persistedItem == null)
            {
                throw new ArgumentNullException("persistedItem");
            }

            var cacheKeyHashCode = CalculateHash(persistedItem.CacheKey);
            var filePath = Path.Combine(_persistenceDirectory, string.Format("{0}-{1}", cacheKeyHashCode, CalculateHash(persistedItem.Value)));

            // Get the lock
            var cacheKeyLock = _modulusLockDictionary[cacheKeyHashCode % _modulusLockDictionary.Count];
            cacheKeyLock.EnterWriteLock();
            try
            {
                File.WriteAllBytes(filePath, Serialize(persistedItem));
            }
            finally
            {
                cacheKeyLock.ExitWriteLock();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Persists a byte array to the medium.
        /// </summary>
        /// <param name="persistedItem">The persisted item.</param>
        public void Persist(PersistedItem persistedItem)
        {
            // Sanitize
            if (persistedItem == null)
            {
                throw new ArgumentNullException("persistedItem");
            }

            var cacheKeyHashCode = CalculateHash(persistedItem.CacheKey);
            var filePath         = Path.Combine(_persistenceDirectory, string.Format("{0}-{1}", cacheKeyHashCode, CalculateHash(persistedItem.Value)));

            // Get the lock
            var cacheKeyLock = _modulusLockDictionary[cacheKeyHashCode % _modulusLockDictionary.Count];

            cacheKeyLock.EnterWriteLock();
            try
            {
                File.WriteAllBytes(filePath, Serialize(persistedItem));
            }
            finally
            {
                cacheKeyLock.ExitWriteLock();
            }
        }