Example #1
0
        public async Task <bool> SetItemAsync <TItem>(string key, ICachedItem <TItem> item, TimeSpan?timeout = null,
                                                      CancellationToken?ct = null)
            where TItem : class
        {
            try
            {
                return(await _database.StringSetAsync(key, JsonConvert.SerializeObject(item),
                                                      TimeSpan.FromMilliseconds(item.ExtendedTimeToLiveMs.GetValueOrDefault(item.TimeToLiveMs))).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                _logService.Error(
                    $"Failed to set item with key '{key}' on Redis Cache Connection '{ConnectionName}': {ex.Message} ({ex.GetType()})",
                    ex);

                return(false);
            }
        }
Example #2
0
        private void UpdateExpiration(DateTime lastUpdatedTime, ICachedItem cachedItem)
        {
            LastUpdatedTime = lastUpdatedTime;

            if (cachedItem == null || cachedItem.ExpirationTime == null)
            {
                _valueExpirationTime = LastUpdatedTime.Add(CacheTime);
            }
            else
            {
                _valueExpirationTime = cachedItem.ExpirationTime.Value;
            }

            if (CachePolicy == AgFx.CachePolicy.AutoRefresh)
            {
                AutoRefreshService.Current.ScheduleRefresh(this);
            }
        }
Example #3
0
        public async Task <ICachedItem <TItem> > GetItemAsync <TItem>(string key, TimeSpan?timeout = null, CancellationToken?ct = null)
            where TItem : class
        {
            var previousProviders = new List <ICachingProvider>();
            var providers         = await GetProvidersAsync().ConfigureAwait(false);

            ICachedItem <TItem> item      = null;
            ICachedItem <TItem> bestMatch = null;

            foreach (var provider in providers)
            {
                var found = await provider.GetItemAsync <TItem>(key, timeout, ct).ConfigureAwait(false);

                if (!found.IsStale)
                {
                    item = found;
                    break;
                }

                if (!found.IsExpired)
                {
                    bestMatch = found;
                }

                previousProviders.Add(provider);
            }

            // NOTE: Intentional fire-and-forget.  We don't want to wait for cache "percolation" to complete.
#pragma warning disable 4014
            if (item != null)
            {
                _ = Task.Run(async() =>
                             await Task.WhenAll(previousProviders.Select(x => x.SetItemAsync(key, item, timeout, ct)))
                             .ConfigureAwait(false));
            }
#pragma warning restore 4014

            return(item ?? bestMatch);
        }
Example #4
0
 public async Task <bool> SetItemAsync <TItem>(string key, ICachedItem <TItem> item, TimeSpan?timeout = null, CancellationToken?ct = null)
     where TItem : class =>
 (await ParallelExecuteAsync(provider => provider.SetItemAsync(key, item, timeout, ct)).ConfigureAwait(false))
 .All(x => x);
Example #5
0
        private void UpdateExpiration(DateTime lastUpdatedTime, ICachedItem cachedItem)
        {
            LastUpdatedTime = lastUpdatedTime;

            if (cachedItem == null || cachedItem.ExpirationTime == null)
            {
                _valueExpirationTime = LastUpdatedTime.Add(CacheTime);
            }
            else
            {
                _valueExpirationTime = cachedItem.ExpirationTime.Value;
            }

            if (CachePolicy == CachePolicy.AutoRefresh)
            {
                AutoRefreshService.Current.ScheduleRefresh(this);
            }
        }
Example #6
0
        public async Task <bool> SetItemAsync <TItem>(string connectionName, string key, ICachedItem <TItem> item, TimeSpan?timeout = null, CancellationToken?ct = null)
            where TItem : class
        {
            var connection = await GetProviderAsync(connectionName).ConfigureAwait(false);

            return((await Task.WhenAll(connection.SetItemAsync(key, item, timeout, ct),
                                       _invalidationService.SendInvalidationAsync(connectionName ?? "default", key)).ConfigureAwait(false))
                   .All(x => x));
        }
Example #7
0
 public Task <bool> SetItemAsync <TItem>(string key, ICachedItem <TItem> item, TimeSpan?timeout = null, CancellationToken?ct = null)
     where TItem : class
 {
     _items[key] = item;
     return(Task.FromResult(true));
 }