Common.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using WMS.Util;
  6. using WMS.Core;
  7. using WMS.Info;
  8. using FreeRedis;
  9. using Newtonsoft.Json;
  10. using System.Reflection;
  11. using Autofac;
  12. using WMS.BZSqlSugar;
  13. namespace WMS.BZWeb
  14. {
  15. public static class Common
  16. {
  17. public static string GetBodyUITheme()
  18. {
  19. return SessionCookieCore.GetBodyUITheme();
  20. }
  21. public static string GetMainUITheme()
  22. {
  23. return SessionCookieCore.GetMainUITheme();
  24. }
  25. public static string GetLoginUITheme()
  26. {
  27. return SessionCookieCore.GetLoginUITheme();
  28. }
  29. public static string GetClientBrowser()
  30. {
  31. return "";//NetUtil.Browser;
  32. }
  33. public static string GetWMSTile()
  34. {
  35. return SysSetCore.GetSysSet().WMSTile;
  36. }
  37. public static string GetWMSVersion()
  38. {
  39. return SysSetCore.GetSysSet().WMSVersion;
  40. }
  41. public static LoginUserInfo GetLoginUserInfo()
  42. {
  43. return LoginBLLCore.GetLoginUser();
  44. }
  45. /// <summary>
  46. /// 当前日期
  47. /// </summary>
  48. /// <returns></returns>
  49. public static string GetToday()
  50. {
  51. return DateTime.Now.ToString("yyyy-MM-dd");
  52. }
  53. }
  54. /// <summary>
  55. /// FreeRedis配置选项
  56. /// </summary>
  57. public class FreeRedisOption
  58. {
  59. /// <summary>
  60. /// Host
  61. /// </summary>
  62. public string RedisHost { get; set; }
  63. /// <summary>
  64. /// Port
  65. /// </summary>
  66. public int RedisPort { get; set; } = 6379;
  67. /// <summary>
  68. /// 密码
  69. /// </summary>
  70. public string RedisPassword { get; set; }
  71. /// <summary>
  72. /// 同步超时
  73. /// </summary>
  74. public int SyncTimeout { get; set; } = 5000;
  75. /// <summary>
  76. /// 连接超时
  77. /// </summary>
  78. public int ConnectTimeout { get; set; } = 15000;
  79. /// <summary>
  80. /// Key前缀
  81. /// </summary>
  82. public string Prefix { get; set; }
  83. /// <summary>
  84. /// 默认访问第几个
  85. /// </summary>
  86. public int DefaultIndex { get; set; } = 0;
  87. /// <summary>
  88. /// 池大小
  89. /// </summary>
  90. public int Poolsize { get; set; } = 5;
  91. /// <summary>
  92. /// 异步管道
  93. /// </summary>
  94. public bool asyncPipeline { get; set; } = true;
  95. /// <summary>
  96. /// 是否启用客户端缓存(6.0及以上支持)
  97. /// </summary>
  98. public bool UseClientSideCache { get; set; }
  99. /// <summary>
  100. /// 客户端缓存Key筛选条件
  101. /// </summary>
  102. public Func<string, bool> ClientSideCacheKeyFilter { get; set; }
  103. }
  104. public class FreeRedisService
  105. {
  106. /// <summary>
  107. /// RedisClient
  108. /// </summary>
  109. private static RedisClient _redisClient;
  110. /// <summary>
  111. /// 初始化配置
  112. /// </summary>
  113. private FreeRedisOption _redisOption;
  114. /// <summary>
  115. /// 构造函数
  116. /// </summary>
  117. public FreeRedisService(FreeRedisOption redisOption)
  118. {
  119. if (redisOption == null)
  120. {
  121. throw new NullReferenceException("初始化配置为空");
  122. }
  123. _redisOption = redisOption;
  124. InitRedisClient();
  125. }
  126. /// <summary>
  127. /// 懒加载Redis客户端
  128. /// </summary>
  129. private readonly static Lazy<RedisClient> redisClientLazy = new Lazy<RedisClient>(() => {
  130. var r = _redisClient;
  131. r.Serialize = obj => JsonConvert.SerializeObject(obj);
  132. r.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);
  133. r.Notice += (s, e) => Console.WriteLine(e.Log);
  134. return r;
  135. });
  136. private static readonly object obj = new object();
  137. /// <summary>
  138. /// 初始化Redis
  139. /// </summary>
  140. /// <returns></returns>
  141. bool InitRedisClient()
  142. {
  143. if (_redisClient == null)
  144. {
  145. lock (obj)
  146. {
  147. if (_redisClient == null)
  148. {
  149. _redisClient = new RedisClient($"{_redisOption.RedisHost}:{_redisOption.RedisPort},password={_redisOption.RedisPassword},defaultDatabase={_redisOption.DefaultIndex},poolsize={_redisOption.Poolsize},ssl=false,writeBuffer=10240,prefix={_redisOption.Prefix},asyncPipeline={_redisOption.asyncPipeline},connectTimeout={_redisOption.ConnectTimeout},abortConnect=false");
  150. //设置客户端缓存
  151. if (_redisOption.UseClientSideCache)
  152. {
  153. if (_redisOption.ClientSideCacheKeyFilter == null)
  154. {
  155. throw new NullReferenceException("如果开启客户端缓存,必须设置客户端缓存Key过滤条件");
  156. }
  157. _redisClient.UseClientSideCaching(new ClientSideCachingOptions()
  158. {
  159. Capacity = 0, //本地缓存的容量,0不限制
  160. KeyFilter = _redisOption.ClientSideCacheKeyFilter, //过滤哪些键能被本地缓存
  161. CheckExpired = (key, dt) => DateTime.Now.Subtract(dt) > TimeSpan.FromSeconds(3) //检查长期未使用的缓存
  162. });
  163. }
  164. return true;
  165. }
  166. }
  167. }
  168. return _redisClient != null;
  169. }
  170. /// <summary>
  171. /// 获取Client实例
  172. /// </summary>
  173. public RedisClient Instance
  174. {
  175. get
  176. {
  177. if (InitRedisClient())
  178. {
  179. return redisClientLazy.Value;
  180. }
  181. throw new NullReferenceException("Redis不可用");
  182. }
  183. }
  184. }
  185. public class AutoFacManager : Autofac.Module
  186. {
  187. //重写Autofac管道Load方法,在这里注册注入
  188. protected override void Load(ContainerBuilder builder)
  189. {
  190. //程序集注入业务服务
  191. var AppServices = Assembly.Load("WMS.BZServices");
  192. //根据名称约定(服务层的接口和实现均以Service结尾),实现服务接口和服务实现的依赖
  193. builder.RegisterAssemblyTypes(AppServices).Where(t => t.Name.EndsWith("Service")).AsSelf().PropertiesAutowired();
  194. }
  195. }
  196. }