Exemplo n.º 1
0
		static void Main(string[] args)
		{
			var host = args.Length > 0 ? args[0] : "localhost";
			var port = args.Length > 1 ? int.Parse(args[1]) : 6379;

			var redisClient = new RedisClient(host, port);
				
			var before = DateTime.Now;
			for (var i = 0; i < Iterations; i++)
			{
				var key = KeyMaster + i;
				redisClient.Set(key, ValueMaster);

				//if (i % LogEveryTimes == 0)
				//    Console.WriteLine("Time taken at {0}: {1}ms", i, (DateTime.Now - before).TotalMilliseconds);
			}

			for (int i = 0; i < Iterations; i++)
			{
				var key = KeyMaster + i;
				redisClient.Get<string>(key);

				//if (i % LogEveryTimes == 0)
				//    Console.WriteLine("Time taken at {0}: {1}ms", i, (DateTime.Now - before).TotalMilliseconds);
			}

			Console.WriteLine("Total Time Taken: {0}ms", (DateTime.Now - before).TotalMilliseconds);
		}
		public RedisSubscription(RedisClient redisClient)
		{
			this.redisClient = redisClient;

			this.SubscriptionCount = 0;
			this.activeChannels = new List<string>();
		}
Exemplo n.º 3
0
		public RedisLock(RedisClient redisClient, string key, TimeSpan? timeOut)
		{
			this.redisClient = redisClient;
			this.key = key;

			ExecExtensions.RetryUntilTrue(
				() => redisClient.SetEntryIfNotExists(key, "lock " + DateTime.UtcNow.ToUnixTime()),
				timeOut
			);
		}
Exemplo n.º 4
0
		public RedisTransaction(RedisClient redisClient)
		{
			this.redisClient = redisClient;

			if (redisClient.CurrentTransaction != null)
				throw new InvalidOperationException("An atomic command is already in use");

			redisClient.Multi();
			redisClient.CurrentTransaction = this;
		}
Exemplo n.º 5
0
        /// <summary>
        /// 汇总输出
        /// </summary>
        public static void OutPutData()
        {
            string host = "localhost";

            using (RedisClient redisClient = new RedisClient(host))
            {
                var locateCount = redisClient.ZCard("AllLocates");
                var allLocateData = redisClient.ZRange("AllLocates", 0, (int)locateCount);
                List<string> allLocate = new List<string>();
                for (int i = 0; i < allLocateData.Length; i++)
                {
                    allLocate.Add(System.Text.Encoding.UTF8.GetString(allLocateData[i]));
                }
                allLocate = allLocate.OrderBy(p => p).ToList();

                Console.WriteLine("开始输出时间序列数据");
                FileStream fs = new FileStream(OutputPath, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                long total = 0;
                for (int h = 0; h < 24; h++)
                {
                    for (int m = 0; m < 60; m += 5)
                    {
                        var pre = string.Format("2014-09-07-{0:D2}:{1:D2}", h, m);
                        Console.WriteLine(string.Format("------------时段:{0}------------", pre));
                        for (int i = 0; i < allLocate.Count; i++)
                        {
                            string key = pre + "@" + allLocate[i];
                            var value = redisClient.Get(key);
                            string count = (value == null ? "0" : System.Text.Encoding.Default.GetString(value));
                            total += int.Parse(count);
                            sw.WriteLine(string.Format("{0},{1},{2}", pre, allLocate[i], count));
                        }
                    }

                }
                sw.Close();
                fs.Close();
                Console.WriteLine(string.Format("------------总和:{0}------------", total));
            }
        }
Exemplo n.º 6
0
		public RedisLock(RedisClient redisClient, string key, TimeSpan? timeOut)
		{
			this.redisClient = redisClient;
			this.key = key;

			ExecExtensions.RetryUntilTrue(
				() =>
				    {
                        //This pattern is taken from the redis command for SETNX http://redis.io/commands/setnx
				        
                        //Calculate a unix time for when the lock should expire
                        TimeSpan realSpan = timeOut ?? new TimeSpan(365, 0, 0, 0); //if nothing is passed in the timeout hold for a year
				        DateTime expireTime = DateTime.UtcNow.Add(realSpan);
				        string lockString = (expireTime.ToUnixTimeMs() + 1).ToString();
                        
                        //Try to set the lock, if it does not exist this will succeed and the lock is obtained
				        var nx = redisClient.SetEntryIfNotExists(key, lockString);
                        if (nx)
                            return true;

                        //If we've gotten here then a key for the lock is present. This could be because the lock is
                        //correctly acquired or it could be because a client that had acquired the lock crashed (or didn't release it properly).
                        //Therefore we need to get the value of the lock to see when it should expire
				        string lockExpireString = redisClient.Get<string>(key);
                        long lockExpireTime;
                        if (!long.TryParse(lockExpireString, out lockExpireTime))
                            return false;
                        //If the expire time is greater than the current time then we can't let the lock go yet
                        if (lockExpireTime > DateTime.UtcNow.ToUnixTimeMs())
                            return false;

                        //If the expire time is less than the current time then it wasn't released properly and we can attempt to 
                        //acquire the lock. This is done by setting the lock to our timeout string AND checking to make sure
                        //that what is returned is the old timeout string in order to account for a possible race condition.
                        return redisClient.GetAndSetEntry(key, lockString) == lockExpireString;
				    },
				timeOut
			);
		}
Exemplo n.º 7
0
 protected void ClosePipeline()
 {
     RedisClient.ResetSendBuffer();
     RedisClient.Pipeline = null;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Issue exec command (not queued)
 /// </summary>
 private void Exec()
 {
     RedisClient.Exec();
     RedisClient.FlushSendBuffer();
 }
Exemplo n.º 9
0
 public RedisTransaction(RedisClient redisClient) : base(redisClient)
 {
 }
 public RedisClientHashes(RedisClient client)
 {
     this.client = client;
 }
Exemplo n.º 11
0
		public RedisClientList(RedisClient client, string listId)
		{
			this.listId = listId;
			this.client = client;
		}
Exemplo n.º 12
0
 public RedisClientSortedSets(RedisClient client)
 {
     this.client = client;
 }
Exemplo n.º 13
0
 public RedisClientLists(RedisClient client)
 {
     this.client = client;
 }
Exemplo n.º 14
0
		public RedisClientHash(RedisClient client, string hashId)
		{
			this.client = client;
			this.hashId = hashId;
		}
 public RedisCommandQueue(RedisClient redisClient)
 {
     this.RedisClient = redisClient;
 }
Exemplo n.º 16
0
        public static void ReadData()
        {
            string host = "localhost";

            using (RedisClient redisClient = new RedisClient(host))
            {
                StreamReader sr = new StreamReader(DataPath);
                Console.WriteLine("开始读取原数据");
                var locDic = new Dictionary<string, int>();
                var sw = new Stopwatch();
                sw.Start();
                string s = sr.ReadLine();
                long i = 0;
                while (s != null)
                {
                    if (i % 10000 == 0)
                        Console.WriteLine("已读取数据量:" + i);
                    var sData = s.Split('\t');
                    var dateSplit = sData[4].Split(':');
                    int min = int.Parse(dateSplit[1]);
                    min = (min / 5) * 5;
                    var locate = sData[6];
                    string key = string.Format("{0}:{1:D2}@{2}", dateSplit[0], min, locate);
                    redisClient.Incr(key);
                    locDic[locate] = 0;
                    s = sr.ReadLine();
                    i++;
                }
                locDic.Keys.ToList().ForEach(p =>
                {
                    redisClient.ZAdd("AllLocates", 0, System.Text.Encoding.UTF8.GetBytes(p));
                });
                sw.Stop();
                Console.WriteLine("读取完毕,总数据量:" + i + ",时间:" + sw.ElapsedMilliseconds + " ms");
            }
        }
 public RedisClientList(RedisClient client, string listId)
 {
     this.listId = listId;
     this.client = client;
 }
Exemplo n.º 18
0
 public RedisClientSet(RedisClient client, string setId)
 {
     this.client = client;
     this.setId  = setId;
 }
Exemplo n.º 19
0
		public RedisTransaction(RedisClient redisClient) : base(redisClient)
		{
		
		}
Exemplo n.º 20
0
		public RedisClientSet(RedisClient client, string setId)
		{
			this.client = client;
			this.setId = setId;
		}
		protected void Dispose(RedisClient redisClient)
		{
			if (redisClient == null) return;
			try
			{
				redisClient.DisposeConnection();
			}
			catch (Exception ex)
			{
				Log.Error(string.Format(
					"Error when trying to dispose of RedisClient to host {0}:{1}",
					redisClient.Host, redisClient.Port), ex);
			}
		}