コード例 #1
0
        public async Task Put(ICacheContext context, CacheEntry newCacheEntry)
        {
            var key = await GetKey(context);

            var        isResponseMessage = false;
            CacheEntry cacheEntry        = null;
            var        temp = await _cache.Get <CacheEntry>(key);

            if (temp != null && context.ResponseValidator(context, temp.Metadata) == ResponseValidationResult.OK)
            {
                cacheEntry = temp;
            }

            var response = newCacheEntry.Value as HttpResponseMessage;

            if (cacheEntry == null)
            {
                object value;
                if (response != null)
                {
                    value = await _serializer.Serialize(response, context.Token);

                    isResponseMessage = true;
                }
                else
                {
                    value = newCacheEntry.Value;
                }

                cacheEntry = new CacheEntry(newCacheEntry.Metadata)
                {
                    Value = value,
                    IsHttpResponseMessage = isResponseMessage
                };
            }
            else
            {
                cacheEntry.Metadata.Merge(newCacheEntry.Metadata);
            }

            await Task.WhenAll(
                _cache.Put(key, cacheEntry),
                _varyBy.Put(context.Uri, newCacheEntry.Metadata.VaryHeaders),
                _uriInfo.Put(context.Uri, key)
                );
        }
コード例 #2
0
        public bool TryFindMatchingMember(
            string destinationName,
            IModelValueMemberInfo destinationMemberInfo,
            ICacheContext <string, IModelValueMemberInfo> sourceMemberInfos,
            out string sourceMemberName,
            out IModelValueMemberInfo sourceMemberInfo)
        {
            if (sourceMemberInfos.TryGetValue(destinationName, out sourceMemberInfo))
            {
                sourceMemberName = sourceMemberInfo.Name;
                return(true);
            }

            sourceMemberName = default;
            sourceMemberInfo = default;
            return(false);
        }
コード例 #3
0
        public async Task <Modifiable> OnStore(ICacheContext context, object result)
        {
            CacheStoreContext handlerContext = null;

            foreach (var handlerInfo in GetHandlerInfo(CacheHandlerType.Store))
            {
                if (handlerContext == null)
                {
                    handlerContext = ((Func <ICacheContext, object, CacheStoreContext>)handlerInfo.InitialConstructor)(context, result);
                }
                else
                {
                    handlerContext = ((Func <CacheStoreContext, CacheStoreContext>)handlerInfo.ContinuationConstructor)(handlerContext);
                }

                await handlerInfo.Handler(handlerContext);
            }

            return((handlerContext ?? new CacheStoreContext <object>(context, null)).GetHandlerResult());
        }
コード例 #4
0
        public async Task <Modifiable> OnExpiring(ICacheContext context, RequestValidationResult reason)
        {
            CacheExpiringContext handlerContext = null;

            foreach (var handlerInfo in GetHandlerInfo(CacheHandlerType.Expiring))
            {
                if (handlerContext == null)
                {
                    handlerContext = ((Func <ICacheContext, RequestValidationResult, CacheExpiringContext>)handlerInfo.InitialConstructor)(context, reason);
                }
                else
                {
                    handlerContext = ((Func <CacheExpiringContext, CacheExpiringContext>)handlerInfo.ContinuationConstructor)(handlerContext);
                }

                await handlerInfo.Handler(handlerContext);
            }

            return((handlerContext ?? new CacheExpiringContext(context, 0)).GetHandlerResult());
        }
コード例 #5
0
        public async Task <Modifiable> OnExpired(ICacheContext context, RequestValidationResult reason, IReadOnlyCollection <Uri> expiredUris)
        {
            CacheExpiredContext handlerContext = null;

            foreach (var handlerInfo in GetHandlerInfo(CacheHandlerType.Expired))
            {
                if (handlerContext == null)
                {
                    handlerContext = ((Func <ICacheContext, RequestValidationResult, IReadOnlyCollection <Uri>, CacheExpiredContext>)handlerInfo.InitialConstructor)(context, reason, expiredUris);
                }
                else
                {
                    handlerContext = ((Func <CacheExpiredContext, CacheExpiredContext>)handlerInfo.ContinuationConstructor)(handlerContext);
                }

                await handlerInfo.Handler(handlerContext);
            }

            return((handlerContext ?? new CacheExpiredContext(context, 0, null)).GetHandlerResult());
        }
コード例 #6
0
        private async Task ExpireResult(ICacheContext context, RequestValidationResult reason)
        {
            if (!Enabled)
            {
                return;
            }

            if ((context.Items["CacheHandler_ItemExpired"] as bool?).GetValueOrDefault())
            {
                return;
            }

            var expiringResult = await context.HandlerRegister.OnExpiring(context, reason);

            var uris = await _cacheManager.Delete(context, expiringResult.Value as IEnumerable <Uri>);

            await context.HandlerRegister.OnExpired(context, reason, new ReadOnlyCollection <Uri>(uris));

            context.Items["CacheHandler_ItemExpired"] = true;
        }
コード例 #7
0
        private CacheEntry <TResult> CreateEntry(TKey k, Func <ICacheContext <TKey>, TResult> context)
        {
            var entry          = new CacheEntry <TResult>();
            var currentContext = new DefaultCacheContext <TKey>(k, entry.AddToken);

            ICacheContext parentContext = null;

            try
            {
                parentContext = cacheContextAccessor.Current;
                cacheContextAccessor.Current = currentContext;

                entry.Result = context(currentContext);
            }
            finally
            {
                cacheContextAccessor.Current = parentContext;
            }
            entry.CompactTokens();
            return(entry);
        }
コード例 #8
0
        public static RequestValidationResult CanCacheRequest(ICacheContext context)
        {
            if (context.Uri == null)
            {
                return(RequestValidationResult.NoRequestInfo);
            }

            var validation = CanCacheRequest(context.Request, context.CacheableHttpMethods);

            if (validation != RequestValidationResult.OK)
            {
                return(validation);
            }

            if (typeof(IEmptyResult).IsAssignableFrom(context.ResultType))
            {
                return(RequestValidationResult.ResultIsEmpty);
            }

            return(RequestValidationResult.OK);
        }
コード例 #9
0
        public async Task <CacheEntry> Get(ICacheContext context)
        {
            var key = await GetKey(context);

            var cacheEntry = await _cache.Get <CacheEntry>(key);

            if (cacheEntry == null)
            {
                return(CacheEntry.Empty);
            }

            var validation = context.ResponseValidator(context, cacheEntry.Metadata);

            if (validation == ResponseValidationResult.Stale && !context.AllowStaleResultValidator(context, cacheEntry.Metadata))
            {
                return(CacheEntry.Empty);
            }

            if (validation != ResponseValidationResult.OK &&
                validation != ResponseValidationResult.Stale &&
                validation != ResponseValidationResult.MustRevalidate)
            {
                return(CacheEntry.Empty);
            }

            object result;

            if (cacheEntry.IsHttpResponseMessage)
            {
                var responseBuffer = cacheEntry.Value as byte[];

                result = await _serializer.Deserialize(responseBuffer, context.Token);
            }
            else
            {
                result = cacheEntry.Value;
            }

            return(new CacheEntry(result, cacheEntry.Metadata));
        }
コード例 #10
0
        public async Task <IList <Uri> > Delete(ICacheContext context, IEnumerable <Uri> additionalDependentUris)
        {
            var key = await GetKey(context);

            var uris = new List <Uri>();

            if (key == CacheKey.Empty)
            {
                return(uris);
            }

            var cacheEntry = await _cache.Get <CacheEntry>(key);

            if (cacheEntry == null)
            {
                return(uris);
            }

            var deleteTask    = _cache.Delete(key);
            var deleteUriTask = _uriInfo.DeleteKey(context.Uri, key);

            await Task.WhenAll(deleteTask, deleteUriTask);

            if (!deleteTask.Result)
            {
                return(uris);
            }

            if (context.Uri != null)
            {
                uris.Add(context.Uri);
            }

            var relatedUris = await DeleteDependentUris(cacheEntry, context, additionalDependentUris);

            uris.AddRange(relatedUris);

            return(uris);
        }
コード例 #11
0
ファイル: Client.cs プロジェクト: FarDragi/DiscordCs
 private void StartConfig(ClientConfig clientConfig)
 {
     _clientConfig = clientConfig;
     _restContext  = clientConfig.GetRest();
     _restContext.Init();
     _cacheContext      = clientConfig.CacheContext;
     _serializerOptions = new JsonSerializerOptions()
     {
         Converters =
         {
             new MemberCollectionConverter(_cacheContext,  this,         Logger),
             new UserCollectionConverter(_cacheContext,    this,         Logger),
             new ChannelCollectionConverter(_cacheContext, this,         _restContext,Logger),
             new ChannelConverter(_cacheContext,           _restContext, Logger),
             new ULongConverter(),
             new TimeSpanConverter()
         }
     };
     _gatewayContext = clientConfig.GetGateway();
     Logger          = clientConfig.Logger;
     Logger.Log(LoggingLevel.Dcs, "DiscosCs v0.1-dev");
     InitCollections();
 }
コード例 #12
0
 public AccountController(ICacheContext cacheContext, IUserService userService) : base(cacheContext, userService)
 {
 }
コード例 #13
0
 public LoginParse(AppInfoService infoService, ICacheContext cacheContext, IRepository <User> userApp)
 {
     _appInfoService = infoService;
     _cacheContext   = cacheContext;
     _app            = userApp;
 }
コード例 #14
0
 public LoginService(ICacheContext cacheContext, IHttpContextAccessor httpContextAccessor)
 {
     _cacheContext        = cacheContext;
     _httpContextAccessor = httpContextAccessor;
 }
コード例 #15
0
 public CheckController(AuthContextFactory app, LoginParse loginParse, ICacheContext cacheContext)
 {
     _app          = app;
     _loginParse   = loginParse;
     _cacheContext = cacheContext;
 }
コード例 #16
0
 public MedicineController(ICacheContext cacheContext, IUserService userService, IMedicineService medicineService) : base(cacheContext, userService)
 {
     _medicineService = medicineService;
 }
コード例 #17
0
ファイル: DictController.cs プロジェクト: gzl118/netcoreframe
 public DictController(WholeInjection injection, ICacheContext cacheContext)
 {
     _cacheContext  = cacheContext;
     this.injection = injection;
 }
コード例 #18
0
 public Handler(ICacheContext cacheContext, IHttpContextAccessor httpContextAccessor)
 {
     this.cacheContext = cacheContext;
     this.httpContext  = httpContextAccessor.HttpContext;
 }
コード例 #19
0
 private Task <CacheKey> GetKey(ICacheContext context)
 {
     return(GetKey(context.ResultType, context.Uri, context.DefaultVaryByHeaders, context.Request?.Headers));
 }
コード例 #20
0
 public PurchaseController(ICacheContext cacheContext, IUserService userService) : base(cacheContext, userService)
 {
 }
コード例 #21
0
 public ChannelConverter(ICacheContext cacheContext, IRestContext restContext, ILogger logger)
 {
     _cacheContext = cacheContext;
     _restContext  = restContext;
     _logger       = logger;
 }
コード例 #22
0
ファイル: ControllerBase.cs プロジェクト: zfq308/MvcKickstart
 protected ControllerBase(ICacheClient cache) : this()
 {
     Cache        = cache;
     CacheContext = new CacheContext(Cache);
 }
コード例 #23
0
 public UserManagerController(ICacheContext cacheContext, IUserService userService) : base(cacheContext, userService)
 {
 }
コード例 #24
0
ファイル: CacheFactory.cs プロジェクト: kindredagency/Caching
 /// <summary>
 ///     Gets the context.
 /// </summary>
 /// <returns>ICacheContext.</returns>
 public static ICacheContext GetContext()
 {
     return _cacheContext ?? (_cacheContext = new CacheContext());
 }
コード例 #25
0
 public Task <bool> Contains <T>(string key, ICacheContext context) where T : class
 {
     return(ContainsWithContext <T>(key, context as TCacheContext));
 }
コード例 #26
0
 public void Set <T>(string key, T value, ICacheContext context) where T : class
 {
     SetWithContext(key, value, context as TCacheContext);
 }
コード例 #27
0
 public Task <T> Get <T>(string key, ICacheContext context) where T : class
 {
     return(GetWithContext <T>(key, context as TCacheContext));
 }
コード例 #28
0
 public UserCollectionConverter(ICacheContext cacheContext, IDatas datas, ILogger logger)
 {
     _cacheContext = cacheContext;
     _datas        = datas;
     _logger       = logger;
 }
コード例 #29
0
 public CacheHitContext(ICacheContext context, TResult result)
     : base(context, result)
 {
 }
コード例 #30
0
 public CacheContext(ICacheContext context)
     : this(context, context.GetHandlerContext())
 {
 }
コード例 #31
0
 public RedisCacheService(ICacheContext context)
 {
     _context = context;
 }