예제 #1
0
 bool IReadOnlyDictionary <TDestinationKey, TDestination> .ContainsKey(TDestinationKey key) => InnerDictionary.ContainsKey((TSourceKey)key);
예제 #2
0
 /// <summary>
 /// Checks if the collection contains a value
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public bool Contains(TValue value)
 {
     return(InnerDictionary.ContainsKey(KeySelector.Invoke(value)));
 }
예제 #3
0
 /// <summary>
 /// Tries to add a value to the collection
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public bool TryAdd(TValue value)
 {
     return(InnerDictionary.TryAdd(KeySelector.Invoke(value), value));
 }
예제 #4
0
 /// <inheritdoc />
 public override bool Equals(object obj)
 {
     using (writeLock.LockRead())
         return(InnerDictionary.Equals(obj));
 }
 public AvatarInventoryItemEquippedModifier(Guid itemId, bool equipped)
 {
     dictionary = new InnerDictionary();
     dictionary.Value.Add(new JsonConvertibleGuid(itemId), equipped);
 }
예제 #6
0
 /// <inheritdoc />
 public bool Remove(KeyValuePair <TKey, TValue> item)
 {
     using (writeLock.LockWrite())
         return(InnerDictionary.Remove(item));
 }
예제 #7
0
 /// <inheritdoc />
 public bool TryGetValue(TKey key, out TValue value)
 {
     using (writeLock.LockRead())
         return(InnerDictionary.TryGetValue(key, out value));
 }
 public bool ContainsKey(string key)
 {
     return(InnerDictionary.ContainsKey(key));
 }
 public bool TryGetValue(string key, out MethodParameterValue value)
 {
     return(InnerDictionary.TryGetValue(key, out value));
 }
예제 #10
0
 /// <summary>
 /// 删除Cache项
 /// </summary>
 /// <param name="key">Cache项键值</param>
 /// <param name="item">Cache项</param>
 private void InnerRemove(TKey key, CacheItem <TKey, TValue> item)
 {
     InnerDictionary.Remove(key);
     item.Dispose();
 }
 protected override IEnumerator <KeyValuePair <keyˈ, valueˈ> > GetEnumerator() =>
 InnerDictionary.Where(kvp => kvp.Case(Filter)).GetEnumerator();
예제 #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();
                }
            }
        }
예제 #13
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
            {
                //读操作时,也会修改字典(调整次序),因此需要锁住字典,进行并发控制
                lock (InnerDictionary.SyncRoot)
                {
                    result = InnerDictionary.TryGetValue(key, out item);
                }
            }
            finally
            {
                this.rWLock.ReleaseReaderLock();
            }

            if (result)
            {
                this.rWLock.AcquireWriterLock(this.lockTimeout);
                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.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);
        }
예제 #14
0
 IEnumerator <KeyValuePair <TDestinationKey, TDestination> > IEnumerable <KeyValuePair <TDestinationKey, TDestination> > .GetEnumerator() => InnerDictionary.Select(item => new KeyValuePair <TDestinationKey, TDestination>(item.Key, item.Value)).GetEnumerator();
예제 #15
0
 /// <inheritdoc />
 public void CopyTo(KeyValuePair <TKey, TValue>[] array, int arrayIndex)
 {
     using (writeLock.LockRead())
         InnerDictionary.CopyTo(array, arrayIndex);
 }
 void ICollection <KeyValuePair <string, MethodParameterValue> > .Clear()
 {
     InnerDictionary.Clear();
 }
예제 #17
0
 /// <inheritdoc />
 public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator()
 {
     using (writeLock.LockRead())
         return(InnerDictionary.GetEnumerator());
 }
 public IEnumerator <KeyValuePair <string, MethodParameterValue> > GetEnumerator()
 {
     return(InnerDictionary.GetEnumerator());
 }
예제 #19
0
 /// <inheritdoc />
 public bool Remove(TKey key)
 {
     using (writeLock.LockWrite())
         return(InnerDictionary.Remove(key));
 }
예제 #20
0
 /// <inheritdoc />
 public void Add(KeyValuePair <TKey, TValue> item)
 {
     using (writeLock.LockWrite())
         InnerDictionary.Add(item);
 }
예제 #21
0
 /// <inheritdoc />
 IEnumerator IEnumerable.GetEnumerator()
 {
     using (writeLock.LockRead())
         return(InnerDictionary.GetEnumerator());
 }
예제 #22
0
 /// <inheritdoc />
 public void Add(TKey key, TValue value)
 {
     using (writeLock.LockWrite())
         InnerDictionary.Add(key, value);
 }
예제 #23
0
 /// <inheritdoc />
 public override string ToString()
 {
     using (writeLock.LockRead())
         return(InnerDictionary.ToString());
 }
예제 #24
0
 /// <inheritdoc />
 public void Clear()
 {
     using (writeLock.LockWrite())
         InnerDictionary.Clear();
 }
예제 #25
0
 /// <summary>
 /// Tries to remove a value from the collection
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public bool Remove(TValue value)
 {
     return(InnerDictionary.TryRemove(KeySelector.Invoke(value), out var _));
 }
예제 #26
0
 /// <inheritdoc />
 public bool Contains(KeyValuePair <TKey, TValue> item)
 {
     using (writeLock.LockRead())
         return(InnerDictionary.Contains(item));
 }
예제 #27
0
 /// <summary>
 /// Removes all values from the collection
 /// </summary>
 public void Clear()
 {
     InnerDictionary.Clear();
 }
예제 #28
0
 /// <inheritdoc />
 public bool ContainsKey(TKey key)
 {
     using (writeLock.LockRead())
         return(InnerDictionary.ContainsKey(key));
 }
예제 #29
0
 /// <summary>
 /// Get the existing item or adds a new one matched by their keys
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public TValue GetOrAdd(TValue value)
 {
     return(InnerDictionary.GetOrAdd(KeySelector.Invoke(value), value));
 }
예제 #30
0
 /// <inheritdoc />
 public void Clear()
 {
     EnsureWritable();
     InnerDictionary.Clear();
 }