Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you both the native
commands and the usage of the StackExchange.Redis.The version of Redis is 3.2.3 and the vesion of StackExchange.Redis
is 1.1.604-alpha.Yeah,You are right,I will show you by using a dotnet core console app.Maybe you will ask me that why don't
you use the ServiceStack.Redis? The reasons why I choose StackExchange.Redis are as follow:
1.I am a poor man so that I can not pay for the License of ServiceStack.Redis
2.The opreations of this two drive are vary easy,but I like to use the StackExchange.Redis.
3.Open source code
First of all,we should start the service of redis.We can not do anything without the running service.And use the ./redis-cli to
enter the client.Then choose the second database for this tutorial.
How many commands belong to Strings?You can find the answer in http://www.redis.io/commands#string
and I use Xmind to make a picture as follow:
Up to today,there are 24 commands that we can use to handle Strings.But I will choose some of them to show you in this
tutorials .Many commands are very useful of Strings,and I will Introduce them at the future tutorials.
The first thing we should take care is that how to store the data? If you learned Memcached before,you can compare with them.
They have some common features.But there are no relationship between Redis and Memcached.In Redis,you can use the command
set to store the data you want to save,and the command get can help you to find out the data you stored.For example, I set a key
named name with a value catcher,after successffully stored,the client will return you a OK message.And using the get command with
the key's name you can get the value of this key.
set name catcher get name
However those two commands can only handle a single k/v.How about handle two k/v or more k/v?Don't worry,there are also some
commands(mset,mget) can handle multi k/v which can help you to finish some difficult jobs.The usage of them look like this:
mset age 18 gender male mget name age gender
It's very good for the flexible commands.The next command is append,which you may often use in the program languages to
handle the strings,such as C#'s StringBuilder.After creating an instance of StringBuilder(sb),we can use sb.Append to append many
strings to sb.Naturally,command append can do this too.As you can see , I appended wong to catcher so I got the result catcherwong.
append name wong
For Relational Database,when designing a table,we often set the primary key increasing automatically.How can we do in Redis?The
creator of Redis already considerred this situation.All of us can use command incr to increase the value by 1 automatically,and use
command incrby to increase the value by a integet.I made the key named age increase automatically and the client returned the result
after increasing.The command incrby can assign the value to increase.
incr age incr age 2
set the value and expiration of a key.For an instance,I set a key named tmp for saving 5s.
setex tmp 5 tmp
Stop introducing more,the usage of other commands you can find out in the site of Redis.
Now, I will show you the same commands above by using C#.
1 //connect the redis server by ip address 2 ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.198.128"); 3 //choose the second database of redis 4 IDatabase db = redis.GetDatabase(2); 5 6 //set get 7 db.StringSet("name","catcher"); 8 Console.WriteLine(string.Format("the value of name is {0}",db.StringGet("name"))); 9 10 //mset mget 11 var dic = new Dictionary<RedisKey, RedisValue>(); 12 dic.Add("age",18); 13 dic.Add("gender","male"); 14 db.StringSet(dic.ToArray()); 15 16 var keys = new RedisKey[2] { "age","gender"}; 17 Parallel.ForEach(db.StringGet(keys), (item) => 18 { 19 Console.WriteLine(string.Format("mget value ----{0}",item)); 20 }); 21 22 //append 23 db.StringAppend("name", " wong"); 24 Console.WriteLine(string.Format("after append the value of name is {0}",db.StringGet("name"))); 25 26 //incr incrby 27 db.StringIncrement("age"); 28 Console.WriteLine(string.Format("after incr the value of age is {0}", db.StringGet("age"))); 29 db.StringIncrement("age",2); 30 Console.WriteLine(string.Format("after incrby the value of age is {0}", db.StringGet("age"))); 31 32 //setex 33 db.StringSet("tmp", "tmp", TimeSpan.FromSeconds(5)); 34 var task = Task.Factory.StartNew(()=> 35 { 36 Task.Delay(TimeSpan.FromSeconds(5)).Wait(); 37 Console.WriteLine(string.IsNullOrWhiteSpace(db.StringGet("tmp"))?"empty value":db.StringGet("tmp").ToString()); 38 });
the redis server.You should sure that your debug environment can visit the CentOS in your Vmware.When you debug the above code ,
you will get the below result.