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. ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)

    操作 MySql 数据库使用MySql.Data程序包(MySql 开发,其他第三方可能会有些问题). project.json 代码: { "version": "1. ...

  2. iOS有关横向TableView的东西

    之前看到Apple store里面有横向的tableview,当然也有可能是collectionview啦. 尤其是项目中只有一条那么需要横向滑动的东西,就没有必要使用庞大的collectionvie ...

  3. psoc学习

    第一是:项目的路径需要放在Documents and Settings\,也就是默认的文件夹的地方,不然会报错错误范例为:Question:CY8CKIT-023 kit example projec ...

  4. oracle常用函数及示例

    学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...

  5. linux系统维护时的一些小技巧,包括系统挂载新磁盘的方法!可收藏!

    这里发布一些平时所用到的小技巧,不多,不过会持续更新.... 1.需要将history创建硬链接ln 全盘需要备份硬链接 ln /etc/xxx /home/xxx 2.root用户不可以远程 /et ...

  6. Win10连接远程桌面时提示“您的凭据不工作”

    我遇到这个问题的时候查找网上都给出一堆高大上的解决办法, 然而我的错误实际上是用户名的问题, 很多人以为远程用户名就一定是锁屏状态下的登录名, 其实不是,跟自己设置有关,所以首先应该检查远程用户名是否 ...

  7. 简约而不简单的Django新手图文教程

    本文面向:有python基础,刚接触web框架的初学者. 环境:windows7   python3.5.1  pycharm专业版  Django 1.10版 pip3 一.Django简介 百度百 ...

  8. 腾讯云下安装 nodejs + 实现 Nginx 反向代理

    本文将介绍如何给腾讯云上的 Ubuntu Server 12.04 LTS 64位主机安装 node 及 nginx,并简单配置反向代理. 笔者在整个安装过程中遇到不少麻烦(不赘述),如果你希望少踩坑 ...

  9. HTML5 摇一摇加强版之一次失败的探索

    最近在看设备传感器的API,当然也少不了研究一下让微信称神的“摇一摇”了.关于“摇一摇”的实现,网上很多资料所以不详细说了,但总是有布局.效果不全等各种问题,所以作为一名资深copypaster,代码 ...

  10. .NET里简易实现IoC

    .NET里简易实现IoC 前言 在前面的篇幅中对依赖倒置原则和IoC框架的使用只是做了个简单的介绍,并没有很详细的去演示,可能有的朋友还是区分不了依赖倒置.依赖注入.控制反转这几个名词,或许知道的也只 ...