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