public ECollegeResponseCacheEntry Get(string cacheKey)
        {
            string dirPath = GetDirectoryForCacheKey(cacheKey);

            ECollegeResponseCacheEntry result = null;

            lock (dirPath)
            {
                if (storage.DirectoryExists(dirPath))
                {
                    string existingCacheFileName = storage.GetFileNames(GetFileGlobForCacheKey(cacheKey)).FirstOrDefault();

                    if (existingCacheFileName != null)
                    {
                        var existingCacheFilePath = string.Format("{0}\\{1}", dirPath, existingCacheFileName);

                        var expirationDate =
                            DateTime.FromFileTimeUtc(long.Parse(Path.GetFileNameWithoutExtension(existingCacheFileName)));

                        if (expirationDate >= DateTime.UtcNow)
                        {
                            using (var f = new IsolatedStorageFileStream(existingCacheFilePath, FileMode.Open, FileAccess.Read, storage))
                            {
                                using (var sr = new StreamReader(f))
                                {
                                    result          = new ECollegeResponseCacheEntry();
                                    result.CachedAt = DateTime.Now;
                                    result.Data     = sr.ReadToEnd();
                                }
                            }
                        }
                        else
                        {
                            storage.DeleteFile(existingCacheFilePath);
                            storage.DeleteDirectory(dirPath);
                        }
                    }
                    else
                    {
                        storage.DeleteDirectory(dirPath);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public ECollegeResponseCacheEntry Get(string scope, string cacheKey, TimeSpan?expiration)
        {
            ECollegeResponseCacheEntry result = null;

            string scopePath = GetDirectoryForScope(scope);

            lock (scopePath)
            {
                if (_storage.DirectoryExists(scopePath))
                {
                    string cacheEntryPath = GetDirectoryForCacheEntry(scope, cacheKey);

                    lock (cacheEntryPath)
                    {
                        if (_storage.DirectoryExists(cacheEntryPath))
                        {
                            string matchingCacheFileName = GetLatestFileForCacheEntry(scope, cacheKey, expiration);

                            if (matchingCacheFileName != null)
                            {
                                var matchingCacheFilePath = string.Format("{0}\\{1}", cacheEntryPath, matchingCacheFileName);

                                using (var f = new IsolatedStorageFileStream(matchingCacheFilePath, FileMode.Open, FileAccess.Read, _storage))
                                {
                                    using (var sr = new StreamReader(f))
                                    {
                                        result          = new ECollegeResponseCacheEntry();
                                        result.CachedAt = DateTime.Now;
                                        result.Data     = sr.ReadToEnd();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }