Beispiel #1
0
        /// <summary>
        /// Creates a disk store.
        /// </summary>
        /// <param name="cache">The Cache that the store is part of.</param>
        /// <param name="diskPath">The directory in which to create data and index files.</param>
        public DiskStore(IEhcache cache, string diskPath)
        {
            this._cache    = cache;
            this._diskPath = diskPath;
            this._name     = cache.Name;

            _expiryThreadInterval = _cache.Configuration.DiskExpiryThreadIntervalSeconds;
            _persistent           = _cache.Configuration.DiskPersistent;
            _maxElementsOnDisk    = _cache.Configuration.MaxElementsOnDisk;
            _eternal = _cache.Configuration.IsEternal;
            _diskSpoolBufferSizeBytes = _cache.Configuration.DiskSpoolBufferSizeMB * OneMegabyte;

            try {
                this.InitialiseFiles();
                _active = true;

                // Always start up the spool thread
                _spoolAndExpiryResetEvent = new ManualResetEvent(false);
                ThreadStart threadStart = new ThreadStart(this.SpoolAndExpiryThreadMain);
                _spoolAndExpiryThread              = new Thread(threadStart);
                _spoolAndExpiryThread.Name         = "Store " + _name + " Spool Thread";
                _spoolAndExpiryThread.Priority     = ThreadPriority.Normal;
                _spoolAndExpiryThread.IsBackground = true;
                _spoolAndExpiryThread.Start();
            } catch (IOException e) {
                this.Dispose();
                _log.Error(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        SR.ErrorCouldNotCreateStore,
                        _name,
                        e.Message),
                    e);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Constructs things that all MemoryStores have in common.
        /// </summary>
        /// <param name="cache">Parent Cache.</param>
        /// <param name="diskStore">Associated Disk store.</param>
        protected MemoryStore(IEhcache cache, IStore diskStore)
        {
            this._diskStore = diskStore;
            this.Cache      = cache;

            if (_log.IsDebugEnabled)
            {
                _log.Debug("Initialized " + this.GetType().Name + " for " + cache.Name);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Constructor for the LruMemoryStore object.
        /// </summary>
        /// <param name="cache">Cache using this store.</param>
        /// <param name="diskStore">Associated DiskStore.</param>
        public LruMemoryStore(IEhcache cache, IStore diskStore)
            : base(cache, diskStore)
        {
            if (_log.IsDebugEnabled)
            {
                _log.Debug(cache.Name + " Cache: Using SpoolingLruDictionary implementation");
            }

            this.Map = new SpoolingLruDictionary(this, cache.MaxElementsInMemory);
        }
Beispiel #4
0
        /// <summary>
        /// A factory method to create a MemoryStore.
        /// </summary>
        /// <param name="cache">Parent Cache.</param>
        /// <param name="diskStore">Associated Disk store.</param>
        /// <returns>Nouvelle instance.</returns>
        internal static MemoryStore Create(IEhcache cache, IStore diskStore)
        {
            MemoryStore memoryStore          = null;
            MemoryStoreEvictionPolicy policy = cache.MemoryStoreEvictionPolicy;

            switch (policy)
            {
            case MemoryStoreEvictionPolicy.Lru:
                memoryStore = new LruMemoryStore(cache, diskStore);
                break;

            default:
                throw new NotImplementedException();
            }

            return(memoryStore);
        }
Beispiel #5
0
        /// <summary>
        /// Shuts down the disk store in preparation for cache shutdown.
        ///
        /// If a VM crash happens, the shutdown hook will not run. The data file and the index file
        /// will be out of synchronisation. At initialisation we always delete the index file
        /// after we have read the elements, so that it has a zero length. On a dirty restart, it still will have
        /// and the data file will automatically be deleted, thus preserving safety.
        /// </summary>
        public void Dispose()
        {
            // Close the cache
            _active = false;
            try {
                this.Flush();

                _spoolAndExpiryResetEvent.Set();
                _spoolAndExpiryThread.Join();

                ((IDisposable)_spoolAndExpiryResetEvent).Dispose();

                _spool.Clear();
                _diskElements.Clear();
                _freeSpace.Clear();
                if (_randomAccessFile != null)
                {
                    _randomAccessFile.Close();
                    _randomAccessFile.Dispose();
                }

                if (!_persistent)
                {
                    _log.Debug("Deleting file " + _dataFile.Name);
                    _dataFile.Delete();
                }
            } catch (IOException e) {
                _log.Error(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        SR.ErrorCouldNotShutdownStore,
                        _name,
                        e.Message),
                    e);
            }

            _randomAccessFile = null;
            _cache            = null;
            GC.SuppressFinalize(this);
        }