예제 #1
0
        private async Task <IEnumerable <ProxyDatabaseResult> > FetchResults(ulong account, ICacheEntry entry)
        {
            _logger.Information("Members for account {Account} not in cache, fetching", account);
            using (var conn = await _conn.Obtain())
            {
                var results = (await conn.QueryAsync <PKMember, PKSystem, ProxyDatabaseResult>(
                                   "select members.*, systems.* from members, systems, accounts where members.system = systems.id and accounts.system = systems.id and accounts.uid = @Uid",
                                   (member, system) =>
                                   new ProxyDatabaseResult {
                    Member = member, System = system
                }, new { Uid = account })).ToList();

                if (results.Count == 0)
                {
                    // Long expiry for accounts with no system registered
                    entry.SetSlidingExpiration(TimeSpan.FromMinutes(5));
                    entry.SetAbsoluteExpiration(TimeSpan.FromHours(1));
                }
                else
                {
                    // Shorter expiry if they already have a system
                    entry.SetSlidingExpiration(TimeSpan.FromMinutes(1));
                    entry.SetAbsoluteExpiration(TimeSpan.FromMinutes(5));
                }

                return(results);
            }
        }
예제 #2
0
        private async Task <IEnumerable <Prize> > FetchPrizes(ICacheEntry arg)
        {
            arg.SetAbsoluteExpiration(TimeSpan.FromMinutes(5));
            arg.SetSlidingExpiration(TimeSpan.FromSeconds(30));

            return(await FetchPrizes());
        }
예제 #3
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));
        }
예제 #4
0
 /// <summary>
 /// 设置 动态程序集滑动过期时间 10 秒
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="type"></param>
 internal static void SetDynamicAssemblyPolicy(this ICacheEntry entry, Type?type)
 {
     if (type?.Assembly.IsDynamic ?? false)
     {
         entry.SetSlidingExpiration(TimeSpan.FromSeconds(10));
     }
 }
예제 #5
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);
                }
            }
        }
예제 #6
0
        private async Task <T> CacheFromApi <T>(Uri uri, ICacheEntry entry, CancellationToken cancellationToken)
        {
            var @object = await GetFromApi <T>(uri, cancellationToken)
                          .ConfigureAwait(false);

            entry
            .SetSlidingExpiration(TimeSpan.FromHours(1))
            .SetValue(@object);

            return(@object);
        }
예제 #7
0
        private async Task <OSM> CreateCache(int id, ICacheEntry entry, DateTime?dateTime)
        {
            entry.SetSlidingExpiration(TimeSpan.FromMinutes(15));
            var text = await GetRelationFromOSMAsync(id, dateTime);

            if (text == null)
            {
                return(null);
            }
            var osm = JsonConvert.DeserializeObject <OSM>(text.ToString());

            return(osm);
        }
 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));
         }
     }
 }
예제 #9
0
        private async Task <ISchema> Create(ICacheEntry cacheEntry)
        {
            cacheEntry.SetSlidingExpiration(TimeSpan.FromHours(6));

            try
            {
                // create channelsSchema by introspecting channels service
                var channelsLink = Links.SignalROrHttp(
                    _configuration["Remotes:Channels"],
                    _configuration["Remotes:ChannelsHttp"],
                    _accessor);

                var channelsSchema = RemoteSchemaTools.MakeRemoteExecutable(
                    await new SchemaBuilder()
                    .ImportIntrospectedSchema(channelsLink),
                    channelsLink);

                // create messagesSchema by introspecting messages service
                var messagesLink = Links.SignalR(
                    _configuration["Remotes:Messages"],
                    _accessor);

                var messagesSchema = RemoteSchemaTools.MakeRemoteExecutable(
                    await new SchemaBuilder()
                    .ImportIntrospectedSchema(messagesLink),
                    messagesLink);

                // combine schemas into one
                var schema = new SchemaBuilder()
                             .Merge(channelsSchema, messagesSchema)
                             .Build();

                // introspect and merge with schema
                var introspection = Introspect.Schema(schema);
                return(new SchemaBuilder()
                       .Merge(schema, introspection)
                       .Build());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #10
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);
            }
        }
예제 #11
0
        private async Task <ISchema> Create(ICacheEntry cacheEntry)
        {
            cacheEntry.SetSlidingExpiration(TimeSpan.FromHours(6));

            try
            {
                var schemaBuilder = await SchemaLoader.Load();

                var resolvers = new SchemaResolvers();

                var schema = SchemaTools.MakeExecutableSchemaWithIntrospection(
                    schemaBuilder,
                    resolvers,
                    resolvers);

                return(schema);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #12
0
 /// <summary>
 /// Sets the default cache entry options.
 /// </summary>
 public static ICacheEntry SetDefaultOptions(this ICacheEntry cacheEntry, IWebContext webContext)
 {
     cacheEntry.SetSlidingExpiration(CacheExpiration);
     cacheEntry.AddExpirationToken(webContext);
     return(cacheEntry);
 }
예제 #13
0
 private async Task <List <OSMLineDTO> > CreateCacheLinesNetwork(string network, DateTime?dateTime, ICacheEntry entry)
 {
     entry.SetSlidingExpiration(TimeSpan.FromMinutes(15));
     return(await CreateNetworkRoutesListAsync(network, dateTime));
 }
예제 #14
0
 private async Task <List <OSMLineDTO> > CreateCacheLines(string reference, OSMRouteType?routeType, string network, ICacheEntry entry, DateTime?dateTime)
 {
     entry.SetSlidingExpiration(TimeSpan.FromMinutes(15));
     return(await CreateRoutesListAsync(reference, routeType, network, dateTime));
 }
예제 #15
0
 void SetupCacheEntry(ICacheEntry cacheEntry)
 {
     cacheEntry.SetSize(_cacheSize);
     cacheEntry.SetSlidingExpiration(_slidingExpirationTime);
     cacheEntry.SetAbsoluteExpiration(_absoluteExpirationTime);
 }