通过数据库名称字符串 反射获取数据并刷新对应缓存
///ClassName是数据库名称字符串 public JsonResult ReloadCache(string ClassName) { try { if (!string.IsNullOrWhiteSpace(ClassName)) { //ClassName = "ECSUPPLYMODELS.USER,ECSUPPLYMODELS"; var modelName = ClassName; ClassName = "ECSUPPLYMODELS."+ ClassName +",ECSUPPLYMODELS"; var bllName = "ECSUPPLYBLL.BaseBLL`1,ECSUPPLYBLL"; var cacheName = "ECSUPPLYCOMMON.EcsupplyRedisCache,ECSUPPLYCOMMON"; #region 反射bll 获取数据 (要获取的是泛型类中的泛型方法,无参数有返回值IList<T>) Type bll = Type.GetType(bllName); //获取bll类型 bll = bll.MakeGenericType(Type.GetType(ClassName)); //指定泛型类(或泛型方法)的具体类型 object bllObject = Activator.CreateInstance(bll); //反射生成实例 MethodInfo bllMethod = bll.GetMethod("GetListAll"); //指定要调用的方法“GetListAll” var list = bllMethod.Invoke(bllObject, null); //执行 ps:参数1是反射得到的实例 参数2是要调用的方法的参数 #endregion #region 反射 加载缓存 (要获取的方法有参数string key, IList<T> dataList /无返回值) Type cache = Type.GetType(cacheName); //获取类型 object cacheObject = Activator.CreateInstance(cache); //反射生成实例 MethodInfo cacheMethod = cache.GetMethod("SetListRedisCache"); //指定方法“SetListRedisCache” cacheMethod = cacheMethod.MakeGenericMethod(new Type[] { Type.GetType(ClassName)});//指定泛型方法的具体类型 cacheMethod.Invoke(cacheObject, new object[] { modelName, list }); //执行 ps:参数1是反射得到的实例 参数2是要调用方法的参数数组 #endregion return Json(new { flag = true, info = "1" }, JsonRequestBehavior.AllowGet); } return Json(new { flag = false,info = "0" }, JsonRequestBehavior.AllowGet); } catch (Exception e) { return Json(new { flag = false, info = e.Message }, JsonRequestBehavior.AllowGet); } }
BLL内容
namespace ECSUPPLYBLL { public class BaseBLL<T> : IBaseBLL<T> where T : class, new() { /// <summary> /// 获取列表 /// </summary> /// <returns>列表</returns> public IList<T> GetListAll() { using (Entities dbContext = new Entities()) { IList<T> list = dbContext.Set<T>().ToList(); return list; } } } }
COMMON内代码
public class EcsupplyRedisCache { private static ConfigurationOptions _configurationOptions = ConfigurationOptions.Parse("127.0.0.1" + ":" + "6379,password=!~@#wdfghOURVCsupp7642m*^$"); private static ConnectionMultiplexer _redis; private static readonly Object locker = new Object(); private static ConnectionMultiplexer GetConnectRedis() { try { _redis = null; _configurationOptions.AbortOnConnectFail = false; _configurationOptions.ConnectTimeout = 1000; _redis = ConnectionMultiplexer.Connect(_configurationOptions); } catch (Exception ex) { LogHelper.WriteLog("获取redis实例时出错,详细错误信息:InnerException:" + ex.InnerException + "------Message:" + ex.Message + "-------StackTrace:" + ex.StackTrace); } return _redis; } private static ConnectionMultiplexer RedisInstance { get { lock (locker) { if (_redis == null) { _redis = GetConnectRedis(); } return _redis; } } } public static void SetListRedisCache<T>(string key, IList<T> dataList) where T : class, new() { try { if (RedisInstance.IsConnected) { lock (locker) { IDatabase db = RedisInstance.GetDatabase(); db.StringSet(key, JsonHelper.ObjectToJSON(dataList)); } } } catch(Exception ex) { LogHelper.WriteLog("初始化缓存信息时出错,详细错误信息:InnerException:" + ex.InnerException + "------Message:" + ex.Message + "-------StackTrace:" + ex.StackTrace); } } }