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 (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; } } } } }