Repository navigation
Expand file tree
/
Copy pathImageCache.cs
More file actions
223 lines (195 loc) · 6.51 KB
/
Copy pathImageCache.cs
File metadata and controls
223 lines (195 loc) · 6.51 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.Drawing;
namespace LinView
{
/// <summary>
/// LRU 图片缓存:最多保留 N 张已加载的 Bitmap。
///
/// ProtectedKey 用于保护"当前正在显示的图片",它不会被淘汰也不会被 Dispose。
/// 其他被淘汰的 Bitmap 会主动 Dispose,立即释放内存。
/// </summary>
public class ImageCache
{
private int _maxCount;
private string? _protectedKey;
private readonly LinkedList<string> _order = new();
private readonly Dictionary<string, LinkedListNode<string>> _nodes = new();
private readonly Dictionary<string, Bitmap> _cache = new();
private readonly object _lock = new();
public ImageCache(int maxCount)
{
_maxCount = Math.Max(0, maxCount);
}
/// <summary>最大缓存数量,0 表示禁用</summary>
public int MaxCount
{
get => _maxCount;
set
{
lock (_lock)
{
_maxCount = Math.Max(0, value);
TrimToSizeNoLock();
}
}
}
/// <summary>受保护的 key(不会被淘汰或释放)</summary>
public string? ProtectedKey
{
get => _protectedKey;
set
{
lock (_lock)
{
_protectedKey = value;
}
}
}
public bool IsEnabled => _maxCount > 0;
public bool Contains(string key)
{
if (_maxCount <= 0) return false;
lock (_lock)
{
return _cache.ContainsKey(key);
}
}
/// <summary>判断某个 Bitmap 引用是否在缓存里</summary>
public bool ContainsBitmap(Bitmap bmp)
{
if (bmp == null) return false;
lock (_lock)
{
foreach (var kv in _cache)
{
if (ReferenceEquals(kv.Value, bmp))
return true;
}
return false;
}
}
public Bitmap? Get(string key)
{
if (_maxCount <= 0) return null;
lock (_lock)
{
if (!_cache.TryGetValue(key, out var bmp)) return null;
if (_nodes.TryGetValue(key, out var node))
{
_order.Remove(node);
_order.AddFirst(node);
}
return bmp;
}
}
public void Add(string key, Bitmap bmp)
{
if (bmp == null) return;
lock (_lock)
{
if (_maxCount <= 0) return;
if (_cache.TryGetValue(key, out var old))
{
if (!ReferenceEquals(old, bmp))
{
_cache[key] = bmp;
// 释放旧位图,但两种情况下不释放:
// 1. 该 key 是 ProtectedKey(正在显示,不能提前释放)
// 2. 旧位图被 cache 中其他 key 共享
bool canDispose =
!string.Equals(key, _protectedKey, StringComparison.Ordinal)
&& !IsBitmapSharedLocked(old, key);
if (canDispose)
old.Dispose();
}
if (_nodes.TryGetValue(key, out var n))
{
_order.Remove(n);
_order.AddFirst(n);
}
return;
}
_cache[key] = bmp;
var node = _order.AddFirst(key);
_nodes[key] = node;
TrimToSizeNoLock();
}
}
public void Remove(string key)
{
lock (_lock)
{
if (_nodes.TryGetValue(key, out var node))
{
_order.Remove(node);
_nodes.Remove(key);
}
if (_cache.TryGetValue(key, out var bmp))
{
_cache.Remove(key);
if (!string.Equals(key, _protectedKey, StringComparison.Ordinal)
&& !IsBitmapSharedLocked(bmp, key))
{
bmp.Dispose();
}
}
}
}
public void Clear()
{
lock (_lock)
{
foreach (var kv in _cache)
{
if (string.Equals(kv.Key, _protectedKey, StringComparison.Ordinal))
continue;
if (IsBitmapSharedLocked(kv.Value, kv.Key))
continue;
kv.Value.Dispose();
}
_cache.Clear();
_order.Clear();
_nodes.Clear();
}
}
/// <summary>
/// 判断某个 Bitmap 是否还被 cache 中除 excludeKey 以外的其他 key 引用。
/// 必须在 _lock 内调用。
/// </summary>
private bool IsBitmapSharedLocked(Bitmap bmp, string excludeKey)
{
foreach (var kv in _cache)
{
if (string.Equals(kv.Key, excludeKey, StringComparison.Ordinal))
continue;
if (ReferenceEquals(kv.Value, bmp))
return true;
}
return false;
}
private void TrimToSizeNoLock()
{
// 从链表尾部(最旧)开始淘汰,跳过 ProtectedKey
var node = _order.Last;
while (node != null && _cache.Count > _maxCount)
{
var key = node.Value;
var prev = node.Previous;
if (!string.Equals(key, _protectedKey, StringComparison.Ordinal))
{
_order.Remove(node);
_nodes.Remove(key);
if (_cache.TryGetValue(key, out var bmp))
{
_cache.Remove(key);
// 只有没被其他 key 共享时才真正释放
if (!IsBitmapSharedLocked(bmp, key))
bmp.Dispose();
}
}
node = prev;
}
}
}
}