12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- // JsonKit v0.5 - A simple but flexible Json library in a single .cs file.
- //
- // Copyright (C) 2014 Topten Software (contact@toptensoftware.com) All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this product
- // except in compliance with the License. You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software distributed under the
- // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- // either express or implied. See the License for the specific language governing permissions
- // and limitations under the License.
- // Define JsonKit_NO_DYNAMIC to disable Expando support
- // Define JsonKit_NO_EMIT to disable Reflection.Emit
- // Define JsonKit_NO_DATACONTRACT to disable support for [DataContract]/[DataMember]
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace Topten.JsonKit
- {
- public class ThreadSafeCache<TKey, TValue>
- {
- public ThreadSafeCache()
- {
- }
- public TValue Get(TKey key, Func<TValue> createIt)
- {
- // Check if already exists
- _lock.EnterReadLock();
- try
- {
- TValue val;
- if (_map.TryGetValue(key, out val))
- return val;
- }
- finally
- {
- _lock.ExitReadLock();
- }
- // Nope, take lock and try again
- _lock.EnterWriteLock();
- try
- {
- // Check again before creating it
- TValue val;
- if (!_map.TryGetValue(key, out val))
- {
- // Store the new one
- val = createIt();
- _map[key] = val;
- }
- return val;
- }
- finally
- {
- _lock.ExitWriteLock();
- }
- }
- public bool TryGetValue(TKey key, out TValue val)
- {
- _lock.EnterReadLock();
- try
- {
- return _map.TryGetValue(key, out val);
- }
- finally
- {
- _lock.ExitReadLock();
- }
- }
- public void Set(TKey key, TValue value)
- {
- _lock.EnterWriteLock();
- try
- {
- _map[key] = value;
- }
- finally
- {
- _lock.ExitWriteLock();
- }
- }
- Dictionary<TKey, TValue> _map = new Dictionary<TKey,TValue>();
- ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
- }
- }
|