public async Task <string> Get(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            // if it contains the key, just return
            if (CacheExtensions.TryGetValue(_cache, key, out string cachedValue))
            {
                return(cachedValue);
            }

            var value = await GetOrCreate(key);

            if (value != null)
            {
                // the memory cache is threadsafe so we don't need to acquire the lock. the worst case
                // scenrio is we call the underlying consul service multiple times initially, but it would
                // be faster than getting the lock everytime
                CacheExtensions.Set <string>(_cache, key, value, new MemoryCacheEntryOptions()
                {
                    AbsoluteExpiration = DateTime.Now.Add(_expirationTimeSpan)
                });
            }

            return(value);
        }
Example #2
0
        // Token: 0x0600017E RID: 382 RVA: 0x00006EDC File Offset: 0x000050DC
        private Task <CompiledViewDescriptor> OnCacheMiss(string normalizedPath)
        {
            object cacheLock = this._cacheLock;
            ViewCompilerWorkItem    viewCompilerWorkItem;
            MemoryCacheEntryOptions memoryCacheEntryOptions;
            TaskCompletionSource <CompiledViewDescriptor> taskCompletionSource;

            lock (cacheLock)
            {
                Task <CompiledViewDescriptor> result;
                if (CacheExtensions.TryGetValue <Task <CompiledViewDescriptor> >(this._cache, normalizedPath, out result))
                {
                    return(result);
                }
                CompiledViewDescriptor precompiledView;
                if (this._precompiledViews.TryGetValue(normalizedPath, out precompiledView))
                {
                    this._logger.ViewCompilerLocatedCompiledViewForPath(normalizedPath);
                    viewCompilerWorkItem = this.CreatePrecompiledWorkItem(normalizedPath, precompiledView);
                }
                else
                {
                    viewCompilerWorkItem = this.CreateRuntimeCompilationWorkItem(normalizedPath);
                }
                memoryCacheEntryOptions = new MemoryCacheEntryOptions();
                for (int i = 0; i < viewCompilerWorkItem.ExpirationTokens.Count; i++)
                {
                    memoryCacheEntryOptions.ExpirationTokens.Add(viewCompilerWorkItem.ExpirationTokens[i]);
                }
                taskCompletionSource = new TaskCompletionSource <CompiledViewDescriptor>();
                if (!viewCompilerWorkItem.SupportsCompilation)
                {
                    taskCompletionSource.SetResult(viewCompilerWorkItem.Descriptor);
                }
                _cacheKeyList.Add(normalizedPath);
                CacheExtensions.Set <Task <CompiledViewDescriptor> >(this._cache, normalizedPath, taskCompletionSource.Task, memoryCacheEntryOptions);
            }
            if (viewCompilerWorkItem.SupportsCompilation)
            {
                CompiledViewDescriptor descriptor = viewCompilerWorkItem.Descriptor;
                if (((descriptor != null) ? descriptor.Item : null) != null && ChecksumValidator.IsItemValid(this._projectEngine.FileSystem, viewCompilerWorkItem.Descriptor.Item))
                {
                    taskCompletionSource.SetResult(viewCompilerWorkItem.Descriptor);
                    return(taskCompletionSource.Task);
                }
                this._logger.ViewCompilerInvalidingCompiledFile(viewCompilerWorkItem.NormalizedPath);
                try
                {
                    CompiledViewDescriptor compiledViewDescriptor = this.CompileAndEmit(normalizedPath);
                    compiledViewDescriptor.ExpirationTokens = memoryCacheEntryOptions.ExpirationTokens;
                    taskCompletionSource.SetResult(compiledViewDescriptor);
                }
                catch (Exception exception)
                {
                    taskCompletionSource.SetException(exception);
                }
            }
            return(taskCompletionSource.Task);
        }
Example #3
0
        public void Store(string key, object content, int duration)
        {
            object cached;

            if (Cache.TryGetValue(key, out cached))
            {
                Cache.Remove(key);
            }

            CacheExtensions.Set(Cache, key, content,
                                new MemoryCacheEntryOptions
            {
                AbsoluteExpiration = DateTime.Now + TimeSpan.FromSeconds(duration),
                Priority           = CacheItemPriority.Low
            });
        }
        public async Task <T> Get <T>(string key, TimeSpan expireIn, Func <Task <T> > method)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }
            var result = CacheExtensions.Get(this._cache, key);

            if (result != null)
            {
                return((T)result);
            }

            var keyLock = _locks.GetOrAdd(key, x => new SemaphoreSlim(1));
            await keyLock.WaitAsync();

            try
            {
                result = CacheExtensions.Get(this._cache, key);
                if (result != null)
                {
                    return((T)result);
                }

                var cacheItemPolicy = new MemoryCacheEntryOptions()
                {
                    AbsoluteExpiration = new DateTimeOffset(DateTime.Now.Add(expireIn))
                };
                result = await method();

                if (result != null)
                {
                    CacheExtensions.Set(this._cache, key, result, cacheItemPolicy);
                }
            }
            finally
            {
                keyLock.Release();
            }

            return((T)(result ?? default(T)));
        }
 public void Set <T>(string key, T value)
 {
     dummyVar0 = CacheExtensions.Set <T>(this._cache, key, value);
     return;
 }