/// <summary>
        /// Retrieves the file corresponding to the key, if it is in the cache.
        /// Also touches the item, thus changing its LRU timestamp. If the file
        /// is not present in the file cache, returns null.
        /// <para />
        /// This should NOT be called on the UI thread.
        /// </summary>
        /// <param name="key">The key to check.</param>
        /// <returns>
        /// The resource if present in cache, otherwise null.
        /// </returns>
        public IBinaryResource GetResource(ICacheKey key)
        {
            SettableCacheEvent cacheEvent = SettableCacheEvent
                                            .Obtain()
                                            .SetCacheKey(key);

            try
            {
                lock (_lock)
                {
                    IBinaryResource resource    = null;
                    IList <string>  resourceIds = CacheKeyUtil.GetResourceIds(key);
                    string          resourceId  = default(string);
                    foreach (var entry in resourceIds)
                    {
                        resourceId = entry;
                        cacheEvent.SetResourceId(resourceId);
                        resource = _storage.GetResource(resourceId, key);
                        if (resource != null)
                        {
                            break;
                        }
                    }

                    if (resource == null)
                    {
                        _cacheEventListener.OnMiss(cacheEvent);
                        _resourceIndex.Remove(resourceId);
                    }
                    else
                    {
                        _cacheEventListener.OnHit(cacheEvent);
                        _resourceIndex.Add(resourceId);
                    }

                    return(resource);
                }
            }
            catch (IOException ioe)
            {
                _cacheErrorLogger.LogError(
                    CacheErrorCategory.GENERIC_IO,
                    typeof(DiskStorageCache),
                    "GetResource");
                cacheEvent.SetException(ioe);
                _cacheEventListener.OnReadException(cacheEvent);
                return(null);
            }
            finally
            {
                cacheEvent.Recycle();
            }
        }
Example #2
0
        internal void CreateRootDirectoryIfNecessary(FileSystemInfo rootDirectory)
        {
            try
            {
                FileUtils.Mkdirs(rootDirectory);
            }
            catch (CreateDirectoryException)
            {
                _cacheErrorLogger.LogError(
                    CacheErrorCategory.WRITE_CREATE_DIR,
                    typeof(DynamicDefaultDiskStorage),
                    "createRootDirectoryIfNecessary");

                throw;
            }

            Debug.WriteLine($"Created cache directory { rootDirectory.FullName }");
        }
Example #3
0
        private static bool CheckExternal(FileSystemInfo directory, ICacheErrorLogger cacheErrorLogger)
        {
            try
            {
                // Get the logical root folder for all external storage devices.
                StorageFolder externalDevices = KnownFolders.RemovableDevices;

                if (directory.FullName.Contains(externalDevices.Path))
                {
                    return(true);
                }
            }
            catch (Exception)
            {
                cacheErrorLogger.LogError(
                    CacheErrorCategory.OTHER,
                    typeof(DefaultDiskStorage),
                    "Failed to read folder to check if external: " + directory.Name);
            }

            return(false);
        }