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

OK,let's begin.

  First of all,we should start the service of redis.We can not do anything without the running service.And use the    to

enter the client.Then choose the second database for this tutorial.

  How many commands belong to Strings?You can find the answer in 

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.

name

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

  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 

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 

The command incrby can assign the value to increase.

  The same as Memcached,Redis can also set the expired time when some of you want to use Redis 

set the value and expiration of a key.For an instance,I set a key named tmp for saving 5s.  

setex tmp 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#.

       ConnectionMultiplexer redis = ConnectionMultiplexer.Connect( IDatabase db = redis.GetDatabase( db.StringSet(, Console.WriteLine(.Format(,db.StringGet( dic = Dictionary dic.Add(, dic.Add(, keys = RedisKey[] { , Parallel.ForEach(db.StringGet(keys),(item) => Console.WriteLine(.Format( db.StringAppend(, Console.WriteLine(.Format(,db.StringGet( db.StringIncrement( Console.WriteLine(.Format(,db.StringGet( db.StringIncrement(, Console.WriteLine(.Format(,db.StringGet( db.StringSet(,,TimeSpan.FromSeconds( task = Task.Factory.StartNew(()=> Task.Delay(TimeSpan.FromSeconds( Console.WriteLine(.IsNullOrWhiteSpace(db.StringGet())?:db.StringGet( });
  The handle of StackExchange.Redis is vary vary similar with the native opreation.The most 

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.

The next post of this series is the basic opreation of Hash in Redis.

相关文章

在笔者近 3 年的 Java 一线开发经历中,尤其是一些移动端、用...
这一篇文章拖了有点久,虽然在项目中使用分布式锁的频率比较...
本文梳理总结了一些 Java 互联网项目中常见的 Redis 缓存应用...
书接上回,消息通知系统(notification-system)作为一个独立...
Redis 是目前互联网后端的热门中间件之一,在许多方面都有深...
在Java Spring 项目中,数据与远程数据库的频繁交互对服务器...