using System; using System.Collections.Generic; using System.Text; using System.Web; using WMS.Util; using WMS.Core; using WMS.Info; using FreeRedis; using Newtonsoft.Json; using System.Reflection; using Autofac; using WMS.BZSqlSugar; namespace WMS.BZWeb { public static class Common { public static string GetBodyUITheme() { return SessionCookieCore.GetBodyUITheme(); } public static string GetMainUITheme() { return SessionCookieCore.GetMainUITheme(); } public static string GetLoginUITheme() { return SessionCookieCore.GetLoginUITheme(); } public static string GetClientBrowser() { return "";//NetUtil.Browser; } public static string GetWMSTile() { return SysSetCore.GetSysSet().WMSTile; } public static string GetWMSVersion() { return SysSetCore.GetSysSet().WMSVersion; } public static LoginUserInfo GetLoginUserInfo() { return LoginBLLCore.GetLoginUser(); } /// /// 当前日期 /// /// public static string GetToday() { return DateTime.Now.ToString("yyyy-MM-dd"); } } /// /// FreeRedis配置选项 /// public class FreeRedisOption { /// /// Host /// public string RedisHost { get; set; } /// /// Port /// public int RedisPort { get; set; } = 6379; /// /// 密码 /// public string RedisPassword { get; set; } /// /// 同步超时 /// public int SyncTimeout { get; set; } = 5000; /// /// 连接超时 /// public int ConnectTimeout { get; set; } = 15000; /// /// Key前缀 /// public string Prefix { get; set; } /// /// 默认访问第几个 /// public int DefaultIndex { get; set; } = 0; /// /// 池大小 /// public int Poolsize { get; set; } = 5; /// /// 异步管道 /// public bool asyncPipeline { get; set; } = true; /// /// 是否启用客户端缓存(6.0及以上支持) /// public bool UseClientSideCache { get; set; } /// /// 客户端缓存Key筛选条件 /// public Func ClientSideCacheKeyFilter { get; set; } } public class FreeRedisService { /// /// RedisClient /// private static RedisClient _redisClient; /// /// 初始化配置 /// private FreeRedisOption _redisOption; /// /// 构造函数 /// public FreeRedisService(FreeRedisOption redisOption) { if (redisOption == null) { throw new NullReferenceException("初始化配置为空"); } _redisOption = redisOption; InitRedisClient(); } /// /// 懒加载Redis客户端 /// private readonly static Lazy redisClientLazy = new Lazy(() => { var r = _redisClient; r.Serialize = obj => JsonConvert.SerializeObject(obj); r.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type); r.Notice += (s, e) => Console.WriteLine(e.Log); return r; }); private static readonly object obj = new object(); /// /// 初始化Redis /// /// bool InitRedisClient() { if (_redisClient == null) { lock (obj) { if (_redisClient == null) { _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"); //设置客户端缓存 if (_redisOption.UseClientSideCache) { if (_redisOption.ClientSideCacheKeyFilter == null) { throw new NullReferenceException("如果开启客户端缓存,必须设置客户端缓存Key过滤条件"); } _redisClient.UseClientSideCaching(new ClientSideCachingOptions() { Capacity = 0, //本地缓存的容量,0不限制 KeyFilter = _redisOption.ClientSideCacheKeyFilter, //过滤哪些键能被本地缓存 CheckExpired = (key, dt) => DateTime.Now.Subtract(dt) > TimeSpan.FromSeconds(3) //检查长期未使用的缓存 }); } return true; } } } return _redisClient != null; } /// /// 获取Client实例 /// public RedisClient Instance { get { if (InitRedisClient()) { return redisClientLazy.Value; } throw new NullReferenceException("Redis不可用"); } } } public class AutoFacManager : Autofac.Module { //重写Autofac管道Load方法,在这里注册注入 protected override void Load(ContainerBuilder builder) { //程序集注入业务服务 var AppServices = Assembly.Load("WMS.BZServices"); //根据名称约定(服务层的接口和实现均以Service结尾),实现服务接口和服务实现的依赖 builder.RegisterAssemblyTypes(AppServices).Where(t => t.Name.EndsWith("Service")).AsSelf().PropertiesAutowired(); } } }