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
SimpleMapCache.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 using System.Collections.Generic;
20 
21 namespace Lucene.Net.Util.Cache
22 {
23 
24  /// <summary> Simple cache implementation that uses a HashMap to store (key, value) pairs.
25  /// This cache is not synchronized, use <see cref="Cache{TKey, TValue}.SynchronizedCache(Cache{TKey, TValue})" />
26  /// if needed.
27  /// </summary>
28  public class SimpleMapCache<TKey, TValue> : Cache<TKey, TValue>
29  {
30  internal System.Collections.Generic.Dictionary<TKey, TValue> map;
31 
32  public SimpleMapCache()
33  : this(new System.Collections.Generic.Dictionary<TKey, TValue>())
34  {
35  }
36 
37  public SimpleMapCache(System.Collections.Generic.Dictionary<TKey, TValue> map)
38  {
39  this.map = map;
40  }
41 
42  public override TValue Get(System.Object key)
43  {
44  return map[(TKey)key];
45  }
46 
47  public override void Put(TKey key, TValue value_Renamed)
48  {
49  map[key] = value_Renamed;
50  }
51 
52  public override bool ContainsKey(System.Object key)
53  {
54  return map.ContainsKey((TKey)key);
55  }
56 
57  protected override void Dispose(bool disposing)
58  {
59  // do nothing
60  }
61 
62  /// <summary> Returns a Set containing all keys in this cache.</summary>
63  public virtual System.Collections.Generic.HashSet<TKey> KeySet()
64  {
65  return new HashSet<TKey>(map.Keys);
66  }
67 
68  internal override Cache<TKey, TValue> GetSynchronizedCache()
69  {
70  return new SynchronizedSimpleMapCache(this);
71  }
72 
73  // Why does does this use both inheritance and composition?
74  private class SynchronizedSimpleMapCache : SimpleMapCache<TKey, TValue>
75  {
76  private System.Object mutex;
77  private SimpleMapCache<TKey, TValue> cache;
78 
79  private bool isDisposed;
80 
81  internal SynchronizedSimpleMapCache(SimpleMapCache<TKey, TValue> cache)
82  {
83  this.cache = cache;
84  this.mutex = this;
85  }
86 
87  public override void Put(TKey key, TValue value_Renamed)
88  {
89  lock (mutex)
90  {
91  cache.Put(key, value_Renamed);
92  }
93  }
94 
95  public override TValue Get(System.Object key)
96  {
97  lock (mutex)
98  {
99  return cache.Get(key);
100  }
101  }
102 
103  public override bool ContainsKey(System.Object key)
104  {
105  lock (mutex)
106  {
107  return cache.ContainsKey(key);
108  }
109  }
110 
111  protected override void Dispose(bool disposing)
112  {
113  lock (mutex)
114  {
115  if (isDisposed) return;
116 
117  if (disposing)
118  {
119  cache.Dispose(disposing);
120  }
121 
122  isDisposed = true;
123  base.Dispose(disposing);
124  }
125  }
126 
127  public override HashSet<TKey> KeySet()
128  {
129  lock (mutex)
130  {
131  return cache.KeySet();
132  }
133  }
134 
135  internal override Cache<TKey, TValue> GetSynchronizedCache()
136  {
137  return this;
138  }
139  }
140  }
141 }