123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- 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();
- }
- /// <summary>
- /// 当前日期
- /// </summary>
- /// <returns></returns>
- public static string GetToday()
- {
- return DateTime.Now.ToString("yyyy-MM-dd");
- }
- }
- /// <summary>
- /// FreeRedis配置选项
- /// </summary>
- public class FreeRedisOption
- {
- /// <summary>
- /// Host
- /// </summary>
- public string RedisHost { get; set; }
- /// <summary>
- /// Port
- /// </summary>
- public int RedisPort { get; set; } = 6379;
- /// <summary>
- /// 密码
- /// </summary>
- public string RedisPassword { get; set; }
- /// <summary>
- /// 同步超时
- /// </summary>
- public int SyncTimeout { get; set; } = 5000;
- /// <summary>
- /// 连接超时
- /// </summary>
- public int ConnectTimeout { get; set; } = 15000;
- /// <summary>
- /// Key前缀
- /// </summary>
- public string Prefix { get; set; }
- /// <summary>
- /// 默认访问第几个
- /// </summary>
- public int DefaultIndex { get; set; } = 0;
- /// <summary>
- /// 池大小
- /// </summary>
- public int Poolsize { get; set; } = 5;
- /// <summary>
- /// 异步管道
- /// </summary>
- public bool asyncPipeline { get; set; } = true;
- /// <summary>
- /// 是否启用客户端缓存(6.0及以上支持)
- /// </summary>
- public bool UseClientSideCache { get; set; }
- /// <summary>
- /// 客户端缓存Key筛选条件
- /// </summary>
- public Func<string, bool> ClientSideCacheKeyFilter { get; set; }
- }
- public class FreeRedisService
- {
- /// <summary>
- /// RedisClient
- /// </summary>
- private static RedisClient _redisClient;
- /// <summary>
- /// 初始化配置
- /// </summary>
- private FreeRedisOption _redisOption;
- /// <summary>
- /// 构造函数
- /// </summary>
- public FreeRedisService(FreeRedisOption redisOption)
- {
- if (redisOption == null)
- {
- throw new NullReferenceException("初始化配置为空");
- }
- _redisOption = redisOption;
- InitRedisClient();
- }
- /// <summary>
- /// 懒加载Redis客户端
- /// </summary>
- private readonly static Lazy<RedisClient> redisClientLazy = new Lazy<RedisClient>(() => {
- 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();
- /// <summary>
- /// 初始化Redis
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取Client实例
- /// </summary>
- 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();
- }
- }
- }
-
|