private async Task <T?> GetContentAsync <T>(ICacheEntry entry, string path, Func <IFileInfo, Task <T> > getContent)
        {
            var fileProvider = _hostingEnvironment.WebRootFileProvider;
            var changeToken  = fileProvider.Watch(path);

            entry.SetPriority(CacheItemPriority.NeverRemove);
            entry.AddExpirationToken(changeToken);

            var file = fileProvider.GetFileInfo(path);

            if ((file is not {
                Exists : true
            }))
예제 #2
0
        private async Task <T?> GetContentAsync <T>(ICacheEntry entry, string path, Func <IFileInfo, Task <T> > getContent)
            where T : class
        {
            var fileProvider = _webHostEnvironment.WebRootFileProvider;
            var changeToken  = fileProvider.Watch(path);

            entry.SetPriority(CacheItemPriority.NeverRemove);
            entry.AddExpirationToken(changeToken);

            var file = fileProvider.GetFileInfo(path);

            if (file == null || !file.Exists)
            {
                return(default);
        private async Task <string?> Get(ICacheEntry entry, string path)
        {
            IFileProvider fileProvider = HostingEnvironment.WebRootFileProvider;
            IChangeToken  changeToken  = fileProvider.Watch(path);

            entry.SetPriority(CacheItemPriority.NeverRemove);
            entry.AddExpirationToken(changeToken);

            IFileInfo file = fileProvider.GetFileInfo(path);

            if (!file.Exists)
            {
                return(null);
            }

            return(await ReadFileContent(file));
        }
예제 #4
0
        private async Task <T> GetContentAsync <T>(ICacheEntry entry, string path, Func <IFileInfo, Task <T> > getContent)
        {
            var urlHelper = _urlHelperFactory.GetUrlHelper(ViewContext);

            path = urlHelper.Content(path);

            var fileProvider = _hostingEnvironment.WebRootFileProvider;
            var changeToken  = fileProvider.Watch(path);

            entry.SetPriority(CacheItemPriority.NeverRemove);
            entry.AddExpirationToken(changeToken);

            var file = fileProvider.GetFileInfo(path);

            if (file == null || !file.Exists)
            {
                return(default(T));
            }

            return(await getContent(file));
        }
예제 #5
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);
            }
        }