private static bool MaximumTimeToLiveExpired(AdObjectCacheEntry <TADWrapperObject> entry) { return(DateTime.UtcNow.CompareTo(entry.MaximumTimeToExpire) > 0); }
private TADWrapperObject LookupOrFindAdObject(string identifyingName, AdObjectLookupCache <TADWrapperObject> .FindAdObjectCacheFailure objectLookupFunction, AdObjectLookupFlags flags) { ExTraceGlobals.ActiveManagerClientTracer.TraceFunction <string>(0L, "LookupOrFindAdObject({0})", identifyingName); AdObjectCacheEntry <TADWrapperObject> adObjectCacheEntry = null; bool flag = false; bool flag2 = (flags & AdObjectLookupFlags.ReadThrough) != AdObjectLookupFlags.None; bool flag3 = false; try { int num = 0; while (num < 2 && !flag) { flag = this.m_rwLock.TryEnterReadLock(AdObjectLookupCache <TADWrapperObject> .CacheLockTimeout); num++; } if (flag) { flag3 = this.m_cache.TryGetValue(identifyingName, out adObjectCacheEntry); bool flag4 = false; if (flag3) { flag4 = AdObjectLookupCache <TADWrapperObject> .ShouldExpireCacheEntry(adObjectCacheEntry); ExTraceGlobals.ActiveManagerClientTracer.TraceDebug((long)this.GetHashCode(), "LookupOrFindAdObject( {0} ) was found in the cache: {1}, and shouldExpireEntry={2}, flags={3}", new object[] { identifyingName, adObjectCacheEntry, flag4, flags }); } if (!flag3 || flag4) { flag2 = true; } } else { AdObjectLookupCache <TADWrapperObject> .Tracer.TraceError((long)this.GetHashCode(), "Timeout waiting for the read lock in AdObjectLookupCache.LookupOrFindAdObject()"); flag2 = true; } } finally { if (flag) { this.m_rwLock.ExitReadLock(); } } if (flag2) { bool flag5 = false; bool flag6 = false; TADWrapperObject updatedObject = default(TADWrapperObject); try { flag5 = this.CheckAndSetADLock(identifyingName); if (flag5) { InvokeWithTimeout.Invoke(delegate() { updatedObject = objectLookupFunction(); }, this.m_adOperationTimeout); } else if (!flag3 || AdObjectLookupCache <TADWrapperObject> .MaximumTimeToLiveExpired(adObjectCacheEntry)) { int num2 = 10; int num3 = 10; for (int i = 0; i < num3; i++) { Thread.Sleep(num2); bool flag7 = false; try { int num4 = 0; while (num4 < 2 && !flag7) { flag7 = this.m_rwLock.TryEnterReadLock(AdObjectLookupCache <TADWrapperObject> .CacheLockTimeout); num4++; } if (flag7) { flag3 = this.m_cache.TryGetValue(identifyingName, out adObjectCacheEntry); } } finally { if (flag7) { this.m_rwLock.ExitReadLock(); } } if (flag3 && !AdObjectLookupCache <TADWrapperObject> .MaximumTimeToLiveExpired(adObjectCacheEntry)) { if (AdObjectLookupCache <TADWrapperObject> .LogAdLatency) { StorageGlobals.EventLogger.LogEvent(StorageEventLogConstants.Tuple_ActiveManagerClientAnotherThreadCompleted, identifyingName, new object[] { identifyingName, num2 * i }); } AdObjectLookupCache <TADWrapperObject> .Tracer.TraceDebug <string>((long)this.GetHashCode(), "Another thread finished doing query for object {0}.", identifyingName); flag6 = true; break; } } if (!flag3 || AdObjectLookupCache <TADWrapperObject> .MaximumTimeToLiveExpired(adObjectCacheEntry)) { StorageGlobals.EventLogger.LogEvent(StorageEventLogConstants.Tuple_ActiveManagerClientAnotherThreadInADCallTimeout, identifyingName, new object[] { identifyingName, num2 * num3 }); AdObjectLookupCache <TADWrapperObject> .Tracer.TraceDebug <string, int>((long)this.GetHashCode(), "Another thread doing query for object {0}, however this thread didn't complete in {1} msec.", identifyingName, num2 *num3); updatedObject = objectLookupFunction(); } } else { if (AdObjectLookupCache <TADWrapperObject> .LogAdLatency) { StorageGlobals.EventLogger.LogEvent(StorageEventLogConstants.Tuple_ActiveManagerClientAnotherThreadInADCall, identifyingName, new object[] { identifyingName }); } AdObjectLookupCache <TADWrapperObject> .Tracer.TraceDebug <string>((long)this.GetHashCode(), "Another thread doing query for object {0}.", identifyingName); flag6 = true; } } catch (TimeoutException ex) { StorageGlobals.EventLogger.LogEvent(StorageEventLogConstants.Tuple_ErrorActiveManagerClientADTimeout, identifyingName, new object[] { identifyingName, this.m_adOperationTimeout }); AdObjectLookupCache <TADWrapperObject> .Tracer.TraceError <string>((long)this.GetHashCode(), "Timeout on ad query: {0}", ex.Message); flag6 = true; } finally { if (flag5) { this.ReleaseADLock(identifyingName); } } if (updatedObject != null && this.MinimizeObjects) { updatedObject.Minimize(); } adObjectCacheEntry = new AdObjectCacheEntry <TADWrapperObject>(updatedObject, this.m_timeToLive, this.m_timeToNegativeLive, this.m_maximumTimeToLive); bool flag8 = false; try { int num5 = 0; while (num5 < 2 && !flag8) { flag8 = this.m_rwLock.TryEnterWriteLock(AdObjectLookupCache <TADWrapperObject> .CacheLockTimeout); num5++; } if (flag8) { if (updatedObject == null && flag6) { AdObjectCacheEntry <TADWrapperObject> adObjectCacheEntry2 = null; flag3 = this.m_cache.TryGetValue(identifyingName, out adObjectCacheEntry2); if (flag3 && !AdObjectLookupCache <TADWrapperObject> .MaximumTimeToLiveExpired(adObjectCacheEntry2)) { adObjectCacheEntry = adObjectCacheEntry2; ExTraceGlobals.ActiveManagerClientTracer.TraceDebug <TADWrapperObject, AdObjectCacheEntry <TADWrapperObject> >((long)this.GetHashCode(), "New ad object was not found, but found possibly stale result '{0}' in the cache as {1}.", adObjectCacheEntry.AdObjectData, adObjectCacheEntry); } else { flag6 = false; } } if (!flag6) { this.m_cache[identifyingName] = adObjectCacheEntry; ExTraceGlobals.ActiveManagerClientTracer.TraceDebug <TADWrapperObject, AdObjectCacheEntry <TADWrapperObject> >((long)this.GetHashCode(), "Stored object '{0}' in the cache as {1}.", adObjectCacheEntry.AdObjectData, adObjectCacheEntry); } } else { AdObjectLookupCache <TADWrapperObject> .Tracer.TraceError((long)this.GetHashCode(), "Timeout waiting for write lock in AdObjectLookupCache.LookupOrFindAdObject()"); } } finally { if (flag8) { this.m_rwLock.ExitWriteLock(); } } } return(adObjectCacheEntry.AdObjectData); }
private static bool ShouldExpireCacheEntry(AdObjectCacheEntry <TADWrapperObject> entry) { return(DateTime.UtcNow.CompareTo(entry.TimeToExpire) > 0); }