F/Orm/LINQS/ThreadSafeCache.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

#region Copyright (c) 2008 by Jahmani Muigai Mwaura and Community
/*--------------------------------------------------------------------------------------------------
 *  LinqToSql, a Linq to Sql parser for the .NET Platform
 *  by Jahmani Mwaura and community
 *  ------------------------------------------------------------------------------------------------
 *  Version: LGPL 2.1
 *  
 *  Software distributed under the License is distributed on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *  for the specific language governing rights and limitations under the
 *  License.
 *  
 *  The Original Code is any part of this file that is not marked as a contribution.
 *  
 *  The Initial Developer of the Original Code is Jahmani Muigai Mwaura.
 *  Portions created by the Initial Developer are Copyright (C) 2008
 *  the Initial Developer. All Rights Reserved.
 *  
 *  Contributor(s): None.
 *--------------------------------------------------------------------------------------------------
 */
#endregion

using System;
using System.Collections.Generic;
using System.Threading;

namespace F.Orm.LINQS
{
    
    public class ThreadSafeCache<TKey, TValue> {

        private readonly Dictionary<TKey, TValue> theDictionary = new Dictionary<TKey, TValue>();
        private readonly ReaderWriterLock rwl = new ReaderWriterLock();
        private readonly TimeSpan readerLockTimeout = TimeSpan.FromSeconds(1);
        private readonly TimeSpan writerLockTimeout = TimeSpan.FromSeconds(2);

        public ThreadSafeCache() {
        }

        public TValue this[TKey key] {
            get {
                try {
                    rwl.AcquireReaderLock(readerLockTimeout);
                    return theDictionary[key];
                }
                finally {
                    rwl.ReleaseReaderLock();
                }
            }
        }

        public ICollection<TKey> Keys {
            get {
                try {
                    rwl.AcquireReaderLock(readerLockTimeout);
                    return theDictionary.Keys;
                }
                finally {
                    rwl.ReleaseReaderLock();
后面还有 81 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。