Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Cache.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Util.Cache
21 {
22 
23 
24  /// <summary> Base class for cache implementations.</summary>
25  public abstract class Cache<TKey, TValue> : IDisposable
26  {
27 
28  /// <summary> Simple Cache wrapper that synchronizes all
29  /// calls that access the cache.
30  /// </summary>
31  internal class SynchronizedCache_Renamed_Class : Cache<TKey, TValue>
32  {
33  internal System.Object mutex;
34  internal Cache<TKey,TValue> cache;
35 
36  internal SynchronizedCache_Renamed_Class(Cache<TKey, TValue> cache)
37  {
38  this.cache = cache;
39  this.mutex = this;
40  }
41 
42  internal SynchronizedCache_Renamed_Class(Cache<TKey, TValue> cache, System.Object mutex)
43  {
44  this.cache = cache;
45  this.mutex = mutex;
46  }
47 
48  public override void Put(TKey key, TValue value_Renamed)
49  {
50  lock (mutex)
51  {
52  cache.Put(key, value_Renamed);
53  }
54  }
55 
56  public override TValue Get(System.Object key)
57  {
58  lock (mutex)
59  {
60  return cache.Get(key);
61  }
62  }
63 
64  public override bool ContainsKey(System.Object key)
65  {
66  lock (mutex)
67  {
68  return cache.ContainsKey(key);
69  }
70  }
71 
72  protected override void Dispose(bool disposing)
73  {
74  lock (mutex)
75  {
76  cache.Dispose();
77  }
78  }
79 
80  internal override Cache<TKey,TValue> GetSynchronizedCache()
81  {
82  return this;
83  }
84  }
85 
86  /// <summary> Returns a thread-safe cache backed by the specified cache.
87  /// In order to guarantee thread-safety, all access to the backed cache must
88  /// be accomplished through the returned cache.
89  /// </summary>
90  public static Cache<TKey, TValue> SynchronizedCache(Cache<TKey, TValue> cache)
91  {
92  return cache.GetSynchronizedCache();
93  }
94 
95  /// <summary> Called by <see cref="SynchronizedCache(Cache{TKey,TValue})" />. This method
96  /// returns a <see cref="SynchronizedCache" /> instance that wraps
97  /// this instance by default and can be overridden to return
98  /// e. g. subclasses of <see cref="SynchronizedCache" /> or this
99  /// in case this cache is already synchronized.
100  /// </summary>
101  internal virtual Cache<TKey, TValue> GetSynchronizedCache()
102  {
103  return new SynchronizedCache_Renamed_Class(this);
104  }
105 
106  /// <summary> Puts a (key, value)-pair into the cache. </summary>
107  public abstract void Put(TKey key, TValue value_Renamed);
108 
109  /// <summary> Returns the value for the given key. </summary>
110  public abstract TValue Get(System.Object key);
111 
112  /// <summary> Returns whether the given key is in this cache. </summary>
113  public abstract bool ContainsKey(System.Object key);
114 
115  /// <summary> Closes the cache.</summary>
116  [Obsolete("Use Dispose() instead")]
117  public void Close()
118  {
119  Dispose();
120  }
121 
122  public void Dispose()
123  {
124  Dispose(true);
125  }
126 
127  protected abstract void Dispose(bool disposing);
128  }
129 }