Esempio n. 1
0
        /// <summary>
        /// 构造函数,初始化具有Dependency的CacheItem
        /// </summary>
        /// <param name="tKey">初始化CacheItem的键值</param>
        /// <param name="data">CacheItem的Value</param>
        /// <param name="dependencyBase">与此CacheItem相关的Dependency,用以判断过期</param>
        /// <param name="cacheQueue"> 本CacheItem所属的CacheQueue的引用</param>
        public CacheItem(TKey tKey, TValue data, DependencyBase dependencyBase, CacheQueueBase cacheQueue)
            : base(cacheQueue)
        {
            this.key    = tKey;
            this.tValue = data;

            if (dependencyBase != null)
            {
                dependencyBase.CacheItem = this;
                dependencyBase.CacheItemBinded();
            }

            this.Dependency = dependencyBase;
        }
Esempio n. 2
0
        /// <summary>
        /// 向CacheQueue中增加一个有关联Dependency的Cache项,如果相应的key已经存在,则抛出异常
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="data">值</param>
        /// <param name="dependency">依赖:相对时间依赖、绝对时间依赖、文件依赖或混合依赖</param>
        /// <returns>值</returns>
        /// <remarks>
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Caching\CacheQueueTest.cs" region="AddRemoveClearTest" lang="cs" title="增加、移除、获取CacheItem项" />
        /// </remarks>
        public TValue Add(TKey key, TValue data, DependencyBase dependency)
        {
            this.rWLock.AcquireWriterLock(this.lockTimeout);
            try
            {
                InnerDictionary.MaxLength = this.MaxLength;

                //先删除已经存在而且过期的Cache项
                if (InnerDictionary.ContainsKey(key) &&
                    ((CacheItem <TKey, TValue>)InnerDictionary[key]).Dependency != null &&
                    ((CacheItem <TKey, TValue>)InnerDictionary[key]).Dependency.HasChanged)
                {
                    this.Remove(key);
                }

                CacheItem <TKey, TValue> item = new CacheItem <TKey, TValue>(key, data, dependency, this);

                if (dependency != null)
                {
                    dependency.UtcLastModified   = DateTime.UtcNow;
                    dependency.UtcLastAccessTime = DateTime.UtcNow;
                }

                if (this.overrideExistsItem)
                {
                    InnerDictionary[key] = item;
                }
                else
                {
                    InnerDictionary.Add(key, item);
                }

                this.Counters.EntriesCounter.RawValue = InnerDictionary.Count;

                return(data);
            }
            finally
            {
                this.rWLock.ReleaseWriterLock();
            }
        }