Esempio n. 1
0
    public string ReMulGet(RedisNativeClient rc)
    {
        string keys = mulget.Text;

        string[] keysval = keys.Split(';');
        string   vals    = "";

        foreach (string singlekey in keysval)
        {
            try
            {
                if (rc.Exists(singlekey) == 1)
                {
                    vals = vals + rc.Get(singlekey) + ';';
                }
                else
                {
                    vals = vals + "ERROR!;";
                }
            }
            catch
            {
                vals = vals + "ERROR!";
            }
        }
        return(vals);
    }
Esempio n. 2
0
        public IDictionary <string, object> GetValuesMap(string[] keys, IRedisClient client)
        {
            RedisNativeClient cli = client as RedisNativeClient;

            if (keys == null)
            {
                throw new ArgumentNullException("keys");
            }
            if (cli == null)
            {
                return(new Dictionary <string, object>());
            }
            var resultBytesArray = cli.MGet(keys);

            var results = new Dictionary <string, object>();

            for (var i = 0; i < resultBytesArray.Length; i++)
            {
                var key = keys[i];

                var resultBytes = resultBytesArray[i];
                if (resultBytes == null)
                {
                    results.Add(key, null);
                }
                else
                {
                    var resultString = resultBytes.FromUtf8Bytes();
                    var result       = JsonSerializer.DeserializeFromString <string>(resultString);
                    results.Add(key, result);
                }
            }

            return(results);
        }
        /// <inheritdoc />
        public void DisposeClient(RedisNativeClient client)
        {
            lock (this._clients) {
                for (var i = 0; i < this._clients.Length; i++)
                {
                    var writeClient = this._clients[i];
                    if (client != writeClient)
                    {
                        continue;
                    }

                    if (client.IsDisposed)
                    {
                        this._clients[i] = null;
                    }
                    else
                    {
                        client.TrackThread = null;
                        client.Active      = false;
                    }

                    Monitor.PulseAll(this._clients);
                    return;
                }
            }
        }
Esempio n. 4
0
    protected void btnRedisMullook(object sender, EventArgs e)
    {
        RedisNativeClient rclient = new RedisNativeClient("192.168.1.207", 6379);
        string            keys    = mulget.Text;

        string[] keysval = keys.Split(';');
        string   result  = "";

        byte[][] results = rclient.MGet(keysval);
        try
        {
            foreach (byte[] re in results)
            {
                foreach (byte r in re)
                {
                    result = result + Convert.ToString(r) + ';';
                }
            }
            Response.Write("<script>window.alert('" + result + "');window.location.href='../Mem_RedisTest.aspx'</script>");
        }
        catch
        {
            Response.Write("<script>window.alert('查询失败!');window.location.href='../Mem_RedisTest.aspx'</script>");
        }
    }
 /// <summary>
 ///     Disposes the write client.
 /// </summary>
 /// <param name="client" >The client.</param>
 public void DisposeWriteClient(RedisNativeClient client)
 {
     lock (this._writeClients) {
         client.Active = false;
         Monitor.PulseAll(this._writeClients);
     }
 }
 /// <summary>
 ///     Disposes the read only client.
 /// </summary>
 /// <param name="client" >The client.</param>
 public void DisposeReadOnlyClient(RedisNativeClient client)
 {
     lock (this._readClients) {
         client.Active = false;
         Monitor.PulseAll(this._readClients);
     }
 }
Esempio n. 7
0
        public void Test_Throughput()
        {
            var bytes = RandomBytes(MessageSizeBytes);
            var swTotal = Stopwatch.StartNew();

            var key = "test:bandwidth:" + bytes.Length;

            int bytesSent = 0;
            int bytesRecv = 0;

            using (var redisClient = new RedisNativeClient(RedisServer))
            {
                Count.Times(x =>
                {
                    var sw = Stopwatch.StartNew();

                    redisClient.Set(key, bytes);
                    bytesSent += bytes.Length;
                    "SEND {0} bytes in {1}ms".Print(bytes.Length, sw.ElapsedMilliseconds);

                    sw.Reset();
                    sw.Start();
                    var receivedBytes = redisClient.Get(key);
                    bytesRecv += receivedBytes.Length;
                    "RECV {0} bytes in {1}ms".Print(receivedBytes.Length, sw.ElapsedMilliseconds);

                    "TOTAL {0} bytes SENT {0} RECV {1} in {2}ms\n".Print(
                        bytesSent, bytesRecv, swTotal.ElapsedMilliseconds);
                });
            }
        }
        /// <summary>
        /// Must call this periodically to move work items from priority queue to pending queue
        /// </summary>
        public bool PrepareNextWorkItem()
        {
            //harvest zombies every 5 minutes
            var now = DateTime.UtcNow;
            var ts  = now - harvestTime;

            if (ts.TotalMinutes > 5)
            {
                HarvestZombies();
                harvestTime = now;
            }

            using (var disposableClient = clientManager.GetDisposableClient <SerializingRedisClient>())
            {
                var client = disposableClient.Client;

                //1. get next workItemId, or return if there isn't one
                var smallest = client.ZRangeWithScores(workItemIdPriorityQueue, 0, 0);
                if (smallest == null || smallest.Length <= 1 ||
                    RedisNativeClient.ParseDouble(smallest[1]) == CONVENIENTLY_SIZED_FLOAT)
                {
                    return(false);
                }
                var workItemId = client.Deserialize(smallest[0]) as string;

                // lock work item id
                var lockKey = queueNamespace.GlobalLockKey(workItemId);
                using (var disposableLock = new DisposableDistributedLock(client, lockKey, lockAcquisitionTimeout, lockTimeout))
                {
                    // if another client has queued this work item id,
                    // then the work item id score will be set to CONVENIENTLY_SIZED_FLOAT
                    // so we return false in this case
                    var score = client.ZScore(workItemIdPriorityQueue, smallest[0]);
                    if (score == CONVENIENTLY_SIZED_FLOAT)
                    {
                        return(false);
                    }

                    using (var pipe = client.CreatePipeline())
                    {
                        var rawWorkItemId = client.Serialize(workItemId);

                        // lock work item id in priority queue
                        pipe.QueueCommand(
                            r =>
                            ((RedisNativeClient)r).ZAdd(workItemIdPriorityQueue, CONVENIENTLY_SIZED_FLOAT, smallest[0]));

                        // track dequeue lock id
                        pipe.QueueCommand(r => ((RedisNativeClient)r).SAdd(dequeueIdSet, rawWorkItemId));

                        // push into pending set
                        pipe.QueueCommand(r => ((RedisNativeClient)r).LPush(pendingWorkItemIdQueue, rawWorkItemId));

                        pipe.Flush();
                    }
                }
            }
            return(true);
        }
        /// <inheritdoc />
        public void DisposeClient(RedisNativeClient client)
        {
            lock (this._readClients) {
                for (var i = 0; i < this._readClients.Length; i++)
                {
                    var readClient = this._readClients[i];
                    if (client != readClient)
                    {
                        continue;
                    }

                    if (client.IsDisposed)
                    {
                        this._readClients[i] = null;
                    }
                    else
                    {
                        client.TrackThread = null;
                        client.Active      = false;
                    }

                    Monitor.PulseAll(this._readClients);
                    return;
                }
            }

            lock (this._writeClients) {
                for (var i = 0; i < this._writeClients.Length; i++)
                {
                    var writeClient = this._writeClients[i];
                    if (client != writeClient)
                    {
                        continue;
                    }

                    if (client.IsDisposed)
                    {
                        this._writeClients[i] = null;
                    }
                    else
                    {
                        client.TrackThread = null;
                        client.Active      = false;
                    }

                    Monitor.PulseAll(this._writeClients);
                    return;
                }
            }

            // Client not found in any pool, pulse both pools.
            lock (this._readClients) {
                Monitor.PulseAll(this._readClients);
            }

            lock (this._writeClients) {
                Monitor.PulseAll(this._writeClients);
            }
        }
Esempio n. 10
0
 public void FlushAll()
 {
     foreach (var item in this.clients)
     {
         RedisNativeClient client = item.Value;
         client.FlushAll();
     }
 }
        public void Benchmark_SET_raw_bytes_8MB()
        {
            var redis = new RedisNativeClient(Config.MasterHost);

            this.Run("ServiceStack.Redis 8MB",
                     8000000,
                     (i, bytes) => redis.Set("eitan" + i.ToString(), bytes));
        }
Esempio n. 12
0
        public long PrependRangeToList(string listId, string[] values)
        {
            if (!this.IsValidValues(values))
            {
                return(0L);
            }
            RedisNativeClient client = this.pool.GetRedisClient(listId);

            return((client == null) ? 0L : client.LPush(listId, UTF8String.ToBytesArray(values)));
        }
Esempio n. 13
0
        public long LPush(string key, string[] values)
        {
            if (!this.IsValidValues(values))
            {
                return(0L);
            }
            RedisNativeClient client = this.pool.GetRedisClient(key);

            return((client == null) ? 0L : client.LPush(key, UTF8String.ToBytesArray(values)));
        }
Esempio n. 14
0
        public double HIncrByFloat(string key, string field, double incrementBy)
        {
            if (!this.IsValidField(field))
            {
                return(0.0);
            }

            RedisNativeClient client = this.pool.GetRedisClient(key);

            return((client == null) ? 0.0 : client.HIncrbyFloat(key, UTF8String.ToBytes(field), incrementBy));
        }
Esempio n. 15
0
        public long HExists(string key, string field)
        {
            if (!this.IsValidField(field))
            {
                return(0L);
            }

            RedisNativeClient client = this.pool.GetRedisClient(key);

            return((client == null) ? 0L : client.HExists(key, UTF8String.ToBytes(field)));
        }
Esempio n. 16
0
        public string RPopLPush(string key, string value)
        {
            if (!this.IsValidValue(value))
            {
                return(null);
            }

            RedisNativeClient client = this.pool.GetRedisClient(key);

            return((client == null) ? null : UTF8String.ToString(client.RPopLPush(key, value)));
        }
Esempio n. 17
0
        public string[] HMGet(string key, params string[] fields)
        {
            if (!this.IsValidFields(fields))
            {
                return(null);
            }

            RedisNativeClient client = this.pool.GetRedisClient(key);

            return((client == null) ? null : UTF8String.ToStringArray(client.HMGet(key, UTF8String.ToBytesArray(fields))));
        }
Esempio n. 18
0
        public bool SetValueIfNotExists(string key, string value, int expirySeconds)
        {
            if (!this.IsValidValue(value))
            {
                return(false);
            }

            RedisNativeClient client = this.pool.GetRedisClient(key);

            return((client == null) ? false : client.Set(key, UTF8String.ToBytes(value), exists: false, expirySeconds: expirySeconds));
        }
Esempio n. 19
0
 public static void RedisIncrement()
 {
     using (IRedisNativeClient nrc = new RedisNativeClient())
     {
         nrc.Del("Increment");
         for (int i = 0; i < iterations; i++)
         {
             nrc.Incr("Increment");
         }
     }
 }
Esempio n. 20
0
        public long HIncrBy(string key, string field, int incrementBy)
        {
            if (!this.IsValidField(field))
            {
                return(0L);
            }

            RedisNativeClient client = this.pool.GetRedisClient(key);

            return((client == null) ? 0L : client.HIncrby(key, UTF8String.ToBytes(field), incrementBy));
        }
Esempio n. 21
0
        public string GetValueFromHash(string hashId, string key)
        {
            if (!this.IsValidField(key))
            {
                return(null);
            }

            RedisNativeClient client = this.pool.GetRedisClient(hashId);

            return((client == null) ? null : UTF8String.ToString(client.HGet(hashId, UTF8String.ToBytes(key))));
        }
Esempio n. 22
0
        public bool LTrim(string key, int keepStartingFrom, int keepEndingAt)
        {
            RedisNativeClient client = this.pool.GetRedisClient(key);

            if (client == null)
            {
                return(false);
            }

            client.LTrim(key, keepStartingFrom, keepEndingAt);
            return(true);
        }
Esempio n. 23
0
        public bool LSet(string key, int index, string value)
        {
            RedisNativeClient client = this.pool.GetRedisClient(key);

            if (client == null)
            {
                return(false);
            }

            client.LSet(key, index, UTF8String.ToBytes(value));
            return(true);
        }
Esempio n. 24
0
        public long Append(string key, string value)
        {
            if (!this.IsValidValue(value))
            {
                return(0L);
            }

            // Get the corresponding Redis client.
            RedisNativeClient client = this.pool.GetRedisClient(key);

            return((client == null) ? 0L : client.Append(key, UTF8String.ToBytes(value)));
        }
Esempio n. 25
0
        public bool LInsert(string key, bool insertBefore, byte[] pivot, byte[] value)
        {
            RedisNativeClient client = this.pool.GetRedisClient(key);

            if (client == null)
            {
                return(false);
            }

            client.LInsert(key, insertBefore, pivot, value);
            return(true);
        }
Esempio n. 26
0
        public RedisNativeClient GetRedisClient(params string[] keys)
        {
            RedisNativeClient client = null;
            RedisNativeClient tmp    = null;

            for (var i = 0; i < keys.Length; i++)
            {
                // Get the current client.
                tmp = this.GetRedisClient(keys[i]);
                if (tmp == null)
                {
                    return(null);
                }

                // Check whether all clients are of the same.
                if (client == null)
                {
                    client = tmp;
                }
                else
                {
                    if (client != tmp)
                    {
#if DEBUG
                        ConsoleColor color = Console.ForegroundColor;
                        Console.Write("[");

                        Console.ForegroundColor = Global.ColorError;
                        Console.Write("ERROR");

                        Console.ForegroundColor = color;
                        Console.Write("] Cannot find a single client for keys ");

                        Console.ForegroundColor = Global.ColorEmphasis;
                        Console.Write(keys[i - 1]);

                        Console.ForegroundColor = color;
                        Console.Write(" and ");

                        Console.ForegroundColor = Global.ColorEmphasis;
                        Console.Write(keys[i]);

                        Console.ForegroundColor = color;
                        Console.WriteLine(".");
#endif
                        return(null);
                    }
                }
            }

            return(client);
        }
Esempio n. 27
0
        public IRedisClient GetRedisClient()
        {
            IRedisClient redisClient = redisPoolManager.GetClient();//获取连接

            if (this.Db.HasValue)
            {
                redisClient.Db = this.Db.Value;
            }
            RedisNativeClient redisNativeClient = (RedisNativeClient)redisClient;

            redisNativeClient.Client = null;//ApsaraDB for Redis不支持client setname所以这里需要显示的把client对象置为null
            return(redisClient);
        }
Esempio n. 28
0
File: Redis.cs Progetto: radtek/Shop
        public static void RemoveSession(string key)
        {
            if (string.IsNullOrEmpty(key) || key == "-1")
            {
                return;
            }

            string redis_key = "SESS_" + key;

            using (RedisNativeClient RNC = GetNativeClientForKeySpace(space))
            {
                RNC.Del(redis_key);
            }
        }
Esempio n. 29
0
    protected void btnReOperate(object sender, EventArgs e)
    {
        RedisNativeClient rclient = new RedisNativeClient("192.168.1.207", 6379);

        try
        {
            string keyval  = key3.Text.Trim();
            int    number  = int.Parse(num.Text.Trim());
            string selflag = Select1.Value;
            long   renum;

            if (selflag.Equals("+"))
            {
                // int.Parse(rclient.Get(key));
                //Int64 re = 0;
                byte[] valueGet = rclient.Get(keyval);
                //if(valueGet == null ||
                //    valueGet.Length <= 0
                //    || valueGet.Length > 8)
                //{
                //    return;
                //}

                //byte[] total = new byte[8];
                //for (int i = 0; i < 8; i++ )
                //{
                //    total[i] = 0;
                //}

                //for (int i = 0; i < valueGet.Length; i++)
                //{
                //    total[i] = valueGet[i];
                //}

                //re = System.BitConverter.ToInt64(total, 0);

                renum = rclient.IncrBy(keyval, number);
                Response.Write("<script>window.alert('" + renum + "');window.location.href='../Mem_RedisTest.aspx'</script>");
            }
            else if (selflag.Equals("-"))
            {
                renum = rclient.DecrBy(keyval, number);
                Response.Write("<script>window.alert('" + renum + "');window.location.href=../Mem_RedisTest.aspx'</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>window.alert('" + ex.Message + "');window.location.href=../Mem_RedisTest.aspx'</script>");
        }
    }
Esempio n. 30
0
File: Redis.cs Progetto: radtek/Shop
 private static void ConRedis()
 {
     try
     {
         using (RedisNativeClient RNC = GetNativeClientForKeySpace(space))
         {
             RNC.Ping();
         }
         IsCon = true;
     }
     catch
     {
         IsCon = false;
     }
 }
Esempio n. 31
0
File: Redis.cs Progetto: radtek/Shop
        /// <summary>
        /// 设置key 的值和保存时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="session_message"></param>
        /// <param name="expire_minutes"></param>
        public static void SetSession(string key, object session_message, int expire_minutes)
        {
            if (key == "-1")
            {
                return;
            }
            string redis_key = "SESS_" + key;

            byte[] bytes = Encoding.UTF8.GetBytes((string)JsonHelper.ToJson(session_message));
            using (RedisNativeClient RNC = GetNativeClientForKeySpace(space))
            {
                RNC.Set(redis_key, bytes);
                RNC.Expire(redis_key, Convert.ToInt32(new TimeSpan(0, expire_minutes, 0).TotalSeconds));
            }
        }
		public RedisPipelineCommand(RedisNativeClient client)
		{
			this.client = client;
		}
 public RedisGeoNativeClientTests()
 {
     redis = new RedisNativeClient(TestConfig.GeoHost);
 }
 public RedisGeoNativeClientTests()
 {
     redis = new RedisNativeClient("10.0.0.121");
 }