原文来自于:https://www.jianshu.com/p/4b60e43c7dd3
基本功能
- 缓存系统
- 数据存储
-
基本工具
服务启动工具
windows安装
下载地址:https://github.com/MicrosoftArchive/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.zip
解压到 C:\myprogram\redis并将这个目录配置到环境变量中打开 cmd 窗口运行 redis-server,不带参数默认为 redis-server redis.windows.confredis 默认端口 6379使用 redis-cli 命令进入对redis的编辑模式在远程服务上执行命令$ redis-cli -h host -p port -a password实例:连接到主机为 127.0.0.1,端口为 6379 ,密码为 mypass 的 redis 服务上$redis-cli -h 127.0.0.1 -p 6379 -a "mypass"redis 127.0.0.1:6379>
基本用法
C:\Users\Administrator>redis-cli127.0.0.1:6379> keys *1) "hello"127.0.0.1:6379> get hello"this is a value"
nodejs 中使用Redis
/* 值为字符串 */var redis = require('redis')var client = redis.createClient(6379, 'localhost')client.set('hello', 'this is a value')client.get('hello', (err, data) => {console.log('redis get hello err,data: ', err, data)})
PS D:\workspace\redisdemo> node .\set.jsredis get hello : this is a value
/* 值为对象类型 */var redis = require('redis')var client = redis.createClient(6379, 'localhost')// Object.prototype.toString = () => {// return JSON.stringify(this)// }client.set('hello', { a: 1, b: 2 })//client.set('hello', { a: 1, b: 2 }.toString())client.get('hello', (err, data) => {console.log('redis get hello err,data: ', err, data, typeof data)})
PS D:\workspace\redisdemo> node .\set.jsnode_redis: Deprecated: The SET command contains a argument of type Object.This is converted to "[object Object]" by using .toString() now and will return an error from v.3.0on.Please handle this in your code to make sure everything works as you intended it to.redis get hello err,data: null [object Object] string
基本使用
// client.jsvar redis = require('redis')module.exports = redis.createClient(6379,'localhost')
插入
// lists.jsvar client = require('./client')//从右边插入client.rpush('testlists', 'a')client.rpush('testlists', 'b')client.rpush('testlists', 'c')client.rpush('testlists', 1)// 从左往右读取,第一个到最后一个client.lrange('testlists', 0, -1, (err, lists) => {console.log('client.lrange, err, lists: ', err, lists)})
PS D:\workspace\redisdemo> node .\lists.jsclient.lrange, err, lists: null [ 'a', 'b', 'c', '1', 'a', 'b', 'c', '1' ]
PS D:\workspace\redisdemo> node .\lists.jsclient.lrange, err, lists: null [ '2', 'a', 'b', 'c', '1', 'a', 'b', 'c', '1', 'a', 'b', 'c', '1' ]
弹出
//从左边弹出client.lpop('testlists', (err, data) => {console.log('client.lpop, err, data: ', err, data)})//从右边弹出client.rpop('testlists', (err, data) => {console.log('client.rpop, err, data:', err, data)})
PS D:\workspace\redisdemo> node .\lists.jsclient.lpop, err, data: null 2client.rpop, err, data: null 1client.lrange, err, lists: null [ 'a', 'b', 'c', '1', 'a', 'b', 'c', '1', 'a', 'b', 'c' ]
集合操作
// sets.jsvar client = require('./client')client.sadd('testSet', 1)client.sadd('testSet', 'a')client.sadd('testSet', 'b')client.smembers('testSet',(err,data)=>{console.log('client.smembers, err, data: ', err, data)})
PS D:\workspace\redisdemo> node .\sets.jsclient.smembers, err, data: null [ 'a', 'b', '1' ]
// 再次运行还是这个值,不会追加插入PS D:\workspace\redisdemo> node .\sets.jsclient.smembers, err, data: null [ 'a', 'b', '1' ]
消息中介
// pub.jsvar client = require('./client')client.publish('testPublish', 'message from pub.js')
// sub.jsvar client = require('./client')client.subscribe('testPublish')client.on('message', (channel, msg) => {console.log('client.on message, channel: ', channel, ' message: ', msg)})
// 先订阅没有消息PS D:\workspace\redisdemo> node .\sub.js// 后发布消息PS D:\workspace\redisdemo> node .\pub.js// 监听到消息client.on message, channel: testPublish message: message from pub.js
Express 项目中 Redis 的代码组织
缓存系统
/* ./config/env/development.js */module.exports = {port:27017,mongodb:'mongodb://localhost/redisdemo',redis:'redis://localhost:6379'}
// .config/redis.jsconst redis = require('redis')const config = require('./config')module.exports = redis.createClient(config.redis)
// controller.jsvar redisClient = require('../../config/redis')const REDIS_NEWS_PREFIX = 'news_'var getNewFromMongo = function (id, cb) {console.log('run getNewsFormMongo')News.findOne({ _id: id }).exec(function (err, doc) {if (doc) {console.log('save mongo doc to redis')redisClient.set(REDIS_NEWS_PREFIX + id, JSON.stringify(doc))}return cb(err, doc)})}var getNewsFromRedis = function (id, cb) {console.log('run getNewsFromRedis')redisClient.get(REDIS_NEWS_PREFIX + id, function (err, v) {if (err) return cb(err, null)if (!v) {console.log('doc not in redis')return cb(null, null)}try {v = JSON.parse(v)} catch (e) {return cb(e, null)}console.log('get doc from redis')return cb(err, v)})}module.exports = {getBydId: function (req, res, next, id) {if (!id) return next(new Error('News not found'))getNewsFromRedis(id, function (err, doc) {if (err) return next(err)if (!doc) {getNewFromMongo(id, function (err, doc) {if (err) return next(err)if (!doc) {return next(new Error('News not found'))}req.news = docreturn next()})} else {req.news = docreturn next()}})}}
// 第一次运行app started,listening on port :3000run getNewsFromRedisdoc not in redisrun getNewsFromMongosave mongo doc to redis
//再运行请求一次run getNewsFromRedisget doc from redis
