private bool TryGetCachedWithWait <TResult>(string cacheKey, IDataCacheStore cache, out TResult cachedResult) { int nextWaitMs = 20; int totalWaitMs = 0; // Use Sleep() instead of locks to avoid lock complexity. // This should not happen very often so performance is secondary. while (totalWaitMs < maxWaitForOtherThreadExec.TotalMilliseconds) { if (TryGetCached(cacheKey, cache, out cachedResult)) { return(true); } // Wait a while and hope for executing thread to finish Thread.Sleep(nextWaitMs); totalWaitMs += nextWaitMs; nextWaitMs = Math.Min(2 * nextWaitMs, 200); if (totalWaitMs + nextWaitMs > maxWaitForOtherThreadExec.TotalMilliseconds) { nextWaitMs = (int)maxWaitForOtherThreadExec.TotalMilliseconds - totalWaitMs; } } return(TryGetCached(cacheKey, cache, out cachedResult)); }
/// <summary> /// Initializes a new instance of the <see cref="DataCacheHandler"/> class. Enables /// possibility to change actual storage of cached items. Default is DataCacheStore. /// </summary> /// <param name="theDataCache"> /// Storage of the actual data. It must be available /// a little longer than the keys in DataTimeOutCache. /// </param> /// <param name="theDataTimeOutCache"> /// One value stored for each DataCache value. These /// values has expire set to data expire time. /// </param> /// <param name="theFetchFlagCache"> /// Cache for marking that fetching of data has been started by using the exec function. /// </param> /// <param name="theFailCountCache"> /// Cache for counting the number of fails per resource. /// </param> public DataCacheHandler(IDataCacheStore theDataCache, IDataCacheStore theDataTimeOutCache, IDataCacheStore theFetchFlagCache, IDataCacheStore theFailCountCache) { _dataCache = theDataCache; _dataTimeoutCache = theDataTimeOutCache; _fetchFlagCache = theFetchFlagCache; _failCountCache = theFailCountCache; GetAppSettings(); }
/// <summary> /// Initializes a new instance of the <see cref="DataCacheHandler"/> class. Creating a new bucket of /// cache items. This is accomplished by using another prefix for storing cached items. /// </summary> /// <param name="cachePrefix"> /// The cache key prefix. /// </param> public DataCacheHandler(string cachePrefix) { _dataCache = new DataCacheStore(cachePrefix + ":", cachePrefix + ":"); _dataTimeoutCache = new DataCacheStore(cachePrefix + "Timeout:", cachePrefix + ":"); _fetchFlagCache = new DataCacheStore(cachePrefix + "FetchFlag:", cachePrefix + ":"); _failCountCache = new DataCacheStore(cachePrefix + "FailCount:", cachePrefix + ":"); GetAppSettings(); }
private static TResult GetCachedOrDefault <TResult>(string cacheKey, IDataCacheStore cache, TResult defaultResult) { object item; if (cache.TryGetValue(cacheKey, out item)) { if (item is TResult result) { return(result); } } return(defaultResult); }
private static bool TryGetCached <TResult>(string cacheKey, IDataCacheStore cache, out TResult cachedResult) { TResult defaultDummy = default(TResult); var result = GetCachedOrDefault(cacheKey, cache, defaultDummy); if (!Equals(result, defaultDummy)) { // Not default, so return from cache cachedResult = result; return(true); } cachedResult = defaultDummy; return(false); }