예제 #1
0
        //添加
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                GetdbClient(out RedisClient dbclient);
                bool n = dbclient.Add <string>("name", "yueyue");
                dbclient.Add <int>("age", 2);
                //add 添加不可覆盖键值,set会覆盖同键的值
                dbclient.Add <int>("number", 158);
                dbclient.Set <string>("test", "hh");

                if (n == true)
                {
                    Response.Write("<script>alert('添加成功!')</script>");
                }
                else
                {
                    Response.Write("<script>alert('添加失败!')</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
예제 #2
0
        public void AddUserInformation(User user)
        {
            string serializedUser = ObjectSerializer.SerializeUser(user);
            string listId         = KeysDictionary.UserInformation(user.Username);

            redis.Add(listId, serializedUser);
        }
        public virtual string InsertFile(string filePath, string uploadFilePath)
        {
            string val = GetFileContents(uploadFilePath);

            dataContext.Add <string>(filePath, val);
            return(val);
        }
예제 #4
0
파일: Program.cs 프로젝트: muairui/c-
        private static void AddString(RedisClient client)
        {
            var timeOut = new TimeSpan(0, 0, 0, 30);

            client.Add("Test", "Learninghard", timeOut);
            while (true)
            {
                if (client.ContainsKey("Test"))
                {
                    Console.WriteLine("String Key: Test -Value: {0}, 当前时间: {1}", client.Get <string>("Test"), DateTime.Now);
                    Thread.Sleep(10000);
                }
                else
                {
                    Console.WriteLine("Value 已经过期了,当前时间:{0}", DateTime.Now);
                    break;
                }
            }
            var person = new Person()
            {
                Name = "Learninghard", Age = 26
            };

            client.Add("lh", person);
            var cachePerson = client.Get <Person>("lh");

            Console.WriteLine("Person's Name is : {0}, Age: {1}", cachePerson.Name, cachePerson.Age);
        }
예제 #5
0
 //添加数据
 public void AddTest()
 {
     try
     {
         GetdbClient(out RedisClient dbclient);
         string a    = "redisTest";
         int    bnum = 5;
         dbclient.Add <string>("a", a);
         dbclient.Add <int>("b", bnum);
         var exist = dbclient.Exists("a");
         if (exist == 1)
         {
             LogHelper.WriteResult(path, "添加数据成功。");
         }
         else
         {
             LogHelper.WriteResult(path, "添加数据失败。");
         }
     }
     catch (Exception ex)
     {
         string ss = "添加数据失败:" + ex.Message;
         LogHelper.WriteResult(path, ss);
     }
 }
예제 #6
0
        public static void ExecuteRedis()
        {
            RedisClient dataContext = new RedisClient("localhost");

            //dataContext.RemoveAll(new List<string>() { "./test.txt", "./temp.txt" });
            dataContext.Add <string>("harageth./file.txt", "This is a test file to make sure that things are working correctly.");
            dataContext.Add <string>("harageth./temp.txt", "And now we will store a second file to just see if we can get a couple of files at once.");
        }
 /// <summary>
 /// 添加缓存
 /// </summary>
 /// <param name="key">缓存键</param>
 /// <param name="target">值</param>
 /// <param name="time">过期时间,单位:秒,0为永不过期</param>
 protected override void AddCache(string key, object target, int time)
 {
     if (time == 0)
     {
         _client.Add(key, target);
         return;
     }
     _client.Add(key, target, DateTime.Now.AddSeconds(time));
 }
예제 #8
0
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存建</param>
        /// <param name="t">缓存值</param>
        /// <param name="timeout">过期时间,单位秒,-1:不过期,0:默认过期时间</param>
        /// <returns></returns>
        public bool Set <T>(string key, T t, int timeout = 0)
        {
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }

            return(Redis.Add <T>(key, t));
        }
예제 #9
0
 /// <summary>
 /// key如果不存在,则添加value,返回true;如果key已经存在,则不添加value,返回false。
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="entity"></param>
 /// <param name="expirySeconds">过期秒数</param>
 /// <returns></returns>
 public bool SetIfNotExists <T>(string key, T entity, int expirySeconds = -1)
 {
     using (RedisClient redisClient = GetRedisClient())
     {
         if (expirySeconds == -1)
         {
             return(redisClient.Add(key, entity));
         }
         else
         {
             return(redisClient.Add(key, entity, new TimeSpan(0, 0, 0, expirySeconds)));
         }
     }
 }
예제 #10
0
 /// <summary>
 /// 缓存数据存储在redis
 /// </summary>
 /// <param name="rkey">键</param>
 /// <param name="rvalue">值</param>
 /// <param name="exptime">过期时间</param>
 public void Add <T>(string rkey, T rvalue, DateTime?exptime = null)
 {
     using (RedisClient rc = new RedisClient(_option.Server, _option.Port, _option.Password))
     {
         if (exptime == null)
         {
             rc.Add(rkey, rvalue);
         }
         else
         {
             rc.Add(rkey, rvalue, exptime.Value);
         }
     }
 }
예제 #11
0
 public void Write(string message)
 {
     using (var redis = new RedisClient(_uri))
     {
         redis.Add("CfBus log", message);
     }
 }
예제 #12
0
        //public JsonResult DataRedis()
        //{
        //    RedisHelper Redishelper = new RedisHelper();
        //    var msg = "";
        //    //从redis里面拿到这个数据
        //    var str = Redishelper.StringGetRedis("NameList");
        //    if (str == "Redis取出数据失败" || str == "发生错误")
        //    {

        //        var res = clientHelper.Post("api/YxApi/GetUserInfo", 1);
        //        if (res != null)
        //        {
        //            var data = JsonConvert.DeserializeObject<UnitedReturn>(res.ToString());
        //            var Data = data.data;
        //            //把范形集合数据存到redis里面
        //            Redishelper.StringSetToRedis("NameList", Data.ToString());
        //            var Redis = Redishelper.StringGetRedis("NameList");
        //            if (Redis != null && Redis != "Redis取出数据失败")
        //            {
        //                msg = "数据存到Redis成功";
        //            }
        //            else
        //            {
        //                msg = "数据存到Redis失败";
        //            }
        //        }
        //        else
        //        {
        //            msg = "获取不到数据";
        //        }
        //    }
        //    else
        //    {
        //        //把获取到的Redis数据存到cookie中
        //        HttpCookie cookie = new HttpCookie("NameList");
        //        cookie.Value = HttpUtility.UrlEncode(str);
        //        Response.Cookies.Add(cookie);
        //        msg = "获取Redis数据成功";
        //    }

        //    return Json(new { Name = msg }, JsonRequestBehavior.AllowGet);
        //}
        /// <summary>
        /// 写入Redis
        /// </summary>
        /// <returns></returns>
        public JsonResult DataRedis()
        {
            try
            {
                RedisClient client  = new RedisClient("127.0.0.1", 6379);
                var         msg     = "";
                var         getData = client.Get <string>("NameList");
                if (!string.IsNullOrEmpty(getData))
                {
                    msg = "Redis已有数据,不用存储";
                }
                else
                {
                    var res  = clientHelper.Post("api/YxApi/GetUserInfo", 1);
                    var data = JsonConvert.DeserializeObject <UnitedReturn>(res.ToString());
                    var Data = data.data;
                    client.Add <string>("NameList", Data.ToString());
                    var getData1 = client.Get <string>("NameList");
                    if (!string.IsNullOrEmpty(getData1))
                    {
                        msg = "数据存入Redis成功";
                    }
                    else
                    {
                        msg = "数据存入Redis失败";
                    }
                }
                return(Json(new { Name = msg }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception)
            {
                log.WriteLog("DataRedis", "写入redis");
                throw;
            }
        }
예제 #13
0
파일: RedisMgr.cs 프로젝트: LeehanLee/L.S
 public override bool Add <T>(string key, T value, int expireByMinutes)
 {
     using (RedisClient redis = new RedisClient(server))
     {
         return(redis.Add <T>(key, value, DateTime.UtcNow.AddMinutes(expireByMinutes)));
     }
 }
예제 #14
0
파일: RedisMgr.cs 프로젝트: LeehanLee/L.S
 /// <summary>
 /// 键存在时不添加也不覆盖,键不存在时添加
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public override bool Add <T>(string key, T value)
 {
     using (RedisClient redis = new RedisClient(server))
     {
         return(redis.Add <T>(key, value));
     }
 }
예제 #15
0
 /// <summary>
 /// 缓存Key
 /// </summary>
 /// <typeparam name="T">可序列化类型</typeparam>
 /// <param name="_Key">键</param>
 /// <param name="_Value">值</param>
 /// <param name="expriedDay">缓存有效天数</param>
 /// <returns>是否缓存成功</returns>
 public bool SetKey <T>(string _Key, T _Value, int expriedDay = -1)
 {
     try
     {
         if (_Client == null)
         {
             if (!Init_Server())
             {
                 return(false);
             }
         }
         DateTime Exprieds = DateTime.Now;
         if (expriedDay == -1)
         {
             Exprieds = DateTime.MaxValue;
         }
         else
         {
             Exprieds = Exprieds.AddDays(expriedDay);
         }
         return(_Client.Add <T>(_Key, _Value, Exprieds));
     }
     catch
     {
         return(false);
     }
 }
        public void Load()
        {
            var before     = DateTime.Now;
            var categorias = new List <ComboItem>();

            using (var nw = new NorthwindEntities1())
            {
                var query = nw.Order_Details.Select(cat => new
                {
                    Id = cat.OrderID,
                    UP = cat.UnitPrice
                });
                categorias.AddRange(query.AsEnumerable().Select(item => new ComboItem
                {
                    Id          = item.Id,
                    Descripcion = item.UP.ToString(CultureInfo.InvariantCulture)
                }));
            }

            Console.WriteLine("Buscar datos de categorias en BBDD tarda {0}. Registros {1} ", (DateTime.Now - before), categorias.Count);

            before = DateTime.Now;
            using (IRedisClient client = new RedisClient())
            {
                client.Add("cmbCategorias", categorias);

                client.ExpireEntryIn("cmbCategorias", new TimeSpan(0, 0, 10));

                Console.WriteLine("Insertar datos en Redis tarda {0} ", (DateTime.Now - before));
            }
        }
예제 #17
0
 public static bool Add <T>(string key, T value, DateTime dt)
 {
     using (RedisClient client = GetClient())
     {
         return(client.Add(key, value, dt));
     }
 }
예제 #18
0
 public void Send(TransportMessage message, Address address)
 {
    using(var redisClient = new RedisClient())
    {
        redisClient.Add(string.Format("{0}:{1}", address.Machine, address.Queue), message);
    }
 }
예제 #19
0
 public static bool Add <T>(string key, T value, TimeSpan ts)
 {
     using (RedisClient client = GetClient())
     {
         return(client.Add(key, value, ts));
     }
 }
예제 #20
0
                 /// <summary>
                 /// 设置缓存
                 /// </summary>
                 /// <typeparam name="T"></typeparam>
                 /// <param name="key">缓存建</param>
                 /// <param name="t">缓存值</param>
                 /// <param name="timeout">过期时间,单位秒,-1:不过期,0:默认过期时间</param>
                 /// <returns></returns>
                public bool Set <T>(string key, T t, int timeout = 0)
                
        {
                        if(timeout >= 0)
                         {
                                if(timeout > 0)
                                 {
                                        secondsTimeOut = timeout;
                                    
                }
                                Redis.Expire(key, secondsTimeOut);
                            
            }
             
                        return Redis.Add <T>(key, t);

                    
        }
예제 #21
0
        public bool Add <T>(string key, T value)
        {
            bool result;

            using (RedisClient redisClient = this.GetRedisClient())
            {
                result = redisClient.Add <T>(key, value);
            }
            return(result);
        }
예제 #22
0
        public static bool SetKeyStringValueObject <T>(String key, T value)
        {
            bool flag = false;

            using (RedisClient redisClient = new RedisClient(host, port))
            {
                flag = redisClient.Add(key, value);
                redisClient.Dispose();
            }
            return(flag);
        }
예제 #23
0
        private void button1_Click(object sender, EventArgs e)
        {
            RedisClient cli = new RedisClient("192.168.2.31");
            Random      r   = new Random();

            for (int i = 0; i < 1000000; i++)
            {
                cli.Add(r.Next().ToString(), r.Next().ToString());
            }
            cli.Save();
        }
예제 #24
0
 /// <summary>
 /// 存储值为string类型
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public static void SetKey(string key, string value)
 {
     using (var client = new RedisClient(Host))
     {
         if (client.ContainsKey(key))
         {
             client.Del(key);
         }
         client.Add <string>(key, value);
     }
 }
        private static void WhenStringIsValueAndRedisClient()
        {
            using (IRedisClient client = new RedisClient())
            {
                client.Remove(STRING_KEY);

                client.Add(STRING_KEY, "daniel");
                var nombre = client.Get <string>("daniel");
                Console.WriteLine(nombre);
            }
        }
예제 #26
0
파일: GO.cs 프로젝트: radtek/cccc
        /// <summary>
        /// 开始处理,尝试更新缓存
        /// </summary>
        /// <param name="typeS">操作类型,包括“更新”、“插入”</param>
        /// <param name="P_cmd">原始带参数的sql语句</param>
        /// <param name="P_ht_in">原始传入的参数</param>
        /// <param name="tablename">被操作的表名</param>
        /// <param name="filed">被操作的字段和对应值</param>
        /// <param name="where">操作条件字段和对应值</param>
        /// <returns></returns>
        public string TryUpdateRedis(string typeS, string P_cmd, Hashtable P_ht_in, string tablename, Dictionary <string, string> filed, Dictionary <string, string> where)
        {
            //获得数据后,根据需要进行判断和处理。 注意。 字段名称一般不会有错误,但是对应值,不一定是期待的@dlyx这样的,也可能是“dj+1”这样的东西。 具体情况具体处理。


            RedisClient RC = RedisClass.GetRedisClient(null);

            RC.Add("aaaaaaaaaaaa", "bbbbbbbbbbb");

            return("ok");
        }
예제 #27
0
        //添加对象
        public static void AddPerson(RedisClient client)
        {
            var person = new Person()
            {
                Name = "qhh", Age = 26
            };

            client.Add("person", person);
            var cachePerson = client.Get <Person>("person");

            Console.WriteLine("name=" + cachePerson.Name + "----age=" + cachePerson.Age);
        }
 public void AddSingleCachedData <TData>(string cacheName, TData entity, DateTime expiredDate)
 {
     try
     {
         client = new RedisClient(RedisIp, RedisPort);
         client.Add <TData>(cacheName, entity, expiredDate);
     }
     catch (Exception ex)
     {
         throw new KnownException(ErrorTypeEnum.CahceGeneralException, "RedisManager.AddSingle", ex);
     }
 }
예제 #29
0
        // GET api/values
        public IEnumerable <string> Get()
        {
            var lala = new List <string>
            {
                "1", "2", "3", "4", "5"
            };
            ICacheClientExtended cacheClient = new RedisClient("localhost");

            cacheClient.Add <IList <string> >("mykey", lala);
            var lala2 = cacheClient.Get <IList <string> >("mykey");

            return(lala2);
        }
예제 #30
0
        public void Test()
        {
            RedisClient client = new RedisClient("127.0.0.1", 6379);

            client.FlushAll(); //清除所有缓存
            client.FlushDb();  //删除当前数据库里面的所有数据
            //client.Add<string>("mykey", "我已设置过期时间噢3秒后会消失", DateTime.Now.AddMilliseconds(3000));
            #region 显示所以Key
            foreach (var key in client.Keys("*"))
            {
                Console.WriteLine(key); //若 key 存在返回 1 ,否则返回 0
            }
            #endregion
            #region 新增删除
            client.Add <string>("mykey", "数据abcd");
            Console.WriteLine(client.Exists("mykey")); //若 key 存在返回 1 ,否则返回 0
            Console.WriteLine(client.Get <string>("mykey"));
            client.Del("mykey");                       //client.Remove("key");
            Console.WriteLine(client.Exists("mykey"));
            if (!client.ContainsKey("mykey"))
            {
                Console.WriteLine("已删除");
            }
            #endregion
            #region 过期
            Console.WriteLine("过期测试");                 //若 key 存在返回 1 ,否则返回 0
            client.Add <string>("mykey", "数据abcd", DateTime.Now.AddMilliseconds(900));
            Console.WriteLine(client.Exists("mykey")); //若 key 存在返回 1 ,否则返回 0
            Thread.Sleep(1000);
            Console.WriteLine(client.Exists("mykey")); //若 key 存在返回 1 ,否则返回 0

            client.Add <string>("mykey", "数据abcd");
            client.Expire("mykey", 1);
            Console.WriteLine(client.Exists("mykey")); //若 key 存在返回 1 ,否则返回 0
            Thread.Sleep(1200);
            Console.WriteLine(client.Exists("mykey")); //若 key 存在返回 1 ,否则返回 0
            #endregion
        }
예제 #31
0
 public bool Add <T>(string key, T t, int timeout)
 {
     if (timeout < 0)
     {
         //永不過期
         return(Redis.Set(key, t));
     }
     if (timeout == 0)
     {
         //默認時長
         timeout = secondsTimeOut;
     }
     return(Redis.Add(key, t, TimeSpan.FromSeconds(timeout)));
 }