/// <summary> /// Will find the eldest engine and force-purge it. /// </summary> private static void ForceExpireOneEngine(DataEngineCache cache) { var engines = cache.m_engines; if (engines == null) { return; } IDataEngine oldest = null; ConcurrentDictionary <string, IDataEngine> container = null; string scope = null, tenant = null; foreach (var tenantEnginesRec in engines) { foreach (var engineRec in tenantEnginesRec.Value) { if (oldest == null || oldest.UtcLastUsedAt > engineRec.Value.UtcLastUsedAt) { oldest = engineRec.Value; container = tenantEnginesRec.Value; tenant = tenantEnginesRec.Key; scope = engineRec.Key; } } } if (oldest != null) { cache.m_tracer.InfoFormat("Forced expiration of engine for tenant {0}, scope {1}", tenant, scope); IDataEngine engine; if (container.TryRemove(scope, out engine)) { // only invoke Dispose here if the engine is not active right now // otherwise, let it be garbage-collected var ageseconds = (DateTime.UtcNow - engine.UtcLastUsedAt).TotalSeconds; if (ageseconds < 0) { ageseconds = -ageseconds; } if (ageseconds > 1.0) { engine.Dispose(); } } } }
/// <summary> /// Will find the eldest engine and force-purge it. /// </summary> private static void ForceExpireOneEngine(DataEngineCache cache) { var engines = cache.m_engines; if (engines == null) { return; } IDataEngine oldest = null; ConcurrentDictionary<string, IDataEngine> container = null; string scope = null, tenant = null; foreach (var tenantEnginesRec in engines) { foreach (var engineRec in tenantEnginesRec.Value) { if (oldest == null || oldest.UtcLastUsedAt > engineRec.Value.UtcLastUsedAt) { oldest = engineRec.Value; container = tenantEnginesRec.Value; tenant = tenantEnginesRec.Key; scope = engineRec.Key; } } } if (oldest != null) { cache.m_tracer.InfoFormat("Forced expiration of engine for tenant {0}, scope {1}", tenant, scope); IDataEngine engine; if (container.TryRemove(scope, out engine)) { // only invoke Dispose here if the engine is not active right now // otherwise, let it be garbage-collected var ageseconds = (DateTime.UtcNow - engine.UtcLastUsedAt).TotalSeconds; if (ageseconds < 0) { ageseconds = -ageseconds; } if (ageseconds > 1.0) { engine.Dispose(); } } } }