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(this IServiceCollection service) where T : class, ICacheProvider, new() { service.AddSingleton(); CacheProvider.Config(service); } public static object Add(string key, object entry) { ICacheProvider cacheProvider = HttpHelper.GetService(); 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(); if (cacheProvider == null) { throw new Exception("请在Startup先指定缓存类型。"); } return cacheProvider.Add(key, entry, utcExpiry); } public static object Get(string key) { ICacheProvider cacheProvider = HttpHelper.GetService(); if (cacheProvider == null) { throw new Exception("请在Startup先指定缓存类型。"); } return cacheProvider.Get(key); } public static void Remove(string key) { ICacheProvider cacheProvider = HttpHelper.GetService(); if (cacheProvider == null) { throw new Exception("请在Startup先指定缓存类型。"); } cacheProvider.Remove(key); } public static void Set(string key, object entry) { ICacheProvider cacheProvider = HttpHelper.GetService(); 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(); if (cacheProvider == null) { throw new Exception("请在Startup先指定缓存类型。"); } cacheProvider.Set(key, entry, utcExpiry); } } }