12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- using System.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Bowin.Common.Utility;
- namespace Bowin.Common.Cache
- {
- public static class CacheHelper
- {
- public static void UseCache<T>(this IServiceCollection service) where T : class, ICacheProvider, new()
- {
- service.AddSingleton<ICacheProvider, T>();
- CacheProvider.Config<T>(service);
- }
- public static object Add(string key, object entry)
- {
- ICacheProvider cacheProvider = HttpHelper.GetService<ICacheProvider>();
- if (cacheProvider == null)
- {
- throw new Exception("请在Startup先指定缓存类型。");
- }
- return cacheProvider.Add(key, entry);
- }
- public static object Add(string key, object entry, DateTime utcExpiry)
- {
- ICacheProvider cacheProvider = HttpHelper.GetService<ICacheProvider>();
- if (cacheProvider == null)
- {
- throw new Exception("请在Startup先指定缓存类型。");
- }
- return cacheProvider.Add(key, entry, utcExpiry);
- }
- public static object Get(string key)
- {
- ICacheProvider cacheProvider = HttpHelper.GetService<ICacheProvider>();
- if (cacheProvider == null)
- {
- throw new Exception("请在Startup先指定缓存类型。");
- }
- return cacheProvider.Get(key);
- }
- public static void Remove(string key)
- {
- ICacheProvider cacheProvider = HttpHelper.GetService<ICacheProvider>();
- if (cacheProvider == null)
- {
- throw new Exception("请在Startup先指定缓存类型。");
- }
- cacheProvider.Remove(key);
- }
- public static void Set(string key, object entry)
- {
- ICacheProvider cacheProvider = HttpHelper.GetService<ICacheProvider>();
- if (cacheProvider == null)
- {
- throw new Exception("请在Startup先指定缓存类型。");
- }
- cacheProvider.Set(key, entry);
- }
- public static void Set(string key, object entry, DateTime utcExpiry)
- {
- ICacheProvider cacheProvider = HttpHelper.GetService<ICacheProvider>();
- if (cacheProvider == null)
- {
- throw new Exception("请在Startup先指定缓存类型。");
- }
- cacheProvider.Set(key, entry, utcExpiry);
- }
- }
- }
|