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次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
随机推荐
- 如何利用tcpdump对mysql进行抓包操作
命令如下: tcpdump -s -l -w - dst -i eno16777736 |strings 其中-i指定监听的网络接口,在RHEL 7下,网络接口名不再是之前的eth0,而是 eno16 ...
- 简约之美Jodd-http--深入源码理解http协议
Jodd 是一个开源的 Java 工具集, 包含一些实用的工具类和小型框架.简单,却很强大! jodd-http是一个轻巧的HTTP客户端.现在我们以一个简单的示例从源码层看看是如何实现的? Http ...
- 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型
前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...
- APEX:对object中数据进行简单处理?
在Salesforce中,常常要对各种数据进行处理,已满足业务逻辑.本篇文章会介绍如何实现从object获取数据,然后将取得的数据进行一系列简单处理. 第一步:SongName__c 是一个新建的ob ...
- 5.0 JS中引用类型介绍
其实,在前面的"js的六大数据类型"文章中稍微说了一下引用类型.前面我们说到js中有六大数据类型(五种基本数据类型 + 一种引用类型).下面的章节中,我们将详细讲解引用类型. 1. ...
- Tableau未必最佳,国内BI也能突破重围!
如今,百度一下商业智能或BI工具,总能看到Tableau的身影.并不是Tableau的营销做得好,而是国内对于商业智能工具的认知和选择似乎都落在了Tableau身上.导致不管业内业外都对商业智能的概念 ...
- Android Studio 编译单个module
前期自己要把gradle环境变量配置好 在Terminal中gradle命令行编译apk 输入gradle assembleRelease 会编译全部module编译单个modulecd ./xiru ...
- MongoDB常用操作--集合1
1.创建集合有两种方式,显示创建和隐式创建 显示创建可以使用命令 db.createCollection("集合名称") 隐式创建可以使用命令 db.集合名称.insert({}) ...
- OpenSUSE下编译安装OpenFoam
在不是Ubuntu系统下安装OpenFoam,需要采用编译安装的方式.以下以OpenSuSE为例进行编译安装. 1 软件包准备 需要下载两个程序包: OpenFOAM-4.x-version-4.1. ...
- 1 selenium3.0.1无法打开火狐浏览器
[问题描述] 1.配置selenium3.0和java后,尝试打开火狐浏览器,提示缺少geckodriver驱动. [解决方案] 1.在http://www.seleniumhq.org/downlo ...