1. 程式人生 > 實用技巧 >《Redis實戰》-Josiah L.Carlson 的python的原始碼翻譯成C# 第三章

《Redis實戰》-Josiah L.Carlson 的python的原始碼翻譯成C# 第三章

using AIStudio.ConSole.Redis.Ch01;
using System;
using System.Collections.Generic;
using System.Threading;

namespace AIStudio.ConSole.Redis.Ch03
{
    class Program
    {
        static void Main(string[] args)
        {
            //根據連線資訊構造客戶端物件
            var conn = new CSRedis.CSRedisClient("127.0.0.1:6379,defaultDatabase=0,poolsize=500,ssl=false,writeBuffer=10240,prefix=test_
");//prefix所有的key會加上test_ bool bool_echo; long? long_echo; decimal? decimal_echo; string string_echo; string[] list_echo; Dictionary<string, string> dic_echo; ValueTuple<string, decimal>[] tuple_echo;
/******************************************************************************************************** # <start id="string-calls-1"/> >>> conn = redis.Redis() >>> conn.get('key') #A >>> conn.incr('key') #B 1 #B >>> conn.incr('key', 15) #B 16 #B >>> conn.decr('key', 5) #C 11 #C >>> conn.get('key') #D '11' #D >>> conn.set('key', '13') #E True #E >>> conn.incr('key') #E 14 #E # <end id="string-calls-1"/> ********************************************************************************************************
*/ #region string-calls-1 conn.Del("key"); string_echo = conn.Get("key"); Console.WriteLine($"conn.Get('key'); {string_echo}"); long_echo = conn.IncrBy("key"); Console.WriteLine($"conn.IncrBy('key'); {long_echo}"); long_echo = conn.IncrBy("key", 15); Console.WriteLine($"conn.IncrBy('key'); {long_echo}"); long_echo = conn.IncrBy("key", -5); string_echo = conn.Get("key"); Console.WriteLine($"conn.Get('key'); {string_echo}"); bool_echo = conn.Set("key", 13); Console.WriteLine($"conn.Set('key', 13); {PrintHelper.Bool2String_echo(bool_echo)}"); long_echo = conn.IncrBy("key"); Console.WriteLine($"conn.IncrBy('key'); {long_echo}"); #endregion /******************************************************************************************************** # <start id="string-calls-2"/> >>> conn.append('new-string-key', 'hello ') #A 6L #B >>> conn.append('new-string-key', 'world!') 12L #B >>> conn.substr('new-string-key', 3, 7) #C 'lo wo' #D >>> conn.setrange('new-string-key', 0, 'H') #E 12 #F >>> conn.setrange('new-string-key', 6, 'W') 12 >>> conn.get('new-string-key') #G 'Hello World!' #H >>> conn.setrange('new-string-key', 11, ', how are you?') #I 25 >>> conn.get('new-string-key') 'Hello World, how are you?' #J >>> conn.setbit('another-key', 2, 1) #K 0 #L >>> conn.setbit('another-key', 7, 1) #M 0 #M >>> conn.get('another-key') #M '!' #N # <end id="string-calls-2"/> *********************************************************************************************************/ #region string-calls-2 conn.Del("new-string-key"); long_echo = conn.Append("new-string-key", "hello "); Console.WriteLine($"conn.Append('new-string-key', 'hello '); {long_echo}"); long_echo = conn.Append("new-string-key", "world!"); Console.WriteLine($"conn.Append('new-string-key', 'world!'); {long_echo}"); string_echo = conn.GetRange("new-string-key", 3, 7); Console.WriteLine($"conn.GetRange('new-string-key', 3, 7); {string_echo}"); long_echo = conn.SetRange("new-string-key", 0, "H"); Console.WriteLine($"conn.SetRange('new-string-key', 0, 'H'); {long_echo}"); long_echo = conn.SetRange("new-string-key", 6, "W"); Console.WriteLine($"conn.SetRange('new-string-key', 6, 'W'); {long_echo}"); string_echo = conn.Get("new-string-key"); Console.WriteLine($"conn.Get('new-string-key'); {string_echo}"); bool_echo = conn.SetBit("another-key", 2, true); Console.WriteLine($"conn.SetBit('another-key', 2, true); {PrintHelper.Bool2String_echo(bool_echo)}"); bool_echo = conn.SetBit("another-key", 7, true); Console.WriteLine($"conn.SetBit('another-key', 7, true); {PrintHelper.Bool2String_echo(bool_echo)}"); string_echo = conn.Get("another-key"); Console.WriteLine($"conn.Get('another-key'); {string_echo}"); #endregion /******************************************************************************************************** # <start id="list-calls-1"/> >>> conn.rpush('list-key', 'last') #A 1L #A >>> conn.lpush('list-key', 'first') #B 2L >>> conn.rpush('list-key', 'new last') 3L >>> conn.lrange('list-key', 0, -1) #C ['first', 'last', 'new last'] #C >>> conn.lpop('list-key') #D 'first' #D >>> conn.lpop('list-key') #D 'last' #D >>> conn.lrange('list-key', 0, -1) ['new last'] >>> conn.rpush('list-key', 'a', 'b', 'c') #E 4L >>> conn.lrange('list-key', 0, -1) ['new last', 'a', 'b', 'c'] >>> conn.ltrim('list-key', 2, -1) #F True #F >>> conn.lrange('list-key', 0, -1) #F ['b', 'c'] #F # <end id="list-calls-1"/> *********************************************************************************************************/ #region list-calls-1 conn.Del("list-key"); long_echo = conn.RPush("list-key", "last"); Console.WriteLine($"conn.RPush('list-key', 'last'); {long_echo}"); long_echo = conn.LPush("list-key", "first"); Console.WriteLine($"conn.LPush('list-key', 'first'); {long_echo}"); long_echo = conn.RPush("list-key", "new last"); Console.WriteLine($"conn.RPush('list-key', 'new last'); {long_echo}"); list_echo = conn.LRange("list-key", 0, -1); Console.WriteLine($"conn.LRange('list-key', 0, -1); {PrintHelper.StringArray2String(list_echo)}"); string_echo = conn.LPop("list-key"); Console.WriteLine($"conn.LPop('list-key'); {string_echo}"); string_echo = conn.LPop("list-key"); Console.WriteLine($"conn.LPop('list-key'); {string_echo}"); list_echo = conn.LRange("list-key", 0, -1); Console.WriteLine($"conn.LRange('list-key', 0, -1); {PrintHelper.StringArray2String(list_echo)}"); long_echo = conn.RPush("list-key", "a", "b", "c"); Console.WriteLine($"conn.RPush('list-key', 'a', 'b', 'c'); {long_echo}"); list_echo = conn.LRange("list-key", 0, -1); Console.WriteLine($"conn.LRange('list-key', 0, -1); {PrintHelper.StringArray2String(list_echo)}"); bool_echo = conn.LTrim("list-key", 2, -1); Console.WriteLine($"conn.LTrim('list-key', 2, -1); {PrintHelper.Bool2String_echo(bool_echo)}"); list_echo = conn.LRange("list-key", 0, -1); Console.WriteLine($"conn.LRange('list-key', 0, -1); {PrintHelper.StringArray2String(list_echo)}"); #endregion /******************************************************************************************************** # <start id="list-calls-2"/> >>> conn.rpush('list', 'item1') #A 1 #A >>> conn.rpush('list', 'item2') #A 2 #A >>> conn.rpush('list2', 'item3') #A 1 #A >>> conn.brpoplpush('list2', 'list', 1) #B 'item3' #B >>> conn.brpoplpush('list2', 'list', 1) #C >>> conn.lrange('list', 0, -1) #D ['item3', 'item1', 'item2'] #D >>> conn.brpoplpush('list', 'list2', 1) 'item2' >>> conn.blpop(['list', 'list2'], 1) #E ('list', 'item3') #E >>> conn.blpop(['list', 'list2'], 1) #E ('list', 'item1') #E >>> conn.blpop(['list', 'list2'], 1) #E ('list2', 'item2') #E >>> conn.blpop(['list', 'list2'], 1) #E >>> # <end id="list-calls-2"/> *********************************************************************************************************/ #region list-calls-2 conn.Del("list"); conn.Del("list2"); long_echo = conn.RPush("list", "item1"); Console.WriteLine($"conn.RPush('list', 'item1'); {long_echo}"); long_echo = conn.RPush("list", "item2"); Console.WriteLine($"conn.RPush('list', 'item2'); {long_echo}"); long_echo = conn.RPush("list2", "item3"); Console.WriteLine($"conn.RPush('list2', 'item3'); {long_echo}"); string_echo = conn.BRPopLPush("list2", "list", 1); Console.WriteLine($"conn.BRPopLPush('list2', 'list', 1); {long_echo}"); string_echo = conn.BRPopLPush("list2", "list", 1); Console.WriteLine($"conn.BRPopLPush('list2', 'list', 1); {long_echo}"); list_echo = conn.LRange("list", 0, -1); Console.WriteLine($"conn.LRange('list', 0, -1); {PrintHelper.StringArray2String(list_echo)}"); string_echo = conn.BRPopLPush("list2", "list", 1); Console.WriteLine($"conn.BRPopLPush('list2', 'list', 1); {long_echo}"); string_echo = conn.BLPop(1, "list", "list2"); Console.WriteLine($"conn.BLPop(1, 'list', 'list2'); {string_echo}"); string_echo = conn.BLPop(1, "list", "list2"); Console.WriteLine($"conn.BLPop(1, 'list', 'list2'); {string_echo}"); string_echo = conn.BLPop(1, "list", "list2"); Console.WriteLine($"conn.BLPop(1, 'list', 'list2'); {string_echo}"); #endregion /******************************************************************************************************** # <start id="set-calls-1"/> >>> conn.sadd('set-key', 'a', 'b', 'c') #A 3 #A >>> conn.srem('set-key', 'c', 'd') #B True #B >>> conn.srem('set-key', 'c', 'd') #B False #B >>> conn.scard('set-key') #C 2 #C >>> conn.smembers('set-key') #D set(['a', 'b']) #D >>> conn.smove('set-key', 'set-key2', 'a') #E True #E >>> conn.smove('set-key', 'set-key2', 'c') #F False #F >>> conn.smembers('set-key2') #F set(['a']) #F # <end id="set-calls-1"/> *********************************************************************************************************/ #region set-calls-1 conn.Del("set-key"); conn.Del("set-key2"); long_echo = conn.SAdd("set-key", "a", "b", "c"); Console.WriteLine($"conn.SAdd('set-key', 'a', 'b', 'c'); {long_echo}"); long_echo = conn.SRem("set-key", "c", "d"); Console.WriteLine($"conn.SRem('set-key', 'c', 'd'); {long_echo}"); long_echo = conn.SRem("set-key", "c", "d"); Console.WriteLine($"conn.SRem('set-key', 'c', 'd'); {long_echo}"); long_echo = conn.SCard("set-key"); Console.WriteLine($"conn.SCard('set-key'); {long_echo}"); list_echo = conn.SMembers("set-key"); Console.WriteLine($"conn.SMembers('set-key'); {PrintHelper.StringArray2String(list_echo)}"); bool_echo = conn.SMove("set-key", "set-key2", "a"); Console.WriteLine($"conn.SMove('set-key', 'set-key2', 'a'); {PrintHelper.Bool2String_echo(bool_echo)}"); bool_echo = conn.SMove("set-key", "set-key2", "c"); Console.WriteLine($"conn.SMove('set-key', 'set-key2', 'a'); {PrintHelper.Bool2String_echo(bool_echo)}"); list_echo = conn.SMembers("set-key2"); Console.WriteLine($"conn.SMembers('set-key2'); {PrintHelper.StringArray2String(list_echo)}"); #endregion /******************************************************************************************************** # <start id="set-calls-2"/> >>> conn.sadd('skey1', 'a', 'b', 'c', 'd') #A 4 #A >>> conn.sadd('skey2', 'c', 'd', 'e', 'f') #A 4 #A >>> conn.sdiff('skey1', 'skey2') #B set(['a', 'b']) #B >>> conn.sinter('skey1', 'skey2') #C set(['c', 'd']) #C >>> conn.sunion('skey1', 'skey2') #D set(['a', 'c', 'b', 'e', 'd', 'f']) #D # <end id="set-calls-2"/> *********************************************************************************************************/ #region set-calls-2 conn.Del("skey1"); conn.Del("skey2"); long_echo = conn.SAdd("skey1", "a", "b", "c", "d"); Console.WriteLine($"conn.SAdd('skey1', 'a', 'b', 'c', 'd'); {long_echo}"); long_echo = conn.SAdd("skey2", "c", "d", "e", "f"); Console.WriteLine($"conn.SAdd('skey2', 'c', 'd', 'e', 'f'); {long_echo}"); list_echo = conn.SDiff("skey1", "skey2"); Console.WriteLine($"conn.SDiff('skey1', 'skey2'); {PrintHelper.StringArray2String(list_echo)}"); list_echo = conn.SInter("skey1", "skey2"); Console.WriteLine($"conn.SInter('skey1', 'skey2'); {PrintHelper.StringArray2String(list_echo)}"); list_echo = conn.SUnion("skey1", "skey2"); Console.WriteLine($"conn.SUnion('skey1', 'skey2'); {PrintHelper.StringArray2String(list_echo)}"); #endregion /******************************************************************************************************** # <start id="hash-calls-1"/> >>> conn.hmset('hash-key', { 'k1':'v1', 'k2':'v2', 'k3':'v3'}) #A True #A >>> conn.hmget('hash-key', ['k2', 'k3']) #B ['v2', 'v3'] #B >>> conn.hlen('hash-key') #C 3 #C >>> conn.hdel('hash-key', 'k1', 'k3') #D True #D # <end id="hash-calls-1"/> *********************************************************************************************************/ #region hash-calls-1 conn.Del("hash-key"); bool_echo = conn.HMSet("hash-key", new object[] { "k1", "v1", "k2", "v2", "k3", "v3" }); Console.WriteLine($"conn.HMSet('hash-key',new string[] {{ 'k1','v1', 'k2', 'v2', 'k3', 'v3' }}); {PrintHelper.Bool2String_echo(bool_echo)}"); list_echo = conn.HMGet("hash-key", new string[] { "k2", "k3" }); Console.WriteLine($"conn.HMGet('hash-key', new string[] {{ 'k2','k3' }}); {PrintHelper.StringArray2String(list_echo)}"); long_echo = conn.HLen("hash-key"); Console.WriteLine($"conn.HLen('hash-key'); {long_echo}"); long_echo = conn.HDel("hash-key", new string[] { "k1", "k3" }); Console.WriteLine($"conn.HDel('hash-key',new string[] {{ 'k1','k3' }}); {long_echo}"); #endregion /******************************************************************************************************** # <start id="hash-calls-2"/> >>> conn.hmset('hash-key2', { 'short':'hello', 'long':1000 * '1'}) #A True #A >>> conn.hkeys('hash-key2') #A ['long', 'short'] #A >>> conn.hexists('hash-key2', 'num') #B False #B >>> conn.hincrby('hash-key2', 'num') #C 1L #C >>> conn.hexists('hash-key2', 'num') #C True #C # <end id="hash-calls-2"/> *********************************************************************************************************/ #region conn.Del("hash-key2"); bool_echo = conn.HMSet("hash-key2", new object[] { "short", "hello", "long", 1000 * '1' }); Console.WriteLine($"conn.HMSet('hash-key2',new string[] {{ 'short','hello', 'long', 1000 * '1'}}); {PrintHelper.Bool2String_echo(bool_echo)}"); list_echo = conn.HKeys("hash-key2"); Console.WriteLine($"conn.HKeys('hash-key2'); {PrintHelper.StringArray2String(list_echo)}"); bool_echo = conn.HExists("hash-key2", "num"); Console.WriteLine($"conn.HExists('hash-key2', 'num'); {PrintHelper.Bool2String_echo(bool_echo)}"); long_echo = conn.HIncrBy("hash-key2", "num"); Console.WriteLine($"conn.HIncrBy('hash-key2', 'num'); {long_echo}"); bool_echo = conn.HExists("hash-key2", "num"); Console.WriteLine($"conn.HExists('hash-key2', 'num'); {PrintHelper.Bool2String_echo(bool_echo)}"); #endregion /******************************************************************************************************** # <start id="zset-calls-1"/> >>> conn.zadd('zset-key', 'a', 3, 'b', 2, 'c', 1) #A 3 #A >>> conn.zcard('zset-key') #B 3 #B >>> conn.zincrby('zset-key', 'c', 3) #C 4.0 #C >>> conn.zscore('zset-key', 'b') #D 2.0 #D >>> conn.zrank('zset-key', 'c') #E 2 #E >>> conn.zcount('zset-key', 0, 3) #F 2L #F >>> conn.zrem('zset-key', 'b') #G True #G >>> conn.zrange('zset-key', 0, -1, withscores = True) #H [('a', 3.0), ('c', 4.0)] #H # <end id="zset-calls-1"/> *********************************************************************************************************/ #region zset-calls-1 conn.Del("zset-key"); long_echo = conn.ZAdd("zset-key", new (decimal, object)[] { (3, 'a'), (2, 'b'), (1, 'c') }); Console.WriteLine($"conn.ZAdd('zset-key', new (decimal, object)[] {{ (3, 'a'),(2, 'b'), (1, 'c') }}); {long_echo}"); long_echo = conn.ZCard("zset-key"); Console.WriteLine($"conn.ZCard('zset-key'); {long_echo}"); decimal_echo = conn.ZIncrBy("zset-key", 'c', (decimal)3); Console.WriteLine($"conn.ZIncrBy('zset-key', 'c', (decimal)3); {decimal_echo}"); decimal_echo = conn.ZScore("zset-key", 'b'); Console.WriteLine($"conn.ZScore('zset-key', 'b'); {decimal_echo}"); long_echo = conn.ZRank("zset-key", 'c'); Console.WriteLine($"conn.ZRank('zset-key', 'c'); {long_echo}"); long_echo = conn.ZCount("zset-key", 0, 3); Console.WriteLine($"conn.ZCount('zset-key', 0, 3); {long_echo}"); long_echo = conn.ZRem("zset-key", 'b'); Console.WriteLine($"conn.ZRem('zset-key', 'b'); {long_echo}"); tuple_echo = conn.ZRangeWithScores("zset-key", 0, 1); Console.WriteLine($"conn.ZRangeWithScores('zset-key', 0, 1); {PrintHelper.ValueTuple2String(tuple_echo)}"); #endregion /******************************************************************************************************** # <start id="zset-calls-2"/> >>> conn.zadd('zset-1', 'a', 1, 'b', 2, 'c', 3) #A 3 #A >>> conn.zadd('zset-2', 'b', 4, 'c', 1, 'd', 0) #A 3 #A >>> conn.zinterstore('zset-i', ['zset-1', 'zset-2']) #B 2L #B >>> conn.zrange('zset-i', 0, -1, withscores = True) #B [('c', 4.0), ('b', 6.0)] #B >>> conn.zunionstore('zset-u', ['zset-1', 'zset-2'], aggregate = 'min') #C 4L #C >>> conn.zrange('zset-u', 0, -1, withscores = True) #C [('d', 0.0), ('a', 1.0), ('c', 1.0), ('b', 2.0)] #C >>> conn.sadd('set-1', 'a', 'd') #D 2 #D >>> conn.zunionstore('zset-u2', ['zset-1', 'zset-2', 'set-1']) #D 4L #D >>> conn.zrange('zset-u2', 0, -1, withscores = True) #D [('d', 1.0), ('a', 2.0), ('c', 4.0), ('b', 6.0)] #D # <end id="zset-calls-2"/> *********************************************************************************************************/ #region zset-calls-2 conn.Del("zset-1"); conn.Del("zset-2"); conn.Del("set-1"); long_echo = conn.ZAdd("zset-1", new (decimal, object)[] { (1, 'a'), (2, 'b'), (3, 'c') }); Console.WriteLine($"conn.ZAdd('zset-key', new (decimal, object)[] {{ (1, 'a'),(2, 'b'), (3, 'c') }}); {long_echo}"); long_echo = conn.ZAdd("zset-2", new (decimal, object)[] { (4, 'b'), (1, 'c'), (0, 'd') }); Console.WriteLine($"conn.ZAdd('zset-key', new (decimal, object)[] {{ (4, 'b'),(1, 'c'), (0, 'd') }}); {long_echo}"); long_echo = conn.ZInterStore("zset-i", null, CSRedis.RedisAggregate.Sum, "zset-1", "zset-2"); Console.WriteLine($"conn.ZInterStore('zset-i', null, CSRedis.RedisAggregate.Sum, 'zset-1', 'zset-2'); {long_echo}"); tuple_echo = conn.ZRangeWithScores("zset-i", 0, -1); Console.WriteLine($"conn.ZRangeWithScores('zset-i', 0, -1); {PrintHelper.ValueTuple2String(tuple_echo)}"); long_echo = conn.ZUnionStore("zset-u", null, CSRedis.RedisAggregate.Min, "zset-1", "zset-2"); Console.WriteLine($"conn.ZInterStore('zset-u', null, CSRedis.RedisAggregate.Min, 'zset-1', 'zset-2'); {long_echo}"); tuple_echo = conn.ZRangeWithScores("zset-u", 0, -1); Console.WriteLine($"conn.ZRangeWithScores('zset-u', 0, -1); {PrintHelper.ValueTuple2String(tuple_echo)}"); long_echo = conn.SAdd("set-1", 'a', 'd'); Console.WriteLine($"conn.SAdd('set-1', 'a', 'd'); {long_echo}"); long_echo = conn.ZUnionStore("zset-u2", null, CSRedis.RedisAggregate.Sum, "zset-1", "zset-2", "set-1"); Console.WriteLine($"conn.ZInterStore('zset-u2', null, CSRedis.RedisAggregate.Sum, 'zset-1', 'zset-2', 'set-1'); {long_echo}"); tuple_echo = conn.ZRangeWithScores("zset-u2", 0, -1); Console.WriteLine($"conn.ZRangeWithScores('zset-u2', 0, -1); {PrintHelper.ValueTuple2String(tuple_echo)}"); #endregion //new TestPublisher().Run_Pubsub(); /******************************************************************************************************** # <start id="sort-calls"/> >>> conn.rpush('sort-input', 23, 15, 110, 7) #A 4 #A >>> conn.sort('sort-input') #B ['7', '15', '23', '110'] #B >>> conn.sort('sort-input', alpha = True) #C ['110', '15', '23', '7'] #C >>> conn.hset('d-7', 'field', 5) #D 1L #D >>> conn.hset('d-15', 'field', 1) #D 1L #D >>> conn.hset('d-23', 'field', 9) #D 1L #D >>> conn.hset('d-110', 'field', 3) #D 1L #D >>> conn.sort('sort-input', by = 'd-*->field') #E ['15', '110', '7', '23'] #E >>> conn.sort('sort-input', by = 'd-*->field', get = 'd-*->field') #F ['1', '3', '5', '9'] #F # <end id="sort-calls"/> *********************************************************************************************************/ #region sort-calls conn.Del("sort-input"); conn.Del("d-7"); conn.Del("d-15"); conn.Del("d-23"); conn.Del("d-110"); long_echo = conn.RPush("sort-input", 23, 15, 110, 7); Console.WriteLine($"conn.RPush('sort-input', 23, 15, 110, 7); {long_echo}"); list_echo = conn.Sort("sort-input"); Console.WriteLine($"conn.Sort('sort-input'); {PrintHelper.StringArray2String(list_echo)}"); list_echo = conn.Sort("sort-input", isAlpha: true); ; Console.WriteLine($"conn.Sort('sort-input'); {PrintHelper.StringArray2String(list_echo)}"); bool_echo = conn.HSet("d-7", "field", 5); Console.WriteLine($"conn.HSet('d-7', 'field', 5); {PrintHelper.Bool2String_echo(bool_echo)}"); bool_echo = conn.HSet("d-15", "field", 1); Console.WriteLine($"conn.HSet('d-15', 'field', 1); {PrintHelper.Bool2String_echo(bool_echo)}"); bool_echo = conn.HSet("d-23", "field", 9); Console.WriteLine($"conn.HSet('d-23', 'field', 9); {PrintHelper.Bool2String_echo(bool_echo)}"); bool_echo = conn.HSet("d-110", "field", 3); Console.WriteLine($"conn.HSet('d-110', 'field', 3); {PrintHelper.Bool2String_echo(bool_echo)}"); list_echo = conn.Sort("sort-input", by: "d-*->field"); Console.WriteLine($"conn.Sort('sort-input', by: 'd-*->field'); {PrintHelper.StringArray2String(list_echo)}"); list_echo = conn.Sort("sort-input", by: "d-*->field", get: "d-*->field"); Console.WriteLine($"conn.Sort('sort-input', by: 'd-*->field', get: 'd-*->field'); {PrintHelper.StringArray2String(list_echo)}"); #endregion new TestNotrans().Test(); new TestTrans().Test(); /******************************************************************************************************** # <start id="other-calls-1"/> >>> conn.set('key', 'value') #A True #A >>> conn.get('key') #A 'value' #A >>> conn.expire('key', 2) #B True #B >>> time.sleep(2) #B >>> conn.get('key') #B >>> conn.set('key', 'value2') True >>> conn.expire('key', 100); conn.ttl('key') #C True #C 100 #C # <end id="other-calls-1"/> *********************************************************************************************************/ #region other-calls-1 conn.Del("key"); bool_echo = conn.Set("key", "value"); Console.WriteLine($"conn.Set('key', 'value'); {PrintHelper.Bool2String_echo(bool_echo)}"); string_echo = conn.Get("key"); Console.WriteLine($"conn.Get('key'); {string_echo}"); bool_echo= conn.Expire("key", 2); Console.WriteLine($"conn.Expire('key', 2); {PrintHelper.Bool2String_echo(bool_echo)}"); Thread.Sleep(2000); string_echo = conn.Get("key"); Console.WriteLine($"conn.Get('key'); {string_echo}"); bool_echo = conn.Set("key", "value2"); Console.WriteLine($"conn.Set('key', 'value2'); {PrintHelper.Bool2String_echo(bool_echo)}"); bool_echo = conn.Expire("key", 100); Console.WriteLine($"conn.Expire('key', 100); {PrintHelper.Bool2String_echo(bool_echo)}"); long_echo= conn.Ttl("key"); Console.WriteLine($"conn.Ttl('key'); {long_echo}"); #endregion } } }
View Code

using CSRedis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AIStudio.ConSole.Redis.Ch03
{
    /********************************************************************************************************
    # <start id="simple-pipeline-notrans"/>
    >>> def notrans():
    ...     print conn.incr('notrans:')                     #A
    ...     time.sleep(.1)                                  #B
    ...     conn.incr('notrans:', -1)                       #C
    ...
    >>> if 1:
    ...     for i in xrange(3) :                             #D
    ...         threading.Thread(target=notrans).start()    #D
    ...     time.sleep(.5)                                  #E
    ...
    1                                                       #F
    2                                                       #F
    3                                                       #F
    # <end id="simple-pipeline-notrans"/>
    *********************************************************************************************************/
    class TestNotrans
    {
        CSRedisClient _conn;
        public TestNotrans()
        {
            _conn = new CSRedis.CSRedisClient("127.0.0.1:6379,defaultDatabase=14,poolsize=500,ssl=false,writeBuffer=10240");
        }
        public void Notrans()
        {
            Console.WriteLine(_conn.IncrBy("notrans:"));
            Thread.Sleep(100);
            _conn.IncrBy("notrans:", -1);
        }

        public void Test()
        {
            for (int i = 0; i < 3; i++)
            {
                Thread thread = new Thread(new ThreadStart(Notrans));
                thread.Start();
            }
            Thread.Sleep(500);
        }
    }
}
View Code
using CSRedis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AIStudio.ConSole.Redis.Ch03
{
    /********************************************************************************************************
    # <start id="pubsub-calls-1"/>
    >>> def publisher(n):
    ...     time.sleep(1)                                                   #A
    ...     for i in xrange(n):
    ...         conn.publish('channel', i)                                  #B
    ...         time.sleep(1)                                               #B
    ...

    >>> def run_pubsub():
    ...     threading.Thread(target = publisher, args = (3,)).start()           #D
    ...     pubsub = conn.pubsub()                                          #E
    ...     pubsub.subscribe(['channel'])                                   #E
    ...     count = 0...     for item in pubsub.listen():                                    #F
    ...         print item                                                  #G
    ...         count += 1                                                  #H
    ...         if count == 4:                                              #H
    ...             pubsub.unsubscribe()                                    #H
    ...         if count == 5:                                              #L
    ...             break                                                   #L
    ...
    >>> run_pubsub()                                                        #C
    { 'pattern': None, 'type': 'subscribe', 'channel': 'channel', 'data': 1L}#I
    { 'pattern': None, 'type': 'message', 'channel': 'channel', 'data': '0'} #J
    { 'pattern': None, 'type': 'message', 'channel': 'channel', 'data': '1'} #J
    { 'pattern': None, 'type': 'message', 'channel': 'channel', 'data': '2'} #J
    {'pattern': None, 'type': 'unsubscribe', 'channel': 'channel', 'data':  #K
    0L}                                                                     #K
    # <end id="pubsub-calls-1"/>
    *********************************************************************************************************/
    public class TestPublisher
    {
        CSRedisClient _conn;
        public TestPublisher()
        {
            _conn = new CSRedis.CSRedisClient("127.0.0.1:6379,defaultDatabase=14,poolsize=500,ssl=false,writeBuffer=10240");
        }

        public void Publisher(object n)
        {
            Thread.Sleep(1000);
            for (int i = 0; i <= (int)n; i++)
            {
                _conn.Publish("channel", i.ToString());
                Thread.Sleep(1000);
            }
        }

        public void Run_Pubsub()
        {
            Thread thread = new Thread(new ParameterizedThreadStart(Publisher));
            thread.Start(3);

            int count = 0;
            CSRedisClient.SubscribeObject pubsub = null;
            pubsub = _conn.Subscribe(("channel", item =>
            {
                Console.WriteLine(item.Body);
                count += 1;
                if (count == 4)
                {
                    pubsub.Unsubscribe();
                }
            }));
        }
    }
}
View Code
using CSRedis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AIStudio.ConSole.Redis.Ch03
{
    /********************************************************************************************************
    # <start id="simple-pipeline-trans"/>
    >>> def trans():
    ...     pipeline = conn.pipeline()                      #A
    ...     pipeline.incr('trans:')                         #B
    ...     time.sleep(.1)                                  #C
    ...     pipeline.incr('trans:', -1)                     #D
    ...     print pipeline.execute()[0]                     #E
    ...
    >>> if 1:
    ...     for i in xrange(3):                             #F
    ...         threading.Thread(target=trans).start()      #F
    ...     time.sleep(.5)                                  #G
    ...
    1                                                       #H
    1                                                       #H
    1                                                       #H
    # <end id="simple-pipeline-trans"/>
    *********************************************************************************************************/
    class TestTrans
    {
        CSRedisClient _conn;
        public TestTrans()
        {
            _conn = new CSRedis.CSRedisClient("127.0.0.1:6379,defaultDatabase=14,poolsize=500,ssl=false,writeBuffer=10240");
        }
        public void Trans()
        {
            var pipeline = _conn.StartPipe();
            Console.WriteLine(pipeline.IncrBy("notrans:"));
            Thread.Sleep(100);
            pipeline.IncrBy("notrans:", -1);
            Console.WriteLine(pipeline.EndPipe()[0]);
        }

        public void Test()
        {
            for (int i = 0; i < 3; i++)
            {
                Thread thread = new Thread(new ThreadStart(Trans));
                thread.Start();
            }
            Thread.Sleep(500);
        }
    }
}
View Code

原始碼碼下載地址AIStudio.ConSole.Redis (gitee.com)