示例#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)
                );
        }
        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);
        }
        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());
        }
        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());
        }
        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
 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;
 }
 public LoginService(ICacheContext cacheContext, IHttpContextAccessor httpContextAccessor)
 {
     _cacheContext        = cacheContext;
     _httpContextAccessor = httpContextAccessor;
 }
 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
 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
 protected ControllerBase(ICacheClient cache) : this()
 {
     Cache        = cache;
     CacheContext = new CacheContext(Cache);
 }
示例#23
0
 public UserManagerController(ICacheContext cacheContext, IUserService userService) : base(cacheContext, userService)
 {
 }
示例#24
0
 /// <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;
 }