Exemplo n.º 1
0
        /// <summary>
        /// Create a new instance
        /// Only the CacheManager can initialise them.
        /// </summary>
        /// <param name="name">The name of the cache. Note that "default" is a reserved name for the defaultCache.</param>
        /// <param name="maxElementsInMemory">The maximum number of elements in memory, before they are evicted.</param>
        /// <param name="memoryStoreEvictionPolicy">One of LRU, LFU and FIFO. Optionally null, in which case it will be set to LRU.</param>
        /// <param name="overflowToDisk">Whether to use the disk store.</param>
        /// <param name="diskStorePath">This parameter is ignored. CacheManager sets it using setter injection.</param>
        /// <param name="eternal">Whether the elements in the cache are eternal, i.e. never expire.</param>
        /// <param name="timeToLiveSeconds">The default amount of time to live for an element from its creation date.</param>
        /// <param name="timeToIdleSeconds">The default amount of time to live for an element from its last accessed or modified date.</param>
        /// <param name="diskPersistent">Whether to persist the cache to disk between JVM restarts.</param>
        /// <param name="diskExpiryThreadIntervalSeconds">How often to run the disk store expiry thread. A large number of 120 seconds plus is recommended.</param>
        /// <param name="maxElementsOnDisk">The maximum number of elements on disk, before they are evicted.</param>
        /// <param name="diskSpoolBufferSizeMB">The amount of memory to allocate the write buffer for puts to the DiskStore.</param>
        internal Cache(
            string name,
            int maxElementsInMemory,
            MemoryStoreEvictionPolicy memoryStoreEvictionPolicy,
            bool overflowToDisk,
            string diskStorePath,
            bool eternal,
            long timeToLiveSeconds,
            long timeToIdleSeconds,
            bool diskPersistent,
            long diskExpiryThreadIntervalSeconds,
            int maxElementsOnDisk,
            int diskSpoolBufferSizeMB)
        {
            this.ChangeStatus(Status.Uninitialized);

            _guid = Guid.NewGuid();

            _configuration      = new CacheConfiguration();
            _configuration.Name = name;
            _configuration.MaxElementsInMemory = maxElementsInMemory;
            _configuration.EvictionPolicy      = memoryStoreEvictionPolicy;
            _configuration.IsOverflowToDisk    = overflowToDisk;
            _configuration.IsEternal           = eternal;
            _configuration.TimeToLiveSeconds   = timeToLiveSeconds;
            _configuration.TimeToIdleSeconds   = timeToIdleSeconds;
            _configuration.DiskPersistent      = diskPersistent;
            _configuration.MaxElementsOnDisk   = maxElementsOnDisk;

            if (diskStorePath == null)
            {
                _configuration.DiskStorePath = ".";
            }
            else
            {
                _configuration.DiskStorePath = diskStorePath;
            }

            // Set this to a safe value.
            if (diskExpiryThreadIntervalSeconds == 0)
            {
                _configuration.DiskExpiryThreadIntervalSeconds = DefaultExpiryThreadIntervalSeconds;
            }
            else
            {
                _configuration.DiskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds;
            }

            if (diskSpoolBufferSizeMB == 0)
            {
                _configuration.DiskSpoolBufferSizeMB = DefaultSpoolBufferSize;
            }
            else
            {
                _configuration.DiskSpoolBufferSizeMB = diskSpoolBufferSizeMB;
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Crée un nouveau cache.
        /// </summary>
        /// <param name="cacheName">Nom du cache.</param>
        /// <returns>Cache.</returns>
        private Cache CreateCache(string cacheName)
        {
            CacheConfigElement element = _configElement.Caches[cacheName];

            if (element == null)
            {
                element = _defaultElement;
            }

            MemoryStoreEvictionPolicy policy = MemoryStoreEvictionPolicy.Lru;

            try {
                policy = (MemoryStoreEvictionPolicy)Enum.Parse(
                    typeof(MemoryStoreEvictionPolicy),
                    element.MemoryStoreEvictionPolicy);
            } catch (ArgumentException) {
                policy = MemoryStoreEvictionPolicy.Lru;
            }

            Cache cache = new Cache(
                cacheName,
                element.MaxElementsInMemory,
                policy,
                element.IsOverflowToDisk,
                _configElement.DiskStorePath,
                element.IsEternal,
                element.TimeToLiveSeconds,
                element.TimeToIdleSeconds,
                element.DiskPersistent,
                element.DiskExpiryThreadIntervalSeconds,
                element.MaxElementsOnDisk,
                element.DiskSpoolBufferSizeMB);

            cache.Init();
            return(cache);
        }