DefaultInMemoryCachingProvider

We found 10 examples in language CSharp for this search. You will see 36 fragments of code.
        [Fact]
        public void AddDefaultInMemoryCache_Should_Get_InMemoryCachingProvider()
        {
            IServiceCollection services = new ServiceCollection();
            services.AddDefaultInMemoryCache();

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            var cachingProvider = serviceProvider.GetService<IEasyCachingProvider>();

            Assert.IsType<DefaultInMemoryCachingProvider>(cachingProvider);
        }


		[Benchmark(Baseline = true)]
		public async Task<string> CacheTower_MemoryCacheLayer()
		{
			return await CacheTower.GetOrSetAsync<string>("GetOrSet_TestKey", (old) =>
			{
				return Task.FromResult("Hello World");
			}, new CacheSettings(TimeSpan.FromDays(1), TimeSpan.FromDays(1)));
		}

		[Benchmark]
		public string CacheManager_MicrosoftMemoryCache()
		{
			return CacheManager.GetOrAdd("GetOrSet_TestKey", (key) =>
			{
				return new CacheItem<string>(key, "Hello World");
			}).Value;
		}

		[Benchmark]
		public string EasyCaching_InMemory()
		{
			return EasyCaching.Get("GetOrSet_TestKey", () => "Hello World", TimeSpan.FromDays(1)).Value;
		}

		[Benchmark]
		public string LazyCache_MemoryProvider()
		{
			return LazyCache.GetOrAdd("GetOrSet_TestKey", () => "Hello World", TimeSpan.FromDays(1));
		}

		[Benchmark]
		public string FusionCache_MemoryProvider()
		{
			return FusionCache.GetOrSet("GetOrSet_TestKey", (cancellationToken) => "Hello World", TimeSpan.FromDays(1));
		}

		[Benchmark]
		public string IntelligentCache_MemoryCache()
		{
			return IntelligentCache.GetSet("GetOrSet_TestKey", () => "Hello World", TimeSpan.FromDays(1));
		}

        /// <summary>
        /// 使用内存缓存
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="providerName"></param>
        /// <returns></returns>
        public static ICachingBuilder UseInMemory(this ICachingBuilder builder, string providerName = "default")
        {
            builder.Services.AddSingleton<IInMemoryCaching>(sp =>
            {
                return new InMemoryCaching(providerName);
            });
            builder.Services.AddSingleton<ICachingProvider>(sp =>
            {
                return new DefaultInMemoryCachingProvider(providerName, sp.GetServices<IInMemoryCaching>());
            });
            return builder;
        }


        public bool Exists(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));

            return _cache.Exists(key);
        }

        public Task<bool> ExistsAsync(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));

            return Task.FromResult(_cache.Exists(key));
        }

        public T Get<T>(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));

            return _cache.Get<T>(key);
        }

        public IDictionary<string, T> GetAll<T>(IEnumerable<string> keys)
        {
            if (keys == null || keys.Count() == 0)
                throw new ArgumentNullException(nameof(keys));

            return _cache.GetAll<T>(keys);
        }

        public Task<IDictionary<string, T>> GetAllAsync<T>(IEnumerable<string> keys)
        {
            if (keys == null || keys.Count() == 0)
                throw new ArgumentNullException(nameof(keys));

            return Task.FromResult(_cache.GetAll<T>(keys));
        }

        public Task<T> GetAsync<T>(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));

            return Task.FromResult(_cache.Get<T>(key));
        }

        #region 内存测试
        public IActionResult Memory()
        {
            DefaultInMemoryCachingProvider cache = (DefaultInMemoryCachingProvider)_memoryProvider;
            return Json(cache.Keys);
        }
        public IActionResult MemoryGet(string key)
        {
            var value = _memoryProvider.Get<string>(key);

            return Json(value);
        }
        public IActionResult MemoryAdd(string key, string value)
        {
            _memoryProvider.Set(key, value, TimeSpan.FromSeconds(60));
            return Content("Success");
        }
        public IActionResult MemoryUpdate(string key, string value)
        {
            _memoryProvider.Set(key, value, TimeSpan.FromSeconds(60));
            return Content("Success");
        }
        public IActionResult MemoryDelete(string key)
        {
            _memoryProvider.Remove(key);
            return Content("Success");
        }
        #endregion
        #region Redis测试

        public IActionResult RedisGet(string key)
        {
            var value = _redisProvider.Get<string>(key);

            return Json(value);
        }

        /// <summary>
        /// Uses the in memory.
        /// </summary>
        /// <returns>The in memory.</returns>
        /// <param name="options">Options.</param>
        /// <param name="configuration">Configuration.</param>
        /// <param name="name">Name.</param>
        /// <param name="sectionName">SectionName.</param>
        public static IServiceCollection AddMemoryServices(this IServiceCollection services, IConfiguration configuration, string name = CachingConstValue.DefaultInMemoryName, string sectionName = CachingConstValue.InMemorySection)
        {
            var dbConfig = configuration.GetSection(sectionName);
            var memoryOptions = new InMemoryOptions();
            dbConfig.Bind(memoryOptions);

            //void configure(InMemoryOptions x)
            //{
            //    x.CachingProviderType = memoryOptions.CachingProviderType;
            //    x.EnableLogging = memoryOptions.EnableLogging;
            //    x.MaxRdSecond = memoryOptions.MaxRdSecond;
            //    x.Order = memoryOptions.Order;
            //    x.DBConfig = memoryOptions.DBConfig;
            //}
            return AddMemoryServices(services,x=> {
                x.CachingProviderType = memoryOptions.CachingProviderType;
                x.EnableLogging = memoryOptions.EnableLogging;
                x.MaxRdSecond = memoryOptions.MaxRdSecond;
                x.Order = memoryOptions.Order;
                x.DBConfig = memoryOptions.DBConfig;
            }, name);
        }
        /// <summary>
        /// Adds the services.
        /// </summary>
        /// <param name="services">Services.</param>
        public static IServiceCollection AddMemoryServices(this IServiceCollection services, Action<InMemoryOptions> configure, string name = CachingConstValue.DefaultInMemoryName)
        {
            services.AddOptions();
            services.Configure(name, configure);
            services.AddMemoryCache();
            services.AddSingleton<IInMemoryCaching, InMemoryCaching>(x =>
            {
                var optionsMon = x.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<InMemoryOptions>>();
                var options = optionsMon.Get(name);
                IMemoryCache memoryCache=x.GetRequiredService<IMemoryCache>();
                return new InMemoryCaching(name, options.DBConfig,memoryCache);
            });
            services.TryAddSingleton<ICachingProviderFactory, DefaultCachingProviderFactory>();
            services.AddSingleton<ICachingProvider, DefaultInMemoryCachingProvider>(x =>
            {
                var mCache = x.GetServices<IInMemoryCaching>();
                var optionsMon = x.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<InMemoryOptions>>();
                var options = optionsMon.Get(name);
                //ILoggerFactory can be null
                var factory = x.GetService<Microsoft.Extensions.Logging.ILoggerFactory>();
                return new DefaultInMemoryCachingProvider(name, mCache, options, factory);
            });
            return services;

        }

        /// <summary>
        /// 使用内存缓存
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="providerName"></param>
        /// <returns></returns>
        public static ICachingBuilder UseInMemory(this ICachingBuilder builder, string providerName = "default")
        {
            builder.Services.AddSingleton<IInMemoryCaching>(sp =>
            {
                return new InMemoryCaching(providerName);
            });
            builder.Services.AddSingleton<ICachingProvider>(sp =>
            {
                return new DefaultInMemoryCachingProvider(providerName, sp.GetServices<IInMemoryCaching>());
            });
            return builder;
        }


		[Benchmark(Baseline = true)]
		public void CacheTower_MemoryCacheLayer()
		{
			Parallel.For(0, ParallelIterations, async i =>
			{
				await CacheTower.GetOrSetAsync<string>("GetOrSet_TestKey", (old) =>
				{
					return Task.FromResult("Hello World");
				}, new CacheSettings(TimeSpan.FromDays(1), TimeSpan.FromDays(1)));
			});
		}

		[Benchmark]
		public void CacheManager_MicrosoftMemoryCache()
		{
			Parallel.For(0, ParallelIterations, i =>
			{
				var _ = CacheManager.GetOrAdd("GetOrSet_TestKey", (key) =>
				{
					return new CacheItem<string>(key, "Hello World");
				}).Value;
			});
		}

		[Benchmark]
		public void EasyCaching_InMemory()
		{
			Parallel.For(0, ParallelIterations, i =>
			{
				_ = EasyCaching.Get("GetOrSet_TestKey", () => "Hello World", TimeSpan.FromDays(1)).Value;
			});
		}

		[Benchmark]
		public void LazyCache_MemoryProvider()
		{
			Parallel.For(0, ParallelIterations, i =>
			{
				LazyCache.GetOrAdd("GetOrSet_TestKey", () => "Hello World", TimeSpan.FromDays(1));
			});
		}

		[Benchmark]
		public void FusionCache_MemoryProvider()
		{
			Parallel.For(0, ParallelIterations, i =>
			{
				FusionCache.GetOrSet("GetOrSet_TestKey", (cancellationToken) => "Hello World", TimeSpan.FromDays(1));
			});
		}

		[Benchmark]
		public void IntelligentCache_MemoryCache()
		{
			Parallel.For(0, ParallelIterations, i =>
			{
				IntelligentCache.GetSet("GetOrSet_TestKey", () => "Hello World", TimeSpan.FromDays(1));
			});
		}


        public bool Exists(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));

            return _cache.Exists(key);
        }

        public Task<bool> ExistsAsync(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));

            return Task.FromResult(_cache.Exists(key));
        }

        public T Get<T>(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));

            return _cache.Get<T>(key);
        }

        public IDictionary<string, T> GetAll<T>(IEnumerable<string> keys)
        {
            if (keys == null || keys.Count() == 0)
                throw new ArgumentNullException(nameof(keys));

            return _cache.GetAll<T>(keys);
        }

        public Task<IDictionary<string, T>> GetAllAsync<T>(IEnumerable<string> keys)
        {
            if (keys == null || keys.Count() == 0)
                throw new ArgumentNullException(nameof(keys));

            return Task.FromResult(_cache.GetAll<T>(keys));
        }

        public Task<T> GetAsync<T>(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));

            return Task.FromResult(_cache.Get<T>(key));
        }


        /// <summary>
        /// Adds the services.
        /// </summary>
        /// <param name="services">Services.</param>
        public void AddServices(IServiceCollection services)
        {
            services.AddOptions();
            services.Configure(_name, configure);
            services.AddSingleton<IInMemoryCaching, InMemoryCaching>(x =>
            {
                var optionsMon = x.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<InMemoryOptions>>();
                var options = optionsMon.Get(_name);
                return new InMemoryCaching(_name, options.DBConfig);
            });

            services.TryAddSingleton<IEasyCachingProviderFactory, DefaultEasyCachingProviderFactory>();
            services.AddSingleton<IEasyCachingProvider, DefaultInMemoryCachingProvider>(x =>
            {
                var mCache = x.GetServices<IInMemoryCaching>();
                var optionsMon = x.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<InMemoryOptions>>();
                var options = optionsMon.Get(_name);
                var dlf = x.GetService<IDistributedLockFactory>();
                // ILoggerFactory can be null
                var factory = x.GetService<Microsoft.Extensions.Logging.ILoggerFactory>();
                return new DefaultInMemoryCachingProvider(_name, mCache, options, dlf, factory);
            });
        }

EasyCaching.InMemory.DefaultInMemoryCachingProvider : IEasyCachingProvider, IEasyCachingProviderBase

Constructors :

public DefaultInMemoryCachingProvider(String name = , IEnumerable<IInMemoryCaching> cache = , InMemoryOptions options = , ILoggerFactory loggerFactory = null)
public DefaultInMemoryCachingProvider(String name = , IEnumerable<IInMemoryCaching> cache = , InMemoryOptions options = , IDistributedLockFactory factory = null, ILoggerFactory loggerFactory = null)

Methods :

public Task<CacheValue<T>> BaseGetAsync(String cacheKey = , Func<Task<T>> dataRetriever = , TimeSpan expiration = )
public Task<CacheValue<T>> BaseGetAsync(String cacheKey = )
public Task<Int32> BaseGetCountAsync(String prefix = )
public Task<Object> BaseGetAsync(String cacheKey = , Type type = )
public Task BaseRemoveAsync(String cacheKey = )
public Task BaseSetAsync(String cacheKey = , T cacheValue = , TimeSpan expiration = )
public Task<Boolean> BaseExistsAsync(String cacheKey = )
public Task BaseRemoveByPrefixAsync(String prefix = )
public Task BaseSetAllAsync(IDictionary<String, T> values = , TimeSpan expiration = )
public Task<IDictionary<String, CacheValue<T>>> BaseGetAllAsync(IEnumerable<String> cacheKeys = )
public Task<IDictionary<String, CacheValue<T>>> BaseGetByPrefixAsync(String prefix = )
public Task BaseRemoveAllAsync(IEnumerable<String> cacheKeys = )
public Task BaseFlushAsync()
public Task<Boolean> BaseTrySetAsync(String cacheKey = , T cacheValue = , TimeSpan expiration = )
public Task<TimeSpan> BaseGetExpirationAsync(String cacheKey = )
public CacheValue<T> BaseGet(String cacheKey = , Func<T> dataRetriever = , TimeSpan expiration = )
public CacheValue<T> BaseGet(String cacheKey = )
public Void BaseRemove(String cacheKey = )
public Void BaseSet(String cacheKey = , T cacheValue = , TimeSpan expiration = )
public Boolean BaseExists(String cacheKey = )
public Void BaseRemoveByPrefix(String prefix = )
public Void BaseSetAll(IDictionary<String, T> values = , TimeSpan expiration = )
public IDictionary<String, CacheValue<T>> BaseGetAll(IEnumerable<String> cacheKeys = )
public IDictionary<String, CacheValue<T>> BaseGetByPrefix(String prefix = )
public Void BaseRemoveAll(IEnumerable<String> cacheKeys = )
public Int32 BaseGetCount(String prefix = )
public Void BaseFlush()
public Boolean BaseTrySet(String cacheKey = , T cacheValue = , TimeSpan expiration = )
public TimeSpan BaseGetExpiration(String cacheKey = )
public ProviderInfo BaseGetProviderInfo()
public Object BaseGetDatabse()
public String get_Name()
public Boolean get_IsDistributedCache()
public Boolean get_UseLock()
public Int32 get_MaxRdSecond()
public CachingProviderType get_CachingProviderType()
public CacheStats get_CacheStats()
public Object get_Database()
public Boolean Exists(String cacheKey = )
public Task<Boolean> ExistsAsync(String cacheKey = )
public Void Flush()
public Task FlushAsync()
public CacheValue<T> Get(String cacheKey = , Func<T> dataRetriever = , TimeSpan expiration = )
public CacheValue<T> Get(String cacheKey = )
public IDictionary<String, CacheValue<T>> GetAll(IEnumerable<String> cacheKeys = )
public Task<IDictionary<String, CacheValue<T>>> GetAllAsync(IEnumerable<String> cacheKeys = )
public Task<CacheValue<T>> GetAsync(String cacheKey = , Func<Task<T>> dataRetriever = , TimeSpan expiration = )
public Task<Object> GetAsync(String cacheKey = , Type type = )
public Task<CacheValue<T>> GetAsync(String cacheKey = )
public IDictionary<String, CacheValue<T>> GetByPrefix(String prefix = )
public Task<IDictionary<String, CacheValue<T>>> GetByPrefixAsync(String prefix = )
public Int32 GetCount(String prefix = )
public Task<Int32> GetCountAsync(String prefix = )
public Void Remove(String cacheKey = )
public Void RemoveAll(IEnumerable<String> cacheKeys = )
public Task RemoveAllAsync(IEnumerable<String> cacheKeys = )
public Task RemoveAsync(String cacheKey = )
public Void RemoveByPrefix(String prefix = )
public Task RemoveByPrefixAsync(String prefix = )
public Void Set(String cacheKey = , T cacheValue = , TimeSpan expiration = )
public Void SetAll(IDictionary<String, T> value = , TimeSpan expiration = )
public Task SetAllAsync(IDictionary<String, T> value = , TimeSpan expiration = )
public Task SetAsync(String cacheKey = , T cacheValue = , TimeSpan expiration = )
public Boolean TrySet(String cacheKey = , T cacheValue = , TimeSpan expiration = )
public Task<Boolean> TrySetAsync(String cacheKey = , T cacheValue = , TimeSpan expiration = )
public TimeSpan GetExpiration(String cacheKey = )
public Task<TimeSpan> GetExpirationAsync(String cacheKey = )
public ProviderInfo GetProviderInfo()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()