1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Imports
|
28
|
|
using System;
|
29
|
|
using System.Collections;
|
30
|
|
using System.Collections.Specialized;
|
31
|
|
|
32
|
|
using IBatisNet.DataMapper.Configuration.Cache;
|
33
|
|
#endregion
|
34
|
|
|
35
|
|
namespace IBatisNet.DataMapper.Configuration.Cache.Memory
|
36
|
|
{
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
public class MemoryCacheControler : ICacheController
|
41
|
|
{
|
42
|
|
#region Fields
|
43
|
|
private MemoryCacheLevel _cacheLevel = MemoryCacheLevel.Weak;
|
44
|
|
private Hashtable _cache = null;
|
45
|
|
#endregion
|
46
|
|
|
47
|
|
#region Constructor (s) / Destructor
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
170
|
public MemoryCacheControler()
|
52
|
|
{
|
53
|
170
|
_cache = new Hashtable();
|
54
|
|
}
|
55
|
|
#endregion
|
56
|
|
|
57
|
|
#region ICacheController Members
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
public object this[object key]
|
65
|
|
{
|
66
|
13
|
get
|
67
|
|
{
|
68
|
13
|
lock (this)
|
69
|
|
{
|
70
|
13
|
object value = null;
|
71
|
13
|
object reference = _cache[key];
|
72
|
13
|
if (reference != null)
|
73
|
|
{
|
74
|
5
|
if (reference is StrongReference)
|
75
|
|
{
|
76
|
0
|
value = ((StrongReference) reference).Target;
|
77
|
|
}
|
78
|
5
|
else if (reference is WeakReference)
|
79
|
|
{
|
80
|
5
|
value = ((WeakReference) reference).Target;
|
81
|
|
}
|
82
|
|
}
|
83
|
13
|
return value;
|
84
|
|
}
|
85
|
|
}
|
86
|
8
|
set
|
87
|
|
{
|
88
|
8
|
lock (this)
|
89
|
|
{
|
90
|
8
|
object reference = null;
|
91
|
8
|
if (_cacheLevel.Equals(MemoryCacheLevel.Weak))
|
92
|
|
{
|
93
|
8
|
reference = new WeakReference(value);
|
94
|
|
}
|
95
|
0
|
else if (_cacheLevel.Equals(MemoryCacheLevel.Strong))
|
96
|
|
{
|
97
|
|
reference = new StrongReference(value);
|
98
|
|
}
|
99
|
8
|
_cache[key] = reference;
|
100
|
|
}
|
101
|
|
}
|
102
|
|
}
|
103
|
|
|
104
|
|
|
105
|
|
|
106
|
|
|
107
|
|
|
108
|
6
|
public void Flush()
|
109
|
|
{
|
110
|
6
|
lock(this)
|
111
|
|
{
|
112
|
6
|
_cache.Clear();
|
113
|
|
}
|
114
|
|
}
|
115
|
|
|
116
|
|
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
170
|
public void Configure(HybridDictionary properties)
|
121
|
|
{
|
122
|
170
|
string referenceType = (string)properties["Type"];;
|
123
|
170
|
if (referenceType != null)
|
124
|
|
{
|
125
|
170
|
_cacheLevel = MemoryCacheLevel.GetByRefenceType(referenceType.ToUpper());
|
126
|
|
}
|
127
|
|
}
|
128
|
|
|
129
|
|
#endregion
|
130
|
|
|
131
|
|
|
132
|
|
|
133
|
|
|
134
|
|
private class StrongReference
|
135
|
|
{
|
136
|
|
private object _target = null;
|
137
|
|
|
138
|
0
|
public StrongReference(object obj)
|
139
|
|
{
|
140
|
|
_target = obj;
|
141
|
|
}
|
142
|
|
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
0
|
public object Target
|
147
|
|
{
|
148
|
|
get
|
149
|
|
{
|
150
|
|
return _target ;
|
151
|
|
}
|
152
|
|
}
|
153
|
|
}
|
154
|
|
}
|
155
|
|
}
|
156
|
|
|