OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
CnmSynchronizedCache.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Threading;
32 
33 namespace OpenSim.Framework
34 {
50  public class CnmSynchronizedCache<TKey, TValue> : ICnmCache<TKey, TValue>
51  {
55  private readonly ICnmCache<TKey, TValue> m_cache;
56 
60  private readonly object m_syncRoot;
61 
69  private CnmSynchronizedCache(ICnmCache<TKey, TValue> cache)
70  {
71  m_cache = cache;
72  m_syncRoot = m_cache.SyncRoot;
73  }
74 
88  {
89  if (cache == null)
90  throw new ArgumentNullException("cache");
91  return cache.IsSynchronized ? cache : new CnmSynchronizedCache<TKey, TValue>(cache);
92  }
93 
94  #region Nested type: SynchronizedEnumerator
95 
99  private class SynchronizedEnumerator : IEnumerator<KeyValuePair<TKey, TValue>>
100  {
104  private readonly IEnumerator<KeyValuePair<TKey, TValue>> m_enumerator;
105 
109  private object m_syncRoot;
110 
120  public SynchronizedEnumerator(IEnumerator<KeyValuePair<TKey, TValue>> enumerator, object syncRoot)
121  {
122  m_syncRoot = syncRoot;
123  m_enumerator = enumerator;
124  Monitor.Enter(m_syncRoot);
125  }
126 
130  ~SynchronizedEnumerator()
131  {
132  Dispose();
133  }
134 
135  #region IEnumerator<KeyValuePair<TKey,TValue>> Members
136 
146  public KeyValuePair<TKey, TValue> Current
147  {
148  get { return m_enumerator.Current; }
149  }
150 
160  object IEnumerator.Current
161  {
162  get { return Current; }
163  }
164 
168  public void Dispose()
169  {
170  if (m_syncRoot != null)
171  {
172  Monitor.Exit(m_syncRoot);
173  m_syncRoot = null;
174  }
175 
176  m_enumerator.Dispose();
177  GC.SuppressFinalize(this);
178  }
179 
189  public bool MoveNext()
190  {
191  return m_enumerator.MoveNext();
192  }
193 
200  public void Reset()
201  {
202  m_enumerator.Reset();
203  }
204 
205  #endregion
206  }
207 
208  #endregion
209 
210  #region ICnmCache<TKey,TValue> Members
211 
225  public int Count
226  {
227  get
228  {
229  lock (m_syncRoot)
230  {
231  return m_cache.Count;
232  }
233  }
234  }
235 
271  public TimeSpan ExpirationTime
272  {
273  get
274  {
275  lock (m_syncRoot)
276  {
277  return m_cache.ExpirationTime;
278  }
279  }
280 
281  set
282  {
283  lock (m_syncRoot)
284  {
285  m_cache.ExpirationTime = value;
286  }
287  }
288  }
289 
307  public bool IsCountLimited
308  {
309  get
310  {
311  lock (m_syncRoot)
312  {
313  return m_cache.IsCountLimited;
314  }
315  }
316  }
317 
336  public bool IsSizeLimited
337  {
338  get
339  {
340  lock (m_syncRoot)
341  {
342  return m_cache.IsSizeLimited;
343  }
344  }
345  }
346 
363  public bool IsSynchronized
364  {
365  get { return true; }
366  }
367 
385  public bool IsTimeLimited
386  {
387  get
388  {
389  lock (m_syncRoot)
390  {
391  return m_cache.IsTimeLimited;
392  }
393  }
394  }
395 
409  public int MaxCount
410  {
411  get
412  {
413  lock (m_syncRoot)
414  {
415  return m_cache.MaxCount;
416  }
417  }
418 
419  set
420  {
421  lock (m_syncRoot)
422  {
423  m_cache.MaxCount = value;
424  }
425  }
426  }
427 
444  public long MaxElementSize
445  {
446  get
447  {
448  lock (m_syncRoot)
449  {
450  return m_cache.MaxElementSize;
451  }
452  }
453  }
454 
474  public long MaxSize
475  {
476  get
477  {
478  lock (m_syncRoot)
479  {
480  return m_cache.MaxSize;
481  }
482  }
483 
484  set
485  {
486  lock (m_syncRoot)
487  {
488  m_cache.MaxSize = value;
489  }
490  }
491  }
492 
516  public long Size
517  {
518  get
519  {
520  lock (m_syncRoot)
521  {
522  return m_cache.Size;
523  }
524  }
525  }
526 
542  public object SyncRoot
543  {
544  get { return m_syncRoot; }
545  }
546 
555  public void Clear()
556  {
557  lock (m_syncRoot)
558  {
559  m_cache.Clear();
560  }
561  }
562 
570  public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
571  {
572  lock (m_syncRoot)
573  {
574  return new SynchronizedEnumerator(m_cache.GetEnumerator(), m_syncRoot);
575  }
576  }
577 
597  public void PurgeExpired()
598  {
599  lock (m_syncRoot)
600  {
601  m_cache.PurgeExpired();
602  }
603  }
604 
619  public void Remove(TKey key)
620  {
621  lock (m_syncRoot)
622  {
623  m_cache.Remove(key);
624  }
625  }
626 
641  public void RemoveRange(IEnumerable<TKey> keys)
642  {
643  lock (m_syncRoot)
644  {
645  m_cache.RemoveRange(keys);
646  }
647  }
648 
694  public bool Set(TKey key, TValue value, long size)
695  {
696  lock (m_syncRoot)
697  {
698  return m_cache.Set(key, value, size);
699  }
700  }
701 
725  public bool TryGetValue(TKey key, out TValue value)
726  {
727  lock (m_syncRoot)
728  {
729  return m_cache.TryGetValue(key, out value);
730  }
731  }
732 
740  IEnumerator IEnumerable.GetEnumerator()
741  {
742  return GetEnumerator();
743  }
744 
745  #endregion
746  }
747 }
IEnumerator< KeyValuePair< TKey, TValue > > GetEnumerator()
Returns an enumerator that iterates through the elements stored to ICnmCache{TKey,TValue}.
static ICnmCache< TKey, TValue > Synchronized(ICnmCache< TKey, TValue > cache)
Returns a ICnmCache{TKey,TValue} wrapper that is synchronized (thread safe).
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key .
void Remove(TKey key)
Removes element associated with key from the ICnmCache{TKey,TValue}.
void RemoveRange(IEnumerable< TKey > keys)
Removes elements that are associated with one of keys from the ICnmCache{TKey,TValue}.
OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString key
Definition: ICM_Api.cs:31
void Clear()
Removes all elements from the ICnmCache{TKey,TValue}.
void PurgeExpired()
Purge expired elements from the ICnmCache{TKey,TValue}.
System.Collections.IEnumerable IEnumerable
bool Set(TKey key, TValue value, long size)
Add or replace an element with the provided key , value and size to ICnmCache{TKey,TValue}.
Represent generic cache to store key/value pairs (elements) limited by time, size and count of elemen...
Definition: ICnmCache.cs:80