Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Cache.cs « Cache « Util « core « src - github.com/mono/Lucene.Net.Light.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 34ded72b233c6b60cdf1c40f85f5f2bdcee56c57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file 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.
 */

using System;

namespace Lucene.Net.Util.Cache
{
	
	
	/// <summary> Base class for cache implementations.</summary>
	public abstract class Cache<TKey, TValue> : IDisposable
	{
		
		/// <summary> Simple Cache wrapper that synchronizes all
		/// calls that access the cache. 
		/// </summary>
		internal class SynchronizedCache_Renamed_Class : Cache<TKey, TValue>
		{
			internal System.Object mutex;
			internal Cache<TKey,TValue> cache;

            internal SynchronizedCache_Renamed_Class(Cache<TKey, TValue> cache)
			{
				this.cache = cache;
				this.mutex = this;
			}

            internal SynchronizedCache_Renamed_Class(Cache<TKey, TValue> cache, System.Object mutex)
			{
				this.cache = cache;
				this.mutex = mutex;
			}
			
			public override void Put(TKey key, TValue value_Renamed)
			{
				lock (mutex)
				{
					cache.Put(key, value_Renamed);
				}
			}
			
			public override TValue Get(System.Object key)
			{
				lock (mutex)
				{
					return cache.Get(key);
				}
			}
			
			public override bool ContainsKey(System.Object key)
			{
				lock (mutex)
				{
					return cache.ContainsKey(key);
				}
			}
			
            protected override void Dispose(bool disposing)
            {
                lock (mutex)
                {
                    cache.Dispose();
                }
            }
			
			internal override Cache<TKey,TValue> GetSynchronizedCache()
			{
				return this;
			}
		}
		
		/// <summary> Returns a thread-safe cache backed by the specified cache. 
		/// In order to guarantee thread-safety, all access to the backed cache must
		/// be accomplished through the returned cache.
		/// </summary>
        public static Cache<TKey, TValue> SynchronizedCache(Cache<TKey, TValue> cache)
		{
			return cache.GetSynchronizedCache();
		}
		
		/// <summary> Called by <see cref="SynchronizedCache(Cache{TKey,TValue})" />. This method
		/// returns a <see cref="SynchronizedCache" /> instance that wraps
		/// this instance by default and can be overridden to return
		/// e. g. subclasses of <see cref="SynchronizedCache" /> or this
		/// in case this cache is already synchronized.
		/// </summary>
        internal virtual Cache<TKey, TValue> GetSynchronizedCache()
		{
            return new SynchronizedCache_Renamed_Class(this);
		}
		
		/// <summary> Puts a (key, value)-pair into the cache. </summary>
		public abstract void  Put(TKey key, TValue value_Renamed);
		
		/// <summary> Returns the value for the given key. </summary>
		public abstract TValue Get(System.Object key);
		
		/// <summary> Returns whether the given key is in this cache. </summary>
		public abstract bool ContainsKey(System.Object key);

	    /// <summary> Closes the cache.</summary>
	    [Obsolete("Use Dispose() instead")]
	    public void Close()
	    {
	        Dispose();
	    }

	    public void Dispose()
	    {
	        Dispose(true);
	    }

	    protected abstract void Dispose(bool disposing);
	}
}