using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace IStation { internal sealed partial class SharedMemoryCache { /// /// 移除所有缓存项,在移除之前阻止任何操作 /// Remove all cache entries, blocking any set operations until complete /// 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; } } } } }