基本功能
基本工具
- 服务启动工具
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.conf
redis 默认端口 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-cli
127.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.js
redis 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.js
node_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.0
on.
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
基本使用
列表操作
连接 redis
// client.js
var redis = require('redis')
module.exports = redis.createClient(6379,'localhost')
插入
// lists.js
var 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.js
client.lrange, err, lists: null [ 'a', 'b', 'c', '1', 'a', 'b', 'c', '1
' ]
PS D:\workspace\redisdemo> node .\lists.js
client.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.js
client.lpop, err, data: null 2
client.rpop, err, data: null 1
client.lrange, err, lists: null [ 'a', 'b', 'c', '1', 'a', 'b', 'c', '1
', 'a', 'b', 'c' ]
集合操作
// sets.js
var 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.js
client.smembers, err, data: null [ 'a', 'b', '1' ]
// 再次运行还是这个值,不会追加插入
PS D:\workspace\redisdemo> node .\sets.js
client.smembers, err, data: null [ 'a', 'b', '1' ]
消息中介
// pub.js
var client = require('./client')
client.publish('testPublish', 'message from pub.js')
// sub.js
var 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.js
const redis = require('redis')
const config = require('./config')
module.exports = redis.createClient(config.redis)
// controller.js
var 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 = doc
return next()
})
} else {
req.news = doc
return next()
}
})
}
}
// 第一次运行
app started,listening on port :3000
run getNewsFromRedis
doc not in redis
run getNewsFromMongo
save mongo doc to redis
//再运行请求一次
run getNewsFromRedis
get doc from redis
想要查看更多文章,敬请关注 dravenxiaokai 的
哇,写的太赞了,别拦着我打赏 o( ̄︶ ̄)o