コード例 #1
0
        public ICacheEntry CreateEntry(object key)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (_cacheConfiguration.Enabled)
            {
                ICacheEntry entry = _memoryCache.CreateEntry(new CacheEntryKey(_name, key));

                if (_cacheConfiguration.Expiry.Type == CacheExpiryType.Absolute)
                {
                    entry.SetAbsoluteExpiration(_cacheConfiguration.Expiry.TTL);
                }
                else if (_cacheConfiguration.Expiry.Type == CacheExpiryType.Sliding)
                {
                    entry.SetSlidingExpiration(_cacheConfiguration.Expiry.TTL);
                }

                return(entry);
            }

            return(new CacheEntry(key));
        }
コード例 #2
0
        Task <Song> GenerateCacheEntry(ICacheEntry cacheEntry, uint songID, CancellationToken cancellation)
        {
            lock (cacheEntry) {
                if (cacheEntry.Value is Task <Song> generating)
                {
                    return(generating);
                }
                else
                {
                    cacheEntry.SetSlidingExpiration(TimeSpan.FromHours(24));
                    Task <Song> result = this.FetchOrGenerateSong(songID, cancellation);
                    cacheEntry.SetAbsoluteExpiration(TimeSpan.FromDays(7));
                    cacheEntry.Value = result;
                    cacheEntry.Dispose();
                    result.ContinueWith(songTask => {
                        if (!songTask.IsCompletedSuccessfully)
                        {
                            return;
                        }

                        Song song = songTask.Result;
                        cacheEntry.SetSize(
                            song.Title?.Length + song.Lyrics?.Length +
                            song.GeneratorError?.Length ?? 64);
                    });
                    return(result);
                }
            }
        }
コード例 #3
0
 private static User PrepareUserResult(ICacheEntry cacheEntry, AutoRestClients.PlatformModuleApi.Models.ApplicationUserExtended userDto)
 {
     if (userDto != null)
     {
         cacheEntry.SetAbsoluteExpiration(TimeSpan.FromMinutes(1));
         cacheEntry.AddExpirationToken(SecurityCacheRegion.CreateChangeToken(userDto.Id));
         return(userDto.ToUser());
     }
     return(null);
 }
コード例 #4
0
        /// <summary>
        /// Adds an object to distributed cache
        /// </summary>
        /// <param name="key">Cache Key</param>
        /// <param name="value">Cache object</param>
        /// <param name="options">Distributed cache options. <see cref="ExtendedDistributedCacheEntryOptions"/></param>
        public void Set(string key, byte[] value, ExtendedDistributedCacheEntryOptions options)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            using (ICacheEntry cacheEntry = CreateEntry(key))
            {
                var memoryCacheEntryOptions = GetMemoryCacheEntryOptions(options, value.LongLength);
                cacheEntry.SetOptions(memoryCacheEntryOptions);
                if (memoryCacheEntryOptions.AbsoluteExpiration != null)
                {
                    cacheEntry.SetAbsoluteExpiration(memoryCacheEntryOptions.AbsoluteExpiration.Value);
                }
                if (memoryCacheEntryOptions.AbsoluteExpirationRelativeToNow != null)
                {
                    cacheEntry.SetAbsoluteExpiration(memoryCacheEntryOptions.AbsoluteExpirationRelativeToNow.Value);
                }
                if (memoryCacheEntryOptions.SlidingExpiration != null)
                {
                    cacheEntry.SetSlidingExpiration(memoryCacheEntryOptions.SlidingExpiration.Value);
                }
                cacheEntry.SetPriority(memoryCacheEntryOptions.Priority);
                cacheEntry.SetSize(value.LongLength);
                cacheEntry.SetValue(value);
            }
        }
コード例 #5
0
        private async Task <IGitHubClient> createNewGitHubClient(ICacheEntry cacheEntry, long installationId)
        {
            var gitHubClient = new GitHubClient(new Octokit.ProductHeaderValue("Ahk"))
            {
                Credentials = new Credentials(await getInstallationToken(installationId))
            };

            gitHubClient.SetRequestTimeout(TimeSpan.FromSeconds(15));

            cacheEntry.SetValue(gitHubClient);
            cacheEntry.SetAbsoluteExpiration(TimeSpan.FromMinutes(5));

            return(gitHubClient);
        }
コード例 #6
0
 private void SetExpiration(ICacheEntry entry, CancellationTokenSource tokenSourceBeforeComputingValue, double inMinutes, Action <ICacheEntry>?configureEntry = null)
 {
     lock (_tokenSourceSync)
     {
         configureEntry?.Invoke(entry);
         if (tokenSourceBeforeComputingValue != ClearCacheTokenSource)
         {
             entry.SetAbsoluteExpiration(TimeSpan.FromTicks(1));
         }
         else
         {
             entry.SetSlidingExpiration(TimeSpan.FromMinutes(inMinutes))
             .AddExpirationToken(new CancellationChangeToken(ClearCacheTokenSource.Token));
         }
     }
 }
コード例 #7
0
 /// <summary>
 /// 设置默认缓存时间,3分钟。
 /// </summary>
 /// <param name="cache">缓存实体接口。</param>
 /// <returns>返回缓存实体接口。</returns>
 public static ICacheEntry SetDefaultAbsoluteExpiration(this ICacheEntry cache)
 {
     return(cache.SetAbsoluteExpiration(Cores.DefaultCacheExpiration));
 }
コード例 #8
0
 void SetupCacheEntry(ICacheEntry cacheEntry)
 {
     cacheEntry.SetSize(_cacheSize);
     cacheEntry.SetSlidingExpiration(_slidingExpirationTime);
     cacheEntry.SetAbsoluteExpiration(_absoluteExpirationTime);
 }