public void AutomaticallyRemovesExpiredItemsOfACacheStorageWithDefaultExpirationPolicyInitializationCode() { var cache = new CacheStorage <string, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(500))); cache.Add("1", 1); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(2000); Assert.IsFalse(cache.Contains("1")); }
public async Task <Store> GetAsync(string url, bool force = false, params object[] parameters) { var uri = new Uri(url); url = $"{uri.Scheme}://{uri.DnsSafeHost}{(uri.Port != 80 && uri.Port != 443 ? $":{uri.Port}" : string.Empty)}/{uri.Segments[1].Trim(' ', '/')}/Products?depPid=0"; return(await this.cacheStorage.GetFromCacheOrFetchAsync( url, async() => await this.GetDirectAsync(url), ExpirationPolicy.Duration(ScrappingConfiguration.StoreCacheExpiration), force)); }
public void RunMultipleThreadsWithRandomAccessCalls() { var cacheStorage = new CacheStorage <Guid, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(1))); var threads = new List <Thread>(); for (int i = 0; i < 25; i++) { var thread = new Thread(() => { var random = new Random(); for (int j = 0; j < 10000; j++) { var randomGuid = _randomGuids[random.Next(0, 9)]; cacheStorage.GetFromCacheOrFetch(randomGuid, () => { var threadId = Thread.CurrentThread.ManagedThreadId; Log.Info("Key '{0}' is now controlled by thread '{1}'", randomGuid, threadId); return(threadId); }); ThreadHelper.Sleep(1); } }); threads.Add(thread); thread.Start(); } while (true) { bool anyThreadAlive = false; foreach (var thread in threads) { if (thread.IsAlive) { anyThreadAlive = true; break; } } if (!anyThreadAlive) { break; } ThreadHelper.Sleep(500); } }
public async Task <Product> GetAsync(string url, bool force = false, params object[] parameters) { var store = parameters?.OfType <Store>().FirstOrDefault(); var department = parameters?.OfType <Department>().FirstOrDefault(); var disabledProducts = parameters?.OfType <ImmutableSortedSet <string> >().FirstOrDefault(); url = UriHelper.EnsureProductUrl(url); return(await this.cacheStorage.GetFromCacheOrFetchAsync( $"{url}/{store != null}/{department != null}", async() => await this.GetDirectAsync(url, store, department, disabledProducts), ExpirationPolicy.Duration(ScraperConfigurations.ProductCacheExpiration), force)); }
private void RunMultipleThreadsWithRandomAccessCalls(Func <ICacheStorage <Guid, int>, Guid, int> retrievalFunc) { var cacheStorage = new CacheStorage <Guid, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(250))); var threads = new List <Thread>(); for (var i = 0; i < 50; i++) { var thread = new Thread(() => { var random = new Random(); for (var j = 0; j < 1000; j++) { var randomGuid = _randomGuids[random.Next(0, 9)]; retrievalFunc(cacheStorage, randomGuid); ThreadHelper.Sleep(10); } }); threads.Add(thread); thread.Start(); } while (true) { var anyThreadAlive = false; foreach (var thread in threads) { if (thread.IsAlive) { anyThreadAlive = true; break; } } if (!anyThreadAlive) { break; } ThreadHelper.Sleep(500); } }
public async Task <Product> GetAsync(string url, bool force = false, params object[] parameters) { var store = parameters?.OfType <Store>().FirstOrDefault(); var department = parameters?.OfType <Department>().FirstOrDefault(); var disabledProducts = parameters?.OfType <ImmutableSortedSet <string> >().FirstOrDefault(); url = Regex.Replace( url, @"(&?)(page=\d+(&?)|img=\d+(&?))", string.Empty, RegexOptions.IgnoreCase).Trim(' '); return(await this.cacheStorage.GetFromCacheOrFetchAsync( $"{url}/{store != null}/{department != null}", async() => await this.GetDirectAsync(url, store, department, disabledProducts), ExpirationPolicy.Duration(ScrappingConfiguration.ProductCacheExpiration), force)); }
public void NotReturnsNullIfTicksOfTimeSpanIsLessOrEqualsToZeroIfForceIsEqualsTrue() { Assert.IsNotNull(ExpirationPolicy.Duration(new TimeSpan(0), true)); }
public void ReturnsNullIfTicksOfTimeSpanIsLessOrEqualsToZero() { Assert.IsNull(ExpirationPolicy.Duration(new TimeSpan(0))); }
/// <summary> /// Adds a value to the cache associated with to a key. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="override">Indicates if the key exists the value will be overridden.</param> /// <param name="expiration">The timespan in which the cache item should expire when added.</param> /// <exception cref="ArgumentNullException">The <paramref name="key" /> is <c>null</c>.</exception> public void Add(TKey key, TValue @value, bool @override = false, TimeSpan expiration = default(TimeSpan)) { Add(key, value, ExpirationPolicy.Duration(expiration), @override); }
/// <summary> /// Adds a value to the cache associated with to a key asynchronously. /// <para /> /// Note that this is a wrapper around <see cref="GetFromCacheOrFetch(TKey,System.Func{TValue},bool,TimeSpan)"/>. /// </summary> /// <param name="key">The key.</param> /// <param name="code">The deferred initialization code of the value.</param> /// <param name="override">Indicates if the key exists the value will be overridden.</param> /// <param name="expiration">The timespan in which the cache item should expire when added.</param> /// <returns>The instance initialized by the <paramref name="code" />.</returns> /// <exception cref="ArgumentNullException">If <paramref name="key" /> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="code" /> is <c>null</c>.</exception> public Task <TValue> GetFromCacheOrFetchAsync(TKey key, Func <Task <TValue> > code, bool @override = false, TimeSpan expiration = default(TimeSpan)) { return(GetFromCacheOrFetchAsync(key, code, ExpirationPolicy.Duration(expiration), @override)); }
/// <summary> /// Initializes a new instance of the <see cref="CacheStorageValueInfo{TValue}"/> class. /// </summary> /// <param name="value"> /// The value. /// </param> /// <param name="expiration"> /// The expiration. /// </param> public CacheStorageValueInfo(TValue value, TimeSpan expiration) : this(value, ExpirationPolicy.Duration(expiration)) { }