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 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

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#.
//connect the redis server by ip address
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.198.128");
//choose the second database of redis
IDatabase db = redis.GetDatabase(); //set get
db.StringSet("name","catcher");
Console.WriteLine(string.Format("the value of name is {0}",db.StringGet("name"))); //mset mget
var dic = new Dictionary<RedisKey, RedisValue>();
dic.Add("age",);
dic.Add("gender","male");
db.StringSet(dic.ToArray()); var keys = new RedisKey[] { "age","gender"};
Parallel.ForEach(db.StringGet(keys), (item) =>
{
Console.WriteLine(string.Format("mget value ----{0}",item));
}); //append
db.StringAppend("name", " wong");
Console.WriteLine(string.Format("after append the value of name is {0}",db.StringGet("name"))); //incr incrby
db.StringIncrement("age");
Console.WriteLine(string.Format("after incr the value of age is {0}", db.StringGet("age")));
db.StringIncrement("age",);
Console.WriteLine(string.Format("after incrby the value of age is {0}", db.StringGet("age"))); //setex
db.StringSet("tmp", "tmp", TimeSpan.FromSeconds());
var task = Task.Factory.StartNew(()=>
{
Task.Delay(TimeSpan.FromSeconds()).Wait();
Console.WriteLine(string.IsNullOrWhiteSpace(db.StringGet("tmp"))?"empty value":db.StringGet("tmp").ToString());
});
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.

Basic Tutorials of Redis(2) - String的更多相关文章
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Basic Tutorials of Redis(8) -Transaction
Data play an important part in our project,how can we ensure correctness of the data and prevent the ...
- Basic Tutorials of Redis(7) -Publish and Subscribe
This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...
- Basic Tutorials of Redis(6) - List
Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- Basic Tutorials of Redis(4) -Set
This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...
- Basic Tutorials of Redis(3) -Hash
When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...
- Basic Tutorials of Redis(1) - Install And Configure Redis
Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...
- 二、Redis基本操作——String(实战篇)
小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
随机推荐
- 卡片抽奖插件 CardShow
这个小项目(卡片秀)是一个卡片抽奖特效插件,用开源项目这样的词语让我多少有些羞愧,毕竟作为一个涉世未深的小伙子,用项目的标准衡量还有很大差距.不过该案例采用 jQuery 插件方式编写,提供配置参数并 ...
- 就这么漂来漂去---一个毕业三个月的java程序员的裸辞风波
注:这并不是一篇技术文章,而是记录了我这几个月经历的入职,裸辞,找工作的心路历程,简单介绍一个博主的情况,我是16年毕业生,校招进了一家北京的公司,java开发,和很多年轻人一样,干了一段时间,我发现 ...
- css中line-height行高的深入学习
之前对css中行高line-height的理解还是有些肤浅,深入后才发觉里面包罗万象.学习行高line-height,首先从基本原理开始 (标注该文章转载 http://www.cnblogs.com ...
- 关于MJRefresh的下拉加载数据bug
当没有更多数据的时候显示NoMoreData 我的理解是先结束刷新再显示没有更多 今天之前一直没发现有问题 贴之前的代码 [self.collectionView reloadData]; [self ...
- 给缺少Python项目实战经验的人
我们在学习过程中最容易犯的一个错误就是:看的多动手的少,特别是对于一些项目的开发学习就更少了! 没有一个完整的项目开发过程,是不会对整个开发流程以及理论知识有牢固的认知的,对于怎样将所学的理论知识应用 ...
- 定时Job在IIS中潜在危险-IIS 定期回收
引言 有时我们会在IIS中启用一些定时服务,但是你必须清楚IIS会定期回收Asp.net的应用程序的.首先来看IIS啥时候回收APPDomain. APPDomain 回收时机 There are ...
- 让Mono 4在Raspberry Pi上飞
最近公司有项目想要在树莓派上做,代替原来的工控机(我们是把工控主机当作小的主机用,一台小的工控主机最少也要600左右,而树莓派只要200多).于是,公司买了一个Raspberry Pi B+和一个Ra ...
- C++的性能C#的产能?! - .Net Native 系列《三》:.NET Native部署测试方案及样例
之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...
- 写自己的Socket框架(一)
本系列仅介绍可用于生产环境的C#异步Socket框架,如果您在其他地方看到类似的代码,不要惊讶,那可能就是我在参考开源代码时,直接“剽窃”过来的. 1.在脑海里思考一下整个socket的链接的处理流程 ...
- 基于java的分布式爬虫
分类 分布式网络爬虫包含多个爬虫,每个爬虫需要完成的任务和单个的爬行器类似,它们从互联网上下载网页,并把网页保存在本地的磁盘,从中抽取URL并沿着这些URL的指向继续爬行.由于并行爬行器需要分割下载任 ...