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. [APUE]UNIX进程的环境(上)

    一. 前言 本章将学习:当执行程序时,其main函数是如何被调用的,命令行参数是如何传送给执行程序的,典型的存储器布局是什么样式,如何分配另外的存储空间,进程如何使用环境变量,进程终止的不同方式等.另 ...

  2. UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?

    选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...

  3. UWP开发之Template10实践:本地文件与照相机文件操作的MVVM实例(图文付原代码)

    前面[UWP开发之Mvvmlight实践五:SuspensionManager中断挂起以及复原处理]章节已经提到过Template10,为了认识MvvmLight的区别特做了此实例. 原代码地址:ht ...

  4. 数据图表插件Echarts(一)

    一.引言 最近做一个智慧城市项目,项目中需要图表和报表进行数据分析,从网上找了很多,最后找到了百度开放的echarts,一个很强大的插件. 二.介绍 ECharts,缩写来自Enterprise Ch ...

  5. Javascript 代理模式模拟一个文件同步功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 程序猿都没对象,JS竟然有对象?

    现在做项目基本是套用框架,不论是网上的前端还是后端框架,也会寻找一些封装好的插件拿来即用,但还是希望拿来时最好自己过后再回过头了解里面的原理,学习里面优秀的东西,不论代码封装性,还是小到命名. 好吧, ...

  7. CSS 3 学习——渐变

    通过CSS渐变创建的是一个没有固定比例和固定尺寸的<image>类型,也就是说是一张图片,这张图片的尺寸由所应用的元素的相关信息决定.凡是支持图片类型的CSS属性都可以设置渐变,而支持颜色 ...

  8. PHP设计模式(七)适配器模式(Adapter For PHP)

    适配器模式:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 如下图(借图): // 设置书的接口 // 书接口 interface BookI ...

  9. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  10. DevExpress第三方控件使用实例之ASPxPopupControl弹出子窗体

    弹出页面控件:ASPxPopupControl, <dxpc:ASPxPopupControl ID="popubCtr" runat="server" ...