コード例 #1
0
        private void Init()
        {
            string UserName;

            UserName = RedisUtils.Item_Get <string>("UserInfo_123");

            //读取数据,如果缓存存在直接从缓存中读取,否则从数据库读取然后写入redis
            if (string.IsNullOrEmpty(UserName)) //初始化缓存
            {
                //TODO 从数据库中获取数据,并写入缓存
                UserName = "******";
                RedisUtils.Item_Set <string>("UserInfo_123", UserName);
                Label1.Text = "数据库数据:" + "张三";
                return;
            }
            Label1.Text = "Redis缓存数据:" + UserName;
        }
コード例 #2
0
        /// <summary>
        /// server context-menu item click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuElement_Server_Click(object sender, RoutedEventArgs e)
        {
            MenuElement item        = (MenuElement)sender;
            ContextMenu menu        = (ContextMenu)item.Parent;
            string      uid         = (string)menu.Tag;
            RedisServer redisServer = RedisUtils.getRedisServer(uid);

            if (null == redisServer)
            {
                return;
            }
            TreeViewItem viewItem = (TreeViewItem)redisServerBox.ItemContainerGenerator.ContainerFromItem(redisServer);

            viewItem.IsSelected = true;

            switch (item.Header.ToString())
            {
            case "Connection":
            case "Reload":
                redisRestconnection(redisServer);
                break;

            case "Disconnection":
                redisQuitConnection(redisServer);
                break;

            case "Delete":
                redisQuitConnection(redisServer);
                redisServer.RedisClient.Dispose();
                App.redisServers.Remove(redisServer);
                ConfigUtils.saveConfig();
                break;

            case "Edit":
                redisQuitConnection(redisServer);
                RedisServerWindow editServerWindow = new RedisServerWindow();
                RedisConnection   conn             = (RedisConnection)redisServer.Connection.Clone();
                editServerWindow.Server = redisServer;
                editServerWindow.ShowDialog();
                break;

            default:
                break;
            }
        }
コード例 #3
0
ファイル: HomeBus.cs プロジェクト: TuanCruise/Code
        public async Task <MaintainModuleInfo> LoadMaintainModuleInfo(string modId)
        {
            string key        = ECacheKey.MaintainModuleInfo.ToString() + modId;
            var    cachedData = _distributedCache.GetString(key);

            if (cachedData != null && cachedData != "null")
            {
                var exec = JsonConvert.DeserializeObject <List <MaintainModuleInfo> >(cachedData);
                return(exec.First());
            }
            else
            {
                var exec = await _moduleService.LoadMaintainModuleInfo(modId);

                RedisUtils.SetCacheData(_distributedCache, _Configuration, exec, key);
                return(exec.First());
            }
        }
コード例 #4
0
ファイル: HomeBus.cs プロジェクト: TuanCruise/Code
        public async Task LoadBtnLanguage()
        {
            try
            {
                string key        = ECacheKey.BtnLanguageInfo.ToString();
                var    cachedData = _distributedCache.GetString(key);
                if (cachedData != null && cachedData != "null")
                {
                    var module = JsonConvert.DeserializeObject <List <LanguageInfo> >(cachedData);
                }
                else
                {
                    var model = await _moduleService.GetAllBtnLanguageText();

                    RedisUtils.SetCacheData(_distributedCache, _Configuration, model, key);
                }
            }
            catch (Exception e)
            {
            }
        }
コード例 #5
0
        static void Main(string[] args)
        {
            var set = RedisUtils.Set("Test", "100");
            var get = RedisUtils.Get("Test");
            var add = RedisUtils.Add("Test", "101");

            get = RedisUtils.Get("Test");
            set = RedisUtils.Set("Test", "102");
            get = RedisUtils.Get("Test");
            var remove = RedisUtils.Remove("Test");

            set = RedisUtils.Set("Test", "test");
            set = RedisUtils.Set("Test1", "test", 60 * 60);
            var keytime = RedisUtils.KeyTimeToLive("Test1");

            set = RedisUtils.Set("int", "1");
            var increment = RedisUtils.Increment("int");
            var decrement = RedisUtils.Decrement("int", 2);
            var getset    = RedisUtils.GetSet("int", "100");

            Console.ReadKey();
        }
コード例 #6
0
        private void initServerList()
        {
            App.config.RedisConnections.ForEach((connection) => {
                RedisUtils.addConnection(connection);
            });

            redisServerBox.Items.Clear();
            this.Dispatcher.Invoke(new Action(delegate
            {
                redisServerBox.ItemsSource = App.redisServers;
            }));

            ComboBoxItem viewTypText = new ComboBoxItem();

            viewTypText.Content = "Text plain";
            ComboBoxItem viewTypeJson = new ComboBoxItem();

            viewTypeJson.Content = "Json";
            viewType.Items.Add(viewTypText);
            viewType.Items.Add(viewTypeJson);
            viewType.SelectedIndex = 0;
        }
コード例 #7
0
        /// <summary>
        /// 登录认证
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public JsonMessage UserLoginAuth(string userName, string password)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return(JsonHandler.CreateMessage(-1, "请输入用户名"));
            }
            if (string.IsNullOrEmpty(password))
            {
                return(JsonHandler.CreateMessage(-1, "请输入密码"));
            }

            User user = GetUser(userName, password);

            if (user == null || user.UserId <= 0)
            {
                return(JsonHandler.CreateMessage(0, "用户名或密码错误"));
            }

            var userInfo = new
            {
                UserId   = user.UserId,
                UserName = user.UserName,
                PassWord = user.PassWord,
                Mobile   = user.Mobile,
                Role     = user.Role
            };

            //生成token
            var token = Guid.NewGuid().ToString();

            //写入token
            CookieUtils.AddCookie("token", token, 30);
            //写入凭证
            RedisUtils.Set(token, userInfo, new TimeSpan(0, 30, 0));

            return(JsonHandler.CreateMessage(1, "登录成功"));
        }
コード例 #8
0
ファイル: HomeBus.cs プロジェクト: TuanCruise/Code
        /// <summary>
        /// Load Tất cả defTask vào cache
        /// </summary>
        /// <returns></returns>
        public async Task <List <ModTreeView> > GetAllModTreeview()
        {
            try
            {
                string key        = ECacheKey.ModTreeview.ToString();
                var    cachedData = _distributedCache.GetString(key);
                if (cachedData != null && cachedData != "null")
                {
                    var modTreeview = JsonConvert.DeserializeObject <List <ModTreeView> >(cachedData);
                    return(modTreeview);
                }
                else
                {
                    var modTreeview = await _moduleService.GetAllModTreeView();

                    RedisUtils.SetCacheData(_distributedCache, _Configuration, modTreeview, key);
                    return(modTreeview);
                }
            }
            catch (Exception e)
            {
                return(null);
            }
        }
コード例 #9
0
ファイル: HomeBus.cs プロジェクト: TuanCruise/Code
        /// <summary>
        /// Load Tất cả defTask vào cache
        /// </summary>
        /// <returns></returns>
        public async Task <List <DefTasks> > LoadAllDefTasks()
        {
            try
            {
                string key        = ECacheKey.DefTasks.ToString();
                var    cachedData = _distributedCache.GetString(key);
                if (cachedData != null && cachedData != "null")
                {
                    var defTasks = JsonConvert.DeserializeObject <List <DefTasks> >(cachedData);
                    return(defTasks);
                }
                else
                {
                    var defTasks = await _moduleService.GetAllDefTasks();

                    RedisUtils.SetCacheData(_distributedCache, _Configuration, defTasks, key);
                    return(defTasks);
                }
            }
            catch (Exception e)
            {
                return(null);
            }
        }
コード例 #10
0
ファイル: HomeBus.cs プロジェクト: TuanCruise/Code
        public async Task <ModuleInfoViewModel> GetModule(string modId)
        {
            try
            {
                string key        = ECacheKey.ModuleInfo.ToString() + modId;
                var    cachedData = _distributedCache.GetString(key);
                if (cachedData != null && cachedData != "null")
                {
                    var module = JsonConvert.DeserializeObject <ModuleInfoViewModel>(cachedData);
                    return(module);
                }
                else
                {
                    var model = await _moduleService.GetModule(modId);

                    RedisUtils.SetCacheData(_distributedCache, _Configuration, model, key);
                    return(model);
                }
            }
            catch (Exception e)
            {
                return(null);
            }
        }
コード例 #11
0
ファイル: HomeBus.cs プロジェクト: TuanCruise/Code
        public async Task <SearchModuleInfo> LoadModSearchByModId(string modId)
        {
            try
            {
                string key        = ECacheKey.ModuleSearchInfo.ToString() + modId;
                var    cachedData = _distributedCache.GetString(key);
                if (cachedData != null && cachedData != "null")
                {
                    var module = JsonConvert.DeserializeObject <List <SearchModuleInfo> >(cachedData);
                    return(module.FirstOrDefault());
                }
                else
                {
                    var modSearchs = await _moduleService.LoadModSearchByModId(modId);

                    RedisUtils.SetCacheData(_distributedCache, _Configuration, modSearchs, key);
                    return(modSearchs.FirstOrDefault());
                }
            }
            catch (Exception e)
            {
                return(null);
            }
        }
コード例 #12
0
ファイル: HomeBus.cs プロジェクト: TuanCruise/Code
        public async Task <List <CodeInfo> > LoadAllDefCode(List <string> codes)
        {
            try
            {
                string key        = ECacheKey.DefCode.ToString();
                var    cachedData = _distributedCache.GetString(key);
                var    defcodes   = new List <CodeInfo>();
                if (cachedData != null && cachedData != "null")
                {
                    defcodes = JsonConvert.DeserializeObject <List <CodeInfo> >(cachedData);
                }
                else
                {
                    defcodes = await _moduleService.GetAllDefCode();

                    RedisUtils.SetCacheData(_distributedCache, _Configuration, defcodes, key);
                }
                return(defcodes.Where(x => codes.Contains(x.CodeName)).ToList());
            }
            catch (Exception e)
            {
                return(null);
            }
        }
コード例 #13
0
        /// <summary>
        /// 获取下拉数据(二级菜单)
        /// </summary>
        public ReturnItem <List <RetEnumerations> > GetSecondLevelEnumerations(EnumerationsModel parameter)
        {
            ReturnItem <List <RetEnumerations> > r = new ReturnItem <List <RetEnumerations> >();

            if (CustomConfigParam.IsUseRedis)
            {
                RedisUtils redisUtils = new RedisUtils();
                if (redisUtils.isCurMethodCached(System.Reflection.MethodBase.GetCurrentMethod(), parameter.OrgID.ToString(), parameter.GroupName))
                {
                    r.Data = redisUtils.getCacheContent <List <RetEnumerations> >(System.Reflection.MethodBase.GetCurrentMethod(), parameter.OrgID.ToString(), parameter.GroupName);
                    r.Msg  = "下拉数据获取成功";
                    r.Code = 0;
                    return(r);
                }
            }
            List <RetEnumerations> Enumerations = new List <RetEnumerations>();

            using (UserEntities user = new UserEntities())
            {
                try
                {
                    var list = new List <string>();
                    if (parameter.OrgID != -1)
                    {
                        list.Add("-1");
                        list.Add(parameter.OrgID.ToString());
                    }
                    else
                    {
                        list.Add("-1");
                    }
                    var getinfo = user.U_Enumerations.Where(s => s.GroupName == parameter.GroupName && list.Contains(s.OrgID.ToString())).ToList();
                    if (getinfo == null)
                    {
                        r.Data = null;
                        r.Code = -1;
                        r.Msg  = "下拉数据不存在";
                        return(r);
                    }
                    if (getinfo != null)
                    {
                        foreach (var item in getinfo)
                        {
                            if (item.ParentID == null)
                            {
                                RetEnumerations single = new RetEnumerations();
                                single.ID        = item.ID;
                                single.Label     = item.Label;
                                single.Value     = item.Value;
                                single.GroupName = item.GroupName;
                                single.Position  = item.Position;
                                single.ParentID  = item.ParentID;
                                single.Active    = item.Active;
                                single.OrgID     = item.OrgID;
                                Enumerations.Add(single);
                            }
                        }
                        Enumerations = Enumerations.OrderBy(o => o.Position).ToList();//升序
                        foreach (var item in Enumerations)
                        {
                            List <RetEnumerations> secondLevel = new List <RetEnumerations>();
                            foreach (var queue in getinfo)
                            {
                                if (queue.ParentID == item.ID)
                                {
                                    RetEnumerations single = new RetEnumerations();
                                    single.ID        = queue.ID;
                                    single.Label     = queue.Label;
                                    single.Value     = queue.Value;
                                    single.GroupName = queue.GroupName;
                                    single.Position  = queue.Position;
                                    single.ParentID  = queue.ParentID;
                                    single.Active    = queue.Active;
                                    single.OrgID     = queue.OrgID;
                                    secondLevel.Add(single);
                                }
                            }
                            item.SecondLevel = secondLevel;
                            item.SecondLevel = item.SecondLevel.OrderBy(o => o.Position).ToList();//升序
                        }
                        r.Data = Enumerations;
                        r.Msg  = "下拉数据获取成功";
                        r.Code = 0;
                    }
                    if (CustomConfigParam.IsUseRedis)
                    {
                        RedisUtils redisUtil = new RedisUtils();
                        redisUtil.saveToRedis(System.Reflection.MethodBase.GetCurrentMethod(), r.Data, parameter.OrgID.ToString(), parameter.GroupName);
                    }
                }
                catch (Exception e)
                {
                    r.Msg = "内部错误请重试";
                    log.ErrorFormat("内部错误:{0},{1}", e.Message, e.StackTrace);
                    r.Code = -1;
                }
            }

            return(r);
        }
コード例 #14
0
 /// <summary>
 /// 批量查数据库方法
 /// </summary>
 /// <param name="obj"></param>
 private void ProcessorHandler(object obj)
 {
     //提交数据库
     List <Order> PostList = RedisUtils.Get <List <Order> >("Sale20170523", 0);
 }
コード例 #15
0
        public Task <JsonResult> PostOrder(Order order)
        {
            if (order == null || string.IsNullOrEmpty(order.loginname))
            {
                return(null);
            }

            //order = new Order();
            //order.loginname = "chengwei";
            //order.username = "******";

            return(Task.Factory.StartNew(() =>
            {
                //秒杀库存数量
                int Number = 100;
                //返回结果
                OrderResult R = new OrderResult();
                //R.result = true;
                //R.content = "chenggong";

                //缓存数据库拿出请求队列
                List <Order> PostList = RedisUtils.Get <List <Order> >("Sale20170523", 0);
                if (PostList == null)
                {
                    PostList = new List <Order>();
                }
                PostList.Add(order);

                //缓存队列开始
                if (PostList.Count == 1)
                {
                    //创建一个进程,10秒后主动提交数据库
                    _timer = new Timer(new TimerCallback(ProcessorHandler), null, 10000, 0);

                    R.result = true;
                    R.content = "抢购成功";
                }
                //缓存队列达到请求数量
                else if (PostList.Count == Number)
                {
                    _timer.Dispose();
                    ProcessorHandler(null);

                    R.result = true;
                    R.content = "抢购成功";
                }
                //超出库存
                else if (PostList.Count > Number)
                {
                    _timer.Dispose();
                    R.result = false;
                    R.content = "库存不足";
                }
                //正常增加
                else
                {
                    R.result = true;
                    R.content = "抢购成功";
                }

                RedisUtils.Set <List <Order> >("Sale20170523", 0, PostList);
                AsyncManager.Parameters["Result"] = R;
            }).ContinueWith <JsonResult>(t =>
            {
                OrderResult R = AsyncManager.Parameters["Result"] as OrderResult;
                return Json(R, JsonRequestBehavior.AllowGet);
            }));
        }