tangxu
2024-04-30 26d8996e55c33186800031f7c305688035bb3182
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
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
 
namespace IStation.ChEr
{
    internal sealed partial class SharedMemoryCache
    {
        /// <summary>
        /// 移除所有缓存项,在移除之前阻止任何操作
        /// Remove all cache entries, blocking any set operations until complete
        /// </summary>
        public void Wipe()
        {
            if (this._isWiping) { return; }
 
            lock (this._wipeLock)
            {
                if (this._isWiping) { return; }
 
                this._isWiping = true;
 
                SpinWait.SpinUntil(() => !this._isSetting); // wait until all running set operations are complete (new ones are blocked)
 
                var keys = GetKeys();
 
                Parallel.ForEach(keys, x => ((IMemoryCacheDirect)this).Remove(x));
 
                try
                {
                    if (keys.Any())
                    {
                        throw new Exception("Unable to remove keys: " + string.Join(", ", keys));
                    }
                }
                finally
                {
                    this._isWiping = false;
                }
            }
        }
    }
}