UP | HOME

redis-essentials

Table of Contents

Chapter 1: Getting Started

Hello Redis

  • 安装就不介绍了,主要是可以使用源代码编译,或者是各种包管理软件安装
  • redis和mysql一样,也是cs架构的系统:
    • 相同的一点是两者的server都是后台运行,redis选择了端口6379(mysql是3306),以 brew安装为例,如果希望开机后redis就开始运行,需要运行如下命令
      brew services start redis
      
    • 不同的是,mysql的client就叫做mysql,而redis为了不混淆使用了redis-cli作为自己 的client
  • redis-cli不再使用SQL,而是使用redis定义的一些命令,比如我们使用SET来创建一个value 为string类型的key;使用GET来读取这个key
    127.0.0.1:6379> SET philosopher "socrates"
    OK
    127.0.0.1:6379> GET philosopher
    "socrates"
    
  • 我们可以使用HELP来了解某个命令
    127.0.0.1:6379> HELP SET
    
      SET key value [EX seconds] [PX milliseconds] [NX|XX]
      summary: Set the string value of a key
      since: 1.0.0
      group: string
    
  • KEYS命令也非常重要,他会返回当前server存储的,满足某种特定pattern(这里的是 glob-style pattern[https://en.wikipedia.org/wiki/Glob_(programming)])的key, 比如我们查找以p开头的key
    127.0.0.1:6379> KEYS p*
    1) "philosopher"
    
  • redis-cli只能起到简单的调试查询的功能,更多的功能我们需要写代码来展示,本书决定 使用nodejs来书写和redis的example(主要是网站)代码

Hello World with Node.js and Redis

  • 首先我们要安装nodejs,这就不多说了.然后我们要使用nodejs的package管理npm来创建 一个空项目
    redis-nodejs-demo (master) $ npm init --yes
    Wrote to /Users/hfeng/github/redis-nodejs-demo/package.json:
    
    {
      "name": "redis-nodejs-demo",
      "version": "1.0.0",
      "description": "Learn redis with the help of nodejs",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "repository": {
        "type": "git",
        "url": "git+https://github.com/harrifeng/redis-nodejs-demo.git"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "bugs": {
        "url": "https://github.com/harrifeng/redis-nodejs-demo/issues"
      },
      "homepage": "https://github.com/harrifeng/redis-nodejs-demo#readme"
    }
    
  • 然后,安装链接redis所必须的"npm package redis"
    redis-nodejs-demo (master) $ npm install -S redis
    redis-nodejs-demo@1.0.0 /Users/hfeng/github/redis-nodejs-demo
    └─┬ redis@2.6.5
      ├── double-ended-queue@2.1.0-0
      ├── redis-commands@1.3.0
      └── redis-parser@2.3.0
    
  • 然后我们创建我们的index.js,里面的代码如下
    var redis = require("redis");
    var client = redis.createClient();
    client.set("my_key", "Hello World using Node.js and Redis");
    client.get("my_key", redis.print);
    client.quit();
    
    // <===================OUTPUT===================>
    // Reply: Hello World using Node.js and Redis
    

Redis data types

  • 对redis data type的理解,有助于提高你设计更好的application,同时还会让你了解 到你的问题最优的解决办法是不是redis
  • redis创建多种data type的原因很简单:一种size并不能使用所有的情况,而且不同的 问题需要不同的解决办法

Strings

  • 在redis里面,string是最'多变'的类型.会为多种情况服务:
    • string会被用来作为redis里面的integer, flot, text string,设置是bitmap
    • string还会用来存储各种各样的数据:text(XML, JSON, HTML, raw text), integer, float, 或者是binary data(video, image, audio).
    • string的最大存储容量是512MB
  • 如下几种情况是string最擅长的case:
    • Cache mechanisms: 可以在redis里面cache text或者binary数据,典型的text数据 是HTML page.典型的binary数据是API返回的image和video.一个最简单的cache系统 依赖SET,GET(MSET, MGET)就可以完成
    • Cache with automatic expiration: String和key自动过期机制(automatic key expiration)可以创建一个健壮的cache system. 相关的命令是SETX, EXPIRE, EXPIREAT. 在数据库查询需要时间过长(但是其结果却在某个特定时间内可以不变) 的情况下,这种处理特别有效.因为我们可以把"特定时间"设置过过期时间,在'不过 期'的情况下,可以尽可能少的查询数据库
    • Counting: redis里面的counter也是使用string类实现的(因为string能代表int么) 使用命令INCR INCRBY能够非常容易的增加counter.常见的counter运用的例子是page view, video view等等.相关的命令还有DECR, DECRBY, INCRFLOATBY

String examples with redis-cli