Beispiel #1
0
        /// <summary>
        /// 菜单表任何操作将缓存重置
        /// </summary>
        public async void RestMenuCache()
        {
            _redisCacheManager.Remove("Menu");
            var menus = await GetMenuList();

            _redisCacheManager.Set("Menu", menus);
        }
Beispiel #2
0
 /// <summary>
 /// 释放组对象
 /// </summary>
 /// <param name="exception"></param>
 /// <returns></returns>
 public override async Task OnDisconnectedAsync(Exception exception)
 {
     if (Context.User.Claims.Any())
     {
         var Id = Context.User.Claims.FirstOrDefault(x => x.Type == "Id").Value.ToGuid();//链接释放将用户关联的ConnectionID从缓存中移除
         _redisCacheManager.Remove(Id.ToString());
     }
     await base.OnDisconnectedAsync(exception);
 }
Beispiel #3
0
        /// <summary>
        /// 添加用户登录信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task <bool> PostWx_UserOpenIdAsync(string code)
        {
            string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WxConfig.AppId + "&secret=" + WxConfig.AppSecret + "&js_code=" + code + "&grant_type=authorization_code";

            Wx_UserOpenId userLog = new Wx_UserOpenId();

            userLog.ID = IdHelper.CreateGuid();
            string returnText = WxHelper.GetResponse(url);

            if (returnText != "")
            {
                JObject obj        = Newtonsoft.Json.Linq.JObject.Parse(returnText);
                string  openid     = obj["openid"].ToString();
                string  sessionkey = obj["session_key"].ToString();
                userLog.OpenId = openid;

                _redisCacheManager.Remove("myOpenId");
                _redisCacheManager.Remove("mySessionKey");
                _redisCacheManager.Set("myOpenId", openid, TimeSpan.FromHours(2));
                _redisCacheManager.Set("mySessionKey", sessionkey, TimeSpan.FromHours(2));
            }
            userLog.LoginTime = DateTime.Now;

            var result = 0;

            return(await Task.Run(() =>
            {
                var isAny = db.Queryable <Wx_UserOpenId>().Where(it => it.OpenId == userLog.OpenId).Any();//是否存在
                if (isAny == false)
                {
                    result = db.Insertable(userLog).ExecuteCommand();
                }
                else
                {
                    result = db.Updateable <Wx_UserOpenId>().SetColumns(it => new Wx_UserOpenId()
                    {
                        LoginTime = userLog.LoginTime
                    })
                             .Where(it => it.OpenId == userLog.OpenId).ExecuteCommand();
                }

                return result > 0;
            }));
        }
Beispiel #4
0
        /// <summary>
        /// 菜单表任何操作将缓存重置
        /// </summary>
        public async Task RestMenuCache()
        {
            try
            {
                _redisCacheManager.Remove("Menu");
                var list = await this._menuRepositoty.GetAll(x => x.IsDrop == false).AsNoTracking().ToListAsync();

                _redisCacheManager.Set("Menu", list);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #5
0
        /// <summary>
        /// 获取微信用户openid
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetOpenId(string code)
        {
            string    url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WxConfig.AppId + "&secret=" + WxConfig.AppSecret + "&js_code=" + code + "&grant_type=authorization_code";
            WebClient wc  = new System.Net.WebClient();

            wc.Credentials = CredentialCache.DefaultCredentials;
            wc.Encoding    = Encoding.UTF8;
            string returnText = wc.DownloadString(url);

            if (returnText.Contains("errcode"))
            {
                //可能发生错误
            }

            JObject obj        = Newtonsoft.Json.Linq.JObject.Parse(returnText);
            string  openid     = obj["openid"].ToString();
            string  sessionkey = obj["session_key"].ToString();

            _redisCacheManager.Remove("myOpenId");
            _redisCacheManager.Remove("mySessionKey");
            _redisCacheManager.Set("myOpenId", openid, TimeSpan.FromHours(2));
            _redisCacheManager.Set("mySessionKey", sessionkey, TimeSpan.FromHours(2));
            return(openid);
        }
Beispiel #6
0
        //Intercept方法是拦截的关键所在,也是IInterceptor接口中的唯一定义
        //代码已经合并 ,学习pr流程
        public override void Intercept(IInvocation invocation)
        {
            var method = invocation.MethodInvocationTarget ?? invocation.Method;

            if (method.ReturnType == typeof(void) || method.ReturnType == typeof(Task))
            {
                invocation.Proceed();
                return;
            }
            //对当前方法的特性验证
            if (method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) is CachingAttribute qCachingAttribute)
            {
                //获取自定义缓存键
                var cacheKey   = qCachingAttribute.CustomKeyValue ?? CustomCacheKey(invocation);
                var cacheValue = _cache.GetValue(cacheKey);
                if (cacheValue != null)
                {
                    if (qCachingAttribute.IsDelete)
                    {
                        //删除Redis里面的数据
                        _cache.Remove(cacheKey);
                    }
                    else
                    {
                        //将当前获取到的缓存值,赋值给当前执行方法
                        Type returnType;
                        if (typeof(Task).IsAssignableFrom(method.ReturnType))
                        {
                            returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault();
                        }
                        else
                        {
                            returnType = method.ReturnType;
                        }
                        dynamic _result = Newtonsoft.Json.JsonConvert.DeserializeObject(cacheValue, returnType);
                        invocation.ReturnValue = (typeof(Task).IsAssignableFrom(method.ReturnType)) ? Task.FromResult(_result) : _result;
                        return;
                    }
                }
                //去执行当前的方法
                invocation.Proceed();

                //存入缓存
                if (!string.IsNullOrWhiteSpace(cacheKey) && qCachingAttribute.IsDelete == false)
                {
                    object response;
                    var    type = invocation.Method.ReturnType;
                    if (typeof(Task).IsAssignableFrom(type))
                    {
                        var resultProperty = type.GetProperty("Result");
                        response = resultProperty.GetValue(invocation.ReturnValue);
                    }
                    else
                    {
                        response = invocation.ReturnValue;
                    }
                    if (response == null)
                    {
                        response = string.Empty;
                    }
                    _cache.Set(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration));
                }
            }
            else
            {
                invocation.Proceed();//直接执行被拦截方法
            }
        }
Beispiel #7
0
 /// <summary>
 /// 移除指定数据缓存
 /// </summary>
 /// <param name="cacheKey">键</param>
 public void RemoveCache(string cacheKey)
 {
     _cache.Remove(cacheKey);
 }