Example #1
0
        /// <summary>
        /// Deletes the item from the cache identified by it's <see cref="ICache.Id" />
        /// </summary>
        /// <param name="cacheId">The unique id of the cache to remove item from </param>
        /// <param name="key">The key associated with the cached item to be removed</param>
        /// <returns>True if a cache was found matching the id supplied</returns>
        /// <remarks>
        /// <c>true</c> will be returned even if the item was not in the cache
        /// </remarks>
        /// <exception cref="ArgumentException">Cache is paused</exception>
        public bool RemoveItem(CacheIdentity cacheId, string key)
        {
            ICache cache = AllCaches.SingleOrDefault(c => c.Id == cacheId);

            if (cache == null)
            {
                return(false);
            }

            if (cache.As <IPausableCache>() != null && cache.As <IPausableCache>().IsPaused)
            {
                throw new ArgumentException(string.Format("Cannot remove item from paused cache ('{0}')", cacheId));
            }

            cache.Remove(key);
            return(true);
        }
Example #2
0
        private static void LoadLocalCaches(string directoryPath)
        {
            string cachePath;

            cachePath = Path.Combine(directoryPath, ".nlyric");
            if (File.Exists(cachePath))
            {
                _allCaches = JsonConvert.DeserializeObject <AllCaches>(File.ReadAllText(cachePath));
                Logger.Instance.LogInfo($"搜索缓存\"{cachePath}\"已被加载。");
            }
            else
            {
                _allCaches = new AllCaches()
                {
                    AlbumCaches = new List <AlbumCache>(),
                    LyricCaches = new List <LyricCache>(),
                    TrackCaches = new List <TrackCache>()
                }
            };
        }
Example #3
0
        /// <summary>
        /// Register the <paramref name="cache"/>
        /// </summary>
        /// <exception cref="ArgumentException">
        /// <returns>The <paramref name="cache"/> extended with <see cref="IStatisticsCache"/> behaviour</returns>
        /// Where a cache with the same <see cref="ICache.Id"/> has already been registered</exception>
        /// <remarks>
        /// Any cache registered will be extended with the <see cref="IStatisticsCache"/> behaviour if not already
        /// </remarks>
        public virtual ICache Register(ICache cache)
        {
            if (cache == null)
            {
                throw new ArgumentNullException("cache");
            }

            //don't add special case null instances
            if (cache.As <INullCache>() != null)
            {
                return(cache);
            }

            if (AllCaches.Any(c => c.Id == cache.Id))
            {
                throw new ArgumentException(string.Format("A cache has already been registered with the Id '{0}'",
                                                          cache.Id));
            }
            bool hasExistingStatisticsDecorator = cache.IsDecoratedWith <IStatisticsCache>();

            cache             = DecorateWithStatistics(cache);
            _allCaches[cache] = !hasExistingStatisticsDecorator;
            return(cache);
        }
 static DispatcherContext()
 {
     AllCaches.Init();
 }
Example #5
0
 public void SaveCaches()
 {
     AllCaches.SaveChanges();
 }