加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Basic Tutorials of Redis(2) - String

发布时间:2020-12-16 04:46:49 所属栏目:安全 来源:网络整理
导读: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

  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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读