public IActionResult GetCacheProviderType(CacheProviderType cacheProviderType) { switch (cacheProviderType) { case CacheProviderType.InMemory: var providerInMemory = _factory.GetCacheProvider("inmemory"); return(Ok(providerInMemory.CacheProviderType.ToString())); case CacheProviderType.Redis: var providerRedis = _factory.GetCacheProvider("redis"); return(Ok(providerRedis.CacheProviderType.ToString())); case CacheProviderType.Memcached: var providerMemcached = _factory.GetCacheProvider("memcached"); return(Ok(providerMemcached.CacheProviderType.ToString())); case CacheProviderType.SQLite: var providerSQLite = _factory.GetCacheProvider("sqlite"); return(Ok(providerSQLite.CacheProviderType.ToString())); case CacheProviderType.Other: var providerOther = _factory.GetCacheProvider("other"); return(Ok(providerOther.CacheProviderType.ToString())); default: return(BadRequest("Provider not supported")); } }
public ICacheRepository Create(CacheProviderType type) { switch (type) { case CacheProviderType.RedisCache: return(new RedisCacheRepository()); case CacheProviderType.MemoryCache: return(new MicrosoftMemoryCacheRepository()); default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } }
/// <summary> /// 创建缓存实例 /// </summary> /// <param name="providerType">缓存类型</param> /// <returns></returns> public static ICacheProvider GetCacheInstance(CacheProviderType providerType) { ICacheProvider cacheProvider = new MemoryCacheProvider(); switch (providerType) { case CacheProviderType.LOCALMEMORYCACHE: cacheProvider = new MemoryCacheProvider(); break; case CacheProviderType.ServiceStackREDIS: cacheProvider = new MemoryCacheProvider(); break; } return(cacheProvider); }
public ICacheProvider CreateCacheProvider(CacheProviderType cacheProviderType) { ICacheProvider cacheProvider; switch (cacheProviderType) { case CacheProviderType.Simple: { cacheProvider = new SimpleCacheProvider(); break; } default: { cacheProvider = new SimpleCacheProvider(); break; } } return(cacheProvider); }
public async Task <IActionResult> ClearCachedItems(CacheProviderType cacheProviderType) { switch (cacheProviderType) { case CacheProviderType.InMemory: var providerInMemory = _factory.GetCacheProvider("inmemory"); await providerInMemory.ClearAsync(); break; case CacheProviderType.Redis: var providerRedis = _factory.GetCacheProvider("redis"); await providerRedis.ClearAsync(); break; case CacheProviderType.Memcached: var providerMemcached = _factory.GetCacheProvider("memcached"); await providerMemcached.ClearAsync(); break; case CacheProviderType.SQLite: var providerSQLite = _factory.GetCacheProvider("sqlite"); await providerSQLite.ClearAsync(); break; case CacheProviderType.Other: var providerOther = _factory.GetCacheProvider("other"); await providerOther.ClearAsync(); break; default: return(BadRequest("Provider not supported")); } return(Ok()); }
/// <summary> /// 拦截方法执行完成后 /// </summary> /// <param name="eventArgs"></param> public override void OnSuccess(MethodExecutionArgs eventArgs) { // 获取key string[] keys = (string[])eventArgs.MethodExecutionTag; if (keys == null || keys.Length != 2) { return; } string key = keys[0]; string valKey = keys[1]; var methodReturn = eventArgs.ReturnValue; ICacheProvider cacheProvider = EngineContext.Current.ServiceLocator.GetInstance <ICacheProvider>(CacheProviderType.ToString()); switch (Method) { case CachingMethod.Get: AddCache(cacheProvider, key, valKey, methodReturn); break; case CachingMethod.Put: if (cacheProvider.Exists(key)) { if (Force) { cacheProvider.Remove(key); AddCache(cacheProvider, key, valKey, methodReturn); } else { PutCache(cacheProvider, key, valKey, methodReturn); } } else { AddCache(cacheProvider, key, valKey, methodReturn); } break; case CachingMethod.Remove: var removeKeys = CorrespondingMethodNames; string rk = string.Empty; foreach (var removeKey in removeKeys) { rk = eventArgs.Method.DeclaringType == null ? removeKey : eventArgs.Method.DeclaringType + "." + removeKey; if (cacheProvider.Exists(rk)) { cacheProvider.Remove(rk); } } break; default: break; } }
/// <summary> /// 进入方法时 /// </summary> /// <param name="eventArgs"></param> public override void OnEntry(MethodExecutionArgs eventArgs) { string key = eventArgs.Method.DeclaringType == null ? eventArgs.Method.Name : eventArgs.Method.DeclaringType + "." + eventArgs.Method.Name; string valKey = GetValueKey(eventArgs); ICacheProvider cacheProvider = EngineContext.Current.ServiceLocator.GetInstance <ICacheProvider>(CacheProviderType.ToString()); switch (Method) { case CachingMethod.Get: if (cacheProvider.Exists(key, valKey)) { var value = cacheProvider.Get(key, valKey); eventArgs.ReturnValue = value; eventArgs.FlowBehavior = FlowBehavior.Return; } else { eventArgs.MethodExecutionTag = new[] { key, valKey }; } break; case CachingMethod.Put: eventArgs.MethodExecutionTag = new[] { key, valKey }; break; case CachingMethod.Remove: var removeKeys = CorrespondingMethodNames; string rk = string.Empty; foreach (var removeKey in removeKeys) { rk = eventArgs.Method.DeclaringType == null ? removeKey : eventArgs.Method.DeclaringType + "." + removeKey; if (cacheProvider.Exists(rk)) { cacheProvider.Remove(rk); } } break; default: break; } }
public async Task <IActionResult> RemoveCachedCustomers(CacheProviderType cacheProviderType) { //cacheable key var cacheKey = "customers"; // OR (may not work in web-farm scenario for some providers) var cacheKeyPattern = "customers-"; switch (cacheProviderType) { case CacheProviderType.InMemory: var providerInMemory = _factory.GetCacheProvider("inmemory"); await providerInMemory.RemoveAsync(cacheKey); // remove by pattern await providerInMemory.RemoveByPatternAsync(cacheKeyPattern); break; case CacheProviderType.Redis: var providerRedis = _factory.GetCacheProvider("redis"); await providerRedis.RemoveAsync(cacheKey); // remove by pattern await providerRedis.RemoveByPatternAsync(cacheKeyPattern); break; case CacheProviderType.Memcached: var providerMemcached = _factory.GetCacheProvider("memcached"); await providerMemcached.RemoveAsync(cacheKey); // remove by pattern await providerMemcached.RemoveByPatternAsync(cacheKeyPattern); break; case CacheProviderType.SQLite: var providerSQLite = _factory.GetCacheProvider("sqlite"); await providerSQLite.RemoveAsync(cacheKey); // remove by pattern await providerSQLite.RemoveByPatternAsync(cacheKeyPattern); break; case CacheProviderType.Other: var providerOther = _factory.GetCacheProvider("other"); await providerOther.RemoveAsync(cacheKey); // remove by pattern await providerOther.RemoveByPatternAsync(cacheKeyPattern); break; default: return(BadRequest("Provider not supported")); } return(Ok()); }
public async Task <IActionResult> CacheCustomer(int customerId, CacheProviderType cacheProviderType) { Customer customer = null; var cacheKey = $"customer-{customerId}"; switch (cacheProviderType) { case CacheProviderType.InMemory: var providerInMemory = _factory.GetCacheProvider("inmemory"); customer = await providerInMemory.GetAsync <Customer>(cacheKey); if (customer == default(Customer)) { //no value in the cache yet //let's load customer and cache the result customer = GetCustomerById(customerId); await providerInMemory.SetAsync(cacheKey, customer, 60); } break; case CacheProviderType.Redis: var providerRedis = _factory.GetCacheProvider("redis"); customer = await providerRedis.GetAsync <Customer>(cacheKey); if (customer == default(Customer)) { //no value in the cache yet //let's load customer and cache the result customer = GetCustomerById(customerId); await providerRedis.SetAsync(cacheKey, customer, 60); } break; case CacheProviderType.Memcached: var providerMemcached = _factory.GetCacheProvider("memcached"); customer = await providerMemcached.GetAsync <Customer>(cacheKey); if (customer == default(Customer)) { //no value in the cache yet //let's load customer and cache the result customer = GetCustomerById(customerId); await providerMemcached.SetAsync(cacheKey, customer, 60); } break; case CacheProviderType.SQLite: var providerSQLite = _factory.GetCacheProvider("sqlite"); customer = await providerSQLite.GetAsync <Customer>(cacheKey); if (customer == default(Customer)) { //no value in the cache yet //let's load customer and cache the result customer = GetCustomerById(customerId); await providerSQLite.SetAsync(cacheKey, customer, 60); } break; case CacheProviderType.Other: var providerOther = _factory.GetCacheProvider("other"); customer = await providerOther.GetAsync <Customer>(cacheKey); if (customer == default(Customer)) { //no value in the cache yet //let's load customer and cache the result customer = GetCustomerById(customerId); await providerOther.SetAsync(cacheKey, customer, 60); } break; default: return(BadRequest("Provider not supported")); } return(Ok(customer)); }
public async Task <IActionResult> CacheCustomers(int cacheTime, CacheProviderType cacheProviderType) { IList <Customer> customers; //cacheable key var cacheKey = "customers"; switch (cacheProviderType) { case CacheProviderType.InMemory: var providerInMemory = _factory.GetCacheProvider("inmemory"); customers = await providerInMemory.GetAsync(cacheKey, cacheTime, () => { var result = new List <Customer>(); result = GetCustomers().ToList(); return(result); }); break; case CacheProviderType.Redis: var providerRedis = _factory.GetCacheProvider("redis"); customers = await providerRedis.GetAsync(cacheKey, cacheTime, () => { var result = new List <Customer>(); result = GetCustomers().ToList(); return(result); }); break; case CacheProviderType.Memcached: var providerMemcached = _factory.GetCacheProvider("memcached"); customers = await providerMemcached.GetAsync(cacheKey, cacheTime, () => { var result = new List <Customer>(); result = GetCustomers().ToList(); return(result); }); break; case CacheProviderType.SQLite: var providerSQLite = _factory.GetCacheProvider("sqlite"); customers = await providerSQLite.GetAsync(cacheKey, cacheTime, () => { var result = new List <Customer>(); result = GetCustomers().ToList(); return(result); }); break; case CacheProviderType.Other: var providerOther = _factory.GetCacheProvider("other"); customers = await providerOther.GetAsync(cacheKey, cacheTime, () => { var result = new List <Customer>(); result = GetCustomers().ToList(); return(result); }); break; default: return(BadRequest("Provider not supported")); } return(Ok(customers)); }