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次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
随机推荐
- 学习ASP.NET Core, 怎能不了解请求处理管道[4]: 应用的入口——Startup
一个ASP.NET Core应用被启动之后就具有了针对请求的处理能力,而这个能力是由管道赋予的,所以应用的启动同时意味着管道的成功构建.由于管道是由注册的服务器和若干中间件构成的,所以应用启动过程中一 ...
- C++中的引用
一,C++中引用的基础知识 1.引用的基本概念 1.所谓的引用其实就是对变量起“别名”.引用和变量对应得是相同的内存,修改引用的值,变量的值也会改变,和指针类似. 2.引用在定义的时候必须要初始化,初 ...
- 12、Struts2表单重复提交
什么是表单重复提交 表单的重复提交: 若刷新表单页面, 再提交表单不算重复提交. 在不刷新表单页面的前提下: 多次点击提交按钮 已经提交成功, 按 "回退" 之后, 再点击 &qu ...
- bzoj1723--前缀和(水题)
题目大意: 你难以想象贝茜看到一只妖精在牧场出现时是多么的惊讶.她不是傻瓜,立即猛扑过去,用她那灵活的牛蹄抓住了那只妖精. "你可以许一个愿望,傻大个儿!"妖精说. ...
- 品牌营销:不要Beat,要逼格!
品牌营销:不要Beat,要逼格! 奥美的创始人大卫·奥格威说,广告营销应当是"具有风度的推销产品".而当下的营销手段,"风度"早已被抛之脑后, ...
- Android中使用ViewFlipper实现屏幕页面切换(关于坐标轴的问题已补充更改)
屏幕切换指的是在同一个Activity内屏幕间的切换,ViewFlipper继承了Framelayout类,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果.如 ...
- HotApp小程序服务范围资质查询器
微信小程序提交审核需要选择资质服务范围,如果服务范围不对,审核会不通过, 开发小程序之前,最好先查询所开发小程序的资质范围,否则无法通过微信审核. 小程序的资质范围查询地址,数据同步微信官方 ht ...
- 端盘子的服务生到月薪一万五的IT精英,你能相信吗
一直以来,我都觉得自己不是一个有故事的人. 以前的我,是个乖宝宝,对父母言听计从,特别内向,甚至一度感觉到自卑.不上学之后,我干过送货员,去工地除泥搬砖,当过油漆工,去过工厂,还去饭店当过端盘子的服务 ...
- 使用MonoTouch.Dialog简化iOS界面开发
MonoTouch.Dialog简称MT.D,是Xamarin.iOS的一个RAD工具包.它提供易于使用的声明式API,不需要使用导航控制器.表格等ViewController来定义复杂的应用程序UI ...
- 2000条你应知的WPF小姿势 基础篇<51-56 依赖属性>
前一阵子由于个人生活原因,具体见上一篇,耽搁了一阵子,在这里也十分感谢大家支持和鼓励.现在开始继续做WPF2000系列. 在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件 ...