Ejemplo n.º 1
0
        public void Initialize()
        {
            _clock = MockSystemClock.Get();
            _clock.SetDateTime(DateTime.Now);
            NoOpDiskTrimmableRegistry.Instance.ResetCounter();
            _diskTrimmableRegistry = NoOpDiskTrimmableRegistry.Instance;
            _cacheEventListener    = new DuplicatingCacheEventListener();

            // We know the directory will be this
            _cacheDirectory = new DirectoryInfo(
                Path.Combine(ApplicationData.Current.LocalCacheFolder.Path, CACHE_TYPE));
            _cacheDirectory.Create();
            if (!_cacheDirectory.Exists)
            {
                throw new Exception(
                          string.Format(
                              "Cannot create cache dir: {0}: directory {1}",
                              ApplicationData.Current.LocalCacheFolder.Path,
                              _cacheDirectory.Exists ? "already exists" : "does not exist"));
            }

            _storage = CreateDiskStorage(TESTCACHE_VERSION_START_OF_VERSIONING);
            _cache   = CreateDiskCache(_storage, false);
            Assert.IsTrue(((NoOpDiskTrimmableRegistry)_diskTrimmableRegistry).RegisterDiskTrimmableCount == 1);
        }
 internal DiskCacheConfig(Builder builder)
 {
     _version                         = builder._version;
     _baseDirectoryName               = Preconditions.CheckNotNull(builder._baseDirectoryName);
     _baseDirectoryPathSupplier       = Preconditions.CheckNotNull(builder._baseDirectoryPathSupplier);
     _defaultSizeLimit                = builder._maxCacheSize;
     _lowDiskSpaceSizeLimit           = builder._maxCacheSizeOnLowDiskSpace;
     _minimumSizeLimit                = builder._maxCacheSizeOnVeryLowDiskSpace;
     _entryEvictionComparatorSupplier = Preconditions.CheckNotNull(builder._entryEvictionComparatorSupplier);
     _cacheErrorLogger                = builder._cacheErrorLogger ?? NoOpCacheErrorLogger.Instance;
     _cacheEventListener              = builder._cacheEventListener ?? NoOpCacheEventListener.Instance;
     _diskTrimmableRegistry           = builder._diskTrimmableRegistry ?? NoOpDiskTrimmableRegistry.Instance;
     _indexPopulateAtStartupEnabled   = builder._indexPopulateAtStartupEnabled;
 }
 /// <summary>
 /// The class that will contain a registry of caches to be trimmed in
 /// low disk space conditions.
 ///
 /// <para />See <see cref="IDiskTrimmableRegistry"/>.
 /// </summary>
 public Builder SetDiskTrimmableRegistry(IDiskTrimmableRegistry diskTrimmableRegistry)
 {
     _diskTrimmableRegistry = diskTrimmableRegistry;
     return(this);
 }
        /// <summary>
        /// Instantiates the <see cref="DiskStorageCache"/>.
        /// </summary>
        public DiskStorageCache(
            IDiskStorage diskStorage,
            IEntryEvictionComparatorSupplier entryEvictionComparatorSupplier,
            Params parameters,
            ICacheEventListener cacheEventListener,
            ICacheErrorLogger cacheErrorLogger,
            IDiskTrimmableRegistry diskTrimmableRegistry,
            bool indexPopulateAtStartupEnabled,
            Clock clock = null)
        {
            _lowDiskSpaceCacheSizeLimit = parameters.LowDiskSpaceCacheSizeLimit;
            _defaultCacheSizeLimit      = parameters.DefaultCacheSizeLimit;
            _cacheSizeLimit             = parameters.DefaultCacheSizeLimit;
            _statFsHelper = StatFsHelper.Instance;

            _storage = diskStorage;

            _entryEvictionComparatorSupplier = entryEvictionComparatorSupplier;

            _cacheSizeLastUpdateTime = default(DateTime);

            _cacheEventListener = cacheEventListener;

            _cacheSizeLimitMinimum = parameters.CacheSizeLimitMinimum;

            _cacheErrorLogger = cacheErrorLogger;

            _cacheStats = new CacheStats();

            if (diskTrimmableRegistry != null)
            {
                diskTrimmableRegistry.RegisterDiskTrimmable(this);
            }

            _clock = clock ?? SystemClock.Get();

            _indexPopulateAtStartupEnabled = indexPopulateAtStartupEnabled;

            _resourceIndex = new HashSet <string>();

            if (_indexPopulateAtStartupEnabled)
            {
                Task.Run(() =>
                {
                    try
                    {
                        lock (_lock)
                        {
                            MaybeUpdateFileCacheSize();
                        }
                    }
                    finally
                    {
                        _countdownEvent.Signal();
                    }
                });
            }
            else
            {
                _countdownEvent.Reset(0);
            }
        }