public async Task <Block> GetBlockAsync(uint256 blockHash, CancellationToken cancel)
        {
            string cacheKey     = $"{nameof(SmartBlockProvider)}:{nameof(GetBlockAsync)}:{blockHash}";
            var    cacheOptions = new MemoryCacheEntryOptions
            {
                Size = 10,
                SlidingExpiration = TimeSpan.FromSeconds(4)
            };

            return(await Cache.AtomicGetOrCreateAsync(
                       cacheKey,
                       cacheOptions,
                       () => InnerBlockProvider.GetBlockAsync(blockHash, cancel)).ConfigureAwait(false));
        }
示例#2
0
        public Task <Block> GetBlockAsync(uint256 blockHash, CancellationToken cancel)
        {
            lock (Lock)
            {
                string cacheKey = $"{nameof(SmartBlockProvider):nameof(GetBlockAsync)}:{blockHash}";
                if (!Cache.TryGetValue(cacheKey, out Task <Block> getBlockTask))
                {
                    getBlockTask = InnerBlockProvider.GetBlockAsync(blockHash, cancel);

                    var cacheEntryOptions = new MemoryCacheEntryOptions()
                                            .SetSize(10)
                                            .SetSlidingExpiration(TimeSpan.FromSeconds(4))
                                            .RegisterPostEvictionCallback(callback: EvictionCallback, state: this);

                    // Save data in cache.
                    Cache.Set(cacheKey, getBlockTask, cacheEntryOptions);
                }
                return(getBlockTask);
            }
        }