Exemplo n.º 1
0
        /// <inheritdoc />
        public bool TryGetValue(string key, out object value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (InnerDictionary != null)
            {
                return(InnerDictionary.TryGetValue(key, out value));
            }
            else if (Properties != null)
            {
                for (var i = 0; i < Properties.Length; i++)
                {
                    var property = Properties[i];
                    if (Comparer.Equals(property.Name, key))
                    {
                        value = property.ValueGetter(Value);
                        return(true);
                    }
                }

                value = null;
                return(false);
            }
            else
            {
                value = null;
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 通过key,获取Cache项的value,如果相应的cache项存在的话
        /// 则将cache项的value作为输出参数,返回给客户端代码
        /// </summary>
        /// <param name="key">cache项的key</param>
        /// <param name="data">cache项的value</param>
        /// <returns>如果CacheQueue中包含此Cache项,则返回true,否则返回false</returns>
        /// <remarks>
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Caching\CacheQueueTest.cs" region="GetCacheItemTest" lang="cs" title="通过key,获取Cache项的value" />
        /// </remarks>
        public bool TryGetValue(TKey key, out TValue data)
        {
            data = default(TValue);
            CacheItem <TKey, TValue> item;
            bool result;

            this.TotalCounters.HitRatioBaseCounter.Increment();
            this.Counters.HitRatioBaseCounter.Increment();

            this.rWLock.AcquireReaderLock(this.lockTimeout);
            try
            {
                result = InnerDictionary.TryGetValue(key, out item);
            }
            finally
            {
                this.rWLock.ReleaseReaderLock();
            }

            if (result)
            {
                this.rWLock.AcquireWriterLock(this.lockTimeout);
                try
                {
                    //判断cache项是否过期
                    if (this.CheckDependencyChanged(key, item))
                    {
                        result = false;
                    }
                    else
                    {
                        data = item.Value;
                        //修改Cache项的最后访问时间
                        if (item.Dependency != null)
                        {
                            item.Dependency.UtcLastAccessTime = DateTime.UtcNow;
                        }
                    }
                }
                finally
                {
                    this.rWLock.ReleaseWriterLock();
                }
            }

            if (result)
            {
                this.TotalCounters.HitsCounter.Increment();
                this.TotalCounters.HitRatioCounter.Increment();
                this.Counters.HitsCounter.Increment();
                this.Counters.HitRatioCounter.Increment();
            }
            else
            {
                this.TotalCounters.MissesCounter.Increment();
                this.Counters.MissesCounter.Increment();
            }

            return(result);
        }
Exemplo n.º 3
0
            bool IReadOnlyDictionary <TDestinationKey, TDestination> .TryGetValue(TDestinationKey key, out TDestination value)
            {
                bool result = InnerDictionary.TryGetValue((TSourceKey)key, out TSource _value);

                value = _value;

                return(result);
            }
Exemplo n.º 4
0
        protected override bool TryGetValue(keyˈ key, out valueˈ value)
        {
            value = default(valueˈ);

            if (!InnerDictionary.TryGetValue(key, out var innerValue))
            {
                return(false);
            }

            value = Projection(key, innerValue);
            return(true);
        }
        protected override bool TryGetValue(keyˈ key, out valueˈ value)
        {
            if (!InnerDictionary.TryGetValue(key, out value))
            {
                return(false);
            }

            if (Filter(key, value))
            {
                return(true);
            }

            value = default(valueˈ);
            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 通过key,删除一Cache项
        /// </summary>
        /// <param name="key">缓存唯一标识</param>
        /// <remarks>
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Caching\CacheQueueTest.cs" region="AddRemoveClearTest" lang="cs" title="增加、移除、获取CacheItem项" />
        /// </remarks>
        public void Remove(TKey key)
        {
            this.DoWriteAction(() =>
            {
                key = ConvertCacheKey(key);

                CacheItem <TKey, TValue> item;

                if (InnerDictionary.TryGetValue(key, out item))
                {
                    this.InnerRemove(key, item);
                }

                //this.Counters.EntriesCounter.RawValue = InnerDictionary.Count;
            });
        }
Exemplo n.º 7
0
        /// <summary>
        /// 通过key,删除一Cache项
        /// </summary>
        /// <param name="key">缓存唯一标识</param>
        /// <remarks>
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Caching\CacheQueueTest.cs" region="AddRemoveClearTest" lang="cs" title="增加、移除、获取CacheItem项" />
        /// </remarks>
        public void Remove(TKey key)
        {
            CacheItem <TKey, TValue> item;

            this.rWLock.AcquireWriterLock(this.lockTimeout);
            try
            {
                if (InnerDictionary.TryGetValue(key, out item))
                {
                    this.InnerRemove(key, item);
                }

                this.Counters.EntriesCounter.RawValue = InnerDictionary.Count;
            }
            finally
            {
                this.rWLock.ReleaseWriterLock();
            }
        }
 public bool TryGetValue(string key, out object value)
 {
     return(InnerDictionary.TryGetValue(key, out value));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Gets the value associated with the specified key.
 /// </summary>
 /// <param name="key">The key of the value to get.</param>
 /// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise,
 /// the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
 /// <returns>True if the Dictionary contains an element with the specified key; otherwise, false.</returns>
 public bool TryGetValue(TKey key, out TValue value)
 {
     return(InnerDictionary.TryGetValue(key, out value));
 }
Exemplo n.º 10
0
 /// <inheritdoc />
 public bool TryGetValue(TKey key, out TValue value)
 {
     using (writeLock.LockRead())
         return(InnerDictionary.TryGetValue(key, out value));
 }
Exemplo n.º 11
0
 public bool TryGetValue(string key, out MethodParameterValue value)
 {
     return(InnerDictionary.TryGetValue(key, out value));
 }
Exemplo n.º 12
0
        /// <summary>
        /// 通过Cache项的key获取Cache项Value的索引器
        /// </summary>
        /// <param name="key">cache项key</param>
        /// <returns>cache项Value</returns>
        /// <remarks>
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Caching\CacheQueueTest.cs" region="GetCacheItemTest" lang="cs" title="通过Cache项的key获取Cache项Value" />
        /// </remarks>
        public TValue this[TKey key]
        {
            get
            {
                CacheItem <TKey, TValue> item = null;
                this.TotalCounters.HitRatioBaseCounter.Increment();
                this.Counters.HitRatioBaseCounter.Increment();

                try
                {
                    this.rWLock.AcquireReaderLock(this.lockTimeout);
                    try
                    {
                        item = InnerDictionary[key];
                    }
                    finally
                    {
                        this.rWLock.ReleaseReaderLock();
                    }

                    this.rWLock.AcquireWriterLock(this.lockTimeout);
                    try
                    {
                        //如果Cache项过期则抛出异常
                        if (this.CheckDependencyChanged(key, item))
                        {
                            throw new DependencyChangedException(string.Format(Resource.DependencyChanged, key, item.Dependency));
                        }

                        //重置cache项的最后访问时间
                        if (item.Dependency != null)
                        {
                            item.Dependency.UtcLastAccessTime = DateTime.UtcNow;
                        }
                    }
                    finally
                    {
                        this.rWLock.ReleaseWriterLock();
                    }

                    this.TotalCounters.HitsCounter.Increment();
                    this.TotalCounters.HitRatioCounter.Increment();
                    this.Counters.HitsCounter.Increment();
                    this.Counters.HitRatioCounter.Increment();

                    return(InnerDictionary[key].Value);
                }
                catch (System.Exception)
                {
                    this.TotalCounters.MissesCounter.Increment();
                    this.Counters.MissesCounter.Increment();
                    throw;
                }
            }
            set
            {
                this.rWLock.AcquireWriterLock(this.lockTimeout);
                try
                {
                    CacheItem <TKey, TValue> item;

                    if (InnerDictionary.TryGetValue(key, out item) == false)
                    {
                        this.Add(key, value);
                        item = InnerDictionary[key];
                    }
                    else
                    {
                        item.Value = value;
                    }

                    item.UpdateDependencyLastModifyTime();
                }
                finally
                {
                    this.rWLock.ReleaseWriterLock();
                }
            }
        }
Exemplo n.º 13
0
        private bool InnerTryGetValue(TKey key, out TValue data)
        {
            data = default(TValue);
            CacheItem <TKey, TValue> item;
            bool result;

            //this.TotalCounters.HitRatioBaseCounter.Increment();
            //this.Counters.HitRatioBaseCounter.Increment();

            this.rWLock.EnterReadLock();
            try
            {
                //读操作时,也会修改字典(调整次序),因此需要锁住字典,进行并发控制
                lock (InnerDictionary.SyncRoot)
                {
                    result = InnerDictionary.TryGetValue(key, out item);
                }
            }
            finally
            {
                this.rWLock.ExitReadLock();
            }

            if (result)
            {
                this.rWLock.EnterWriteLock();
                try
                {
                    //判断cache项是否过期
                    if (this.GetDependencyChanged(key, item))
                    {
                        result = false;
                    }
                    else
                    {
                        data = item.Value;
                        //修改Cache项的最后访问时间
                        if (item.Dependency != null)
                        {
                            item.Dependency.UtcLastAccessTime = DateTime.UtcNow;
                        }
                    }
                }
                finally
                {
                    this.rWLock.ExitWriteLock();
                }
            }

            //if (result)
            //{
            //    this.TotalCounters.HitsCounter.Increment();
            //    this.TotalCounters.HitRatioCounter.Increment();
            //    this.Counters.HitsCounter.Increment();
            //    this.Counters.HitRatioCounter.Increment();
            //}
            //else
            //{
            //    this.TotalCounters.MissesCounter.Increment();
            //    this.Counters.MissesCounter.Increment();
            //}

            return(result);
        }
Exemplo n.º 14
0
        /// <summary>
        /// 通过Cache项的key获取Cache项Value的索引器
        /// </summary>
        /// <param name="key">cache项key</param>
        /// <returns>cache项Value</returns>
        /// <remarks>
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Caching\CacheQueueTest.cs" region="GetCacheItemTest" lang="cs" title="通过Cache项的key获取Cache项Value" />
        /// </remarks>
        public TValue this[TKey key]
        {
            get
            {
                try

                {
                    //this.TotalCounters.HitRatioBaseCounter.Increment();
                    //this.Counters.HitRatioBaseCounter.Increment();

                    key = ConvertCacheKey(key);

                    CacheItem <TKey, TValue> item = this.DoReadFunc(() => this.InnerDictionary[key]);

                    this.DoWriteAction(() =>
                    {
                        //如果Cache项过期则抛出异常
                        this.CheckDependencyChanged(key, item);

                        //重置cache项的最后访问时间
                        if (item.Dependency != null)
                        {
                            item.Dependency.UtcLastAccessTime = DateTime.UtcNow;
                        }
                    });

                    //this.TotalCounters.HitsCounter.Increment();
                    //this.TotalCounters.HitRatioCounter.Increment();
                    //this.Counters.HitsCounter.Increment();
                    //this.Counters.HitRatioCounter.Increment();

                    return(item.Value);
                }
                catch (System.Exception)
                {
                    //this.TotalCounters.MissesCounter.Increment();
                    //this.Counters.MissesCounter.Increment();
                    throw;
                }
            }
            set
            {
                this.DoWriteAction(() =>
                {
                    key = ConvertCacheKey(key);

                    CacheItem <TKey, TValue> item;

                    if (InnerDictionary.TryGetValue(key, out item) == false)
                    {
                        this.Add(key, value);
                        item = InnerDictionary[key];
                    }
                    else
                    {
                        item.Value = value;
                    }

                    item.UpdateDependencyLastModifyTime();
                });
            }
        }