原文来自于:https://www.jianshu.com/p/4b60e43c7dd3

基本功能

  1. 解压到 C:\myprogram\redis
  2. 并将这个目录配置到环境变量中
  3. 打开 cmd 窗口运行 redis-server
  4. 不带参数默认为 redis-server redis.windows.conf
  5. redis 默认端口 6379
  6. 使用 redis-cli 命令进入对redis的编辑模式
  7. 在远程服务上执行命令
  8. $ redis-cli -h host -p port -a password
  9. 实例:连接到主机为 127.0.0.1,端口为 6379 ,密码为 mypass redis 服务上
  10. $redis-cli -h 127.0.0.1 -p 6379 -a "mypass"
  11. redis 127.0.0.1:6379>

基本用法

  1. C:\Users\Administrator>redis-cli
  2. 127.0.0.1:6379> keys *
  3. 1) "hello"
  4. 127.0.0.1:6379> get hello
  5. "this is a value"

nodejs 中使用Redis

  1. /* 值为字符串 */
  2. var redis = require('redis')
  3. var client = redis.createClient(6379, 'localhost')
  4. client.set('hello', 'this is a value')
  5. client.get('hello', (err, data) => {
  6. console.log('redis get hello err,data: ', err, data)
  7. })
  1. PS D:\workspace\redisdemo> node .\set.js
  2. redis get hello : this is a value
  1. /* 值为对象类型 */
  2. var redis = require('redis')
  3. var client = redis.createClient(6379, 'localhost')
  4. // Object.prototype.toString = () => {
  5. // return JSON.stringify(this)
  6. // }
  7. client.set('hello', { a: 1, b: 2 })
  8. //client.set('hello', { a: 1, b: 2 }.toString())
  9. client.get('hello', (err, data) => {
  10. console.log('redis get hello err,data: ', err, data, typeof data)
  11. })
  1. PS D:\workspace\redisdemo> node .\set.js
  2. node_redis: Deprecated: The SET command contains a argument of type Object.
  3. This is converted to "[object Object]" by using .toString() now and will return an error from v.3.0
  4. on.
  5. Please handle this in your code to make sure everything works as you intended it to.
  6. redis get hello err,data: null [object Object] string

基本使用

  • 存储和获取值
  • 列表
  • 集合
  • 消息中介

    列表操作

    连接 redis

  1. // client.js
  2. var redis = require('redis')
  3. module.exports = redis.createClient(6379,'localhost')

插入

  1. // lists.js
  2. var client = require('./client')
  3. //从右边插入
  4. client.rpush('testlists', 'a')
  5. client.rpush('testlists', 'b')
  6. client.rpush('testlists', 'c')
  7. client.rpush('testlists', 1)
  8. // 从左往右读取,第一个到最后一个
  9. client.lrange('testlists', 0, -1, (err, lists) => {
  10. console.log('client.lrange, err, lists: ', err, lists)
  11. })
  1. PS D:\workspace\redisdemo> node .\lists.js
  2. client.lrange, err, lists: null [ 'a', 'b', 'c', '1', 'a', 'b', 'c', '1
  3. ' ]
  1. PS D:\workspace\redisdemo> node .\lists.js
  2. client.lrange, err, lists: null [ '2', 'a', 'b', 'c', '1', 'a', 'b', 'c
  3. ', '1', 'a', 'b', 'c', '1' ]

弹出

  1. //从左边弹出
  2. client.lpop('testlists', (err, data) => {
  3. console.log('client.lpop, err, data: ', err, data)
  4. })
  5. //从右边弹出
  6. client.rpop('testlists', (err, data) => {
  7. console.log('client.rpop, err, data:', err, data)
  8. })
  1. PS D:\workspace\redisdemo> node .\lists.js
  2. client.lpop, err, data: null 2
  3. client.rpop, err, data: null 1
  4. client.lrange, err, lists: null [ 'a', 'b', 'c', '1', 'a', 'b', 'c', '1
  5. ', 'a', 'b', 'c' ]

集合操作

  1. // sets.js
  2. var client = require('./client')
  3. client.sadd('testSet', 1)
  4. client.sadd('testSet', 'a')
  5. client.sadd('testSet', 'b')
  6. client.smembers('testSet',(err,data)=>{
  7. console.log('client.smembers, err, data: ', err, data)
  8. })
  1. PS D:\workspace\redisdemo> node .\sets.js
  2. client.smembers, err, data: null [ 'a', 'b', '1' ]
  1. // 再次运行还是这个值,不会追加插入
  2. PS D:\workspace\redisdemo> node .\sets.js
  3. client.smembers, err, data: null [ 'a', 'b', '1' ]

消息中介

  1. // pub.js
  2. var client = require('./client')
  3. client.publish('testPublish', 'message from pub.js')
  1. // sub.js
  2. var client = require('./client')
  3. client.subscribe('testPublish')
  4. client.on('message', (channel, msg) => {
  5. console.log('client.on message, channel: ', channel, ' message: ', msg)
  6. })
  1. // 先订阅没有消息
  2. PS D:\workspace\redisdemo> node .\sub.js
  3. // 后发布消息
  4. PS D:\workspace\redisdemo> node .\pub.js
  5. // 监听到消息
  6. client.on message, channel: testPublish message: message from pub.js

Express 项目中 Redis 的代码组织

缓存系统

  1. /* ./config/env/development.js */
  2. module.exports = {
  3. port:27017,
  4. mongodb:'mongodb://localhost/redisdemo',
  5. redis:'redis://localhost:6379'
  6. }
  1. // .config/redis.js
  2. const redis = require('redis')
  3. const config = require('./config')
  4. module.exports = redis.createClient(config.redis)
  1. // controller.js
  2. var redisClient = require('../../config/redis')
  3. const REDIS_NEWS_PREFIX = 'news_'
  4. var getNewFromMongo = function (id, cb) {
  5. console.log('run getNewsFormMongo')
  6. News
  7. .findOne({ _id: id })
  8. .exec(function (err, doc) {
  9. if (doc) {
  10. console.log('save mongo doc to redis')
  11. redisClient.set(REDIS_NEWS_PREFIX + id, JSON.stringify(doc))
  12. }
  13. return cb(err, doc)
  14. })
  15. }
  16. var getNewsFromRedis = function (id, cb) {
  17. console.log('run getNewsFromRedis')
  18. redisClient.get(REDIS_NEWS_PREFIX + id, function (err, v) {
  19. if (err) return cb(err, null)
  20. if (!v) {
  21. console.log('doc not in redis')
  22. return cb(null, null)
  23. }
  24. try {
  25. v = JSON.parse(v)
  26. } catch (e) {
  27. return cb(e, null)
  28. }
  29. console.log('get doc from redis')
  30. return cb(err, v)
  31. })
  32. }
  33. module.exports = {
  34. getBydId: function (req, res, next, id) {
  35. if (!id) return next(new Error('News not found'))
  36. getNewsFromRedis(id, function (err, doc) {
  37. if (err) return next(err)
  38. if (!doc) {
  39. getNewFromMongo(id, function (err, doc) {
  40. if (err) return next(err)
  41. if (!doc) {
  42. return next(new Error('News not found'))
  43. }
  44. req.news = doc
  45. return next()
  46. })
  47. } else {
  48. req.news = doc
  49. return next()
  50. }
  51. })
  52. }
  53. }
  1. // 第一次运行
  2. app startedlistening on port :3000
  3. run getNewsFromRedis
  4. doc not in redis
  5. run getNewsFromMongo
  6. save mongo doc to redis
  1. //再运行请求一次
  2. run getNewsFromRedis
  3. get doc from redis