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

  The same as Memcached,Redis can also set the expired time when some of you want to use Redis for your Cache Module.Setex can

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 handle of StackExchange.Redis is vary vary similar with the native opreation.The most important about the code is to connect

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.

Basic Tutorials of Redis(2) - String的更多相关文章

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

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

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

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

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

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

  7. 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#? ...

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

  9. 二、Redis基本操作——String(实战篇)

    小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...

随机推荐

  1. git 命令

    切换仓库地址: git remote set-url origin xxx.git切换分支:git checkout name撤销修改:git checkout -- file删除文件:git rm  ...

  2. .Net Core MVC 网站开发(Ninesky) 2.3、项目架构调整-控制反转和依赖注入的使用

    再次调整项目架构是因为和群友dezhou的一次聊天,我原来的想法是项目尽量做简单点别搞太复杂了,仅使用了DbContext的注入,其他的也没有写接口耦合度很高.和dezhou聊过之后我仔细考虑了一下, ...

  3. 一篇文章看懂TPCx-BB(大数据基准测试工具)源码

    TPCx-BB是大数据基准测试工具,它通过模拟零售商的30个应用场景,执行30个查询来衡量基于Hadoop的大数据系统的包括硬件和软件的性能.其中一些场景还用到了机器学习算法(聚类.线性回归等).为了 ...

  4. 【开源】.Net Aop(静态织入)框架 BSF.Aop

    BSF.Aop .Net 免费开源,静态Aop织入(直接修改IL中间语言)框架,类似PostSharp(收费): 实现前后Aop切面和INotifyPropertyChanged注入方式. 开源地址: ...

  5. DataTable 转换成 Json的3种方法

    在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,A ...

  6. CGI与FastCGI nginx+PHP-FPM

    本文转载自CGI与FastCGI 1.当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html. ...

  7. 读书笔记汇总 - SQL必知必会(第4版)

    本系列记录并分享学习SQL的过程,主要内容为SQL的基础概念及练习过程. 书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL i ...

  8. JavaWeb——ServletContext

    一.基本概念 说起ServletContext,一些人会产生误解,以为一个servlet对应一个ServletContext.其实不是这样的,事实是一个web应用对应一个ServletContext, ...

  9. 关于SMARTFORMS文本编辑器出错

    最近在做ISH的一个打印功能,SMARTFORM的需求本身很简单,但做起来则一波三折. 使用环境是这样的:Windows 7 64bit + SAP GUI 740 Patch 5 + MS Offi ...

  10. Android Studio开发RecyclerView遇到的各种问题以及解决(二)

    开发RecyclerView时候需要导入别人的例子,我的是从github导入的,下载下github的压缩包之后解压看你要导入的文件是priject还是Module.(一般有app文件夹的大部分是pro ...