Пример #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;
        }
Пример #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\PortableCacheQueueTest.cs" region="AddRemoveClearTest" lang="cs" title="增加、移除、获取CahceItem项" />
        /// </remarks>
        public TValue Add(TKey key, TValue data, DependencyBase dependency)
        {
            key = ConvertCacheKey(key);

            return(this._rwLock.DoWriteFunc(() =>
            {
                //删除已经存在而且过期的Cache项
                if (this._innerDictionary.ContainsKey(key) &&
                    ((CacheItem <TKey, TValue>) this._innerDictionary[key]).Dependency != null &&
                    ((CacheItem <TKey, TValue>) this._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)
                {
                    this._innerDictionary[key] = item;
                }
                else
                {
                    this._innerDictionary.Add(key, item);
                }

#if NetFramework
                this.Counters.EntriesCounter.RawValue = this.innerDictionary.Count;
#endif

                return data;
            }));
        }