Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis配置 #31

Open
Wscats opened this issue Feb 28, 2018 · 0 comments
Open

redis配置 #31

Wscats opened this issue Feb 28, 2018 · 0 comments

Comments

@Wscats
Copy link
Owner

Wscats commented Feb 28, 2018

安装

在这个地址里下载redis的压缩文件github下载地址解压到C:\redis目录下

image

运行

C:\redis该目录下打开cmd命令执行一下命令启动redis服务

redis-server.exe redis.windows.conf

image

测试

这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了,切换到redis目录下运行一下命令

redis-cli.exe -h 127.0.0.1 -p 6379

设置键值对

set myKey abc

取出键值对

get myKey

image

配置node_redis

安装redis模块

npm install redis

配置和连接

var redis = require("redis");
var client = redis.createClient();//默认localhost和6379端口
//相当于var client  = redis.createClient('6379', '127.0.0.1');
//监听错误
client.on("error", function(err) {
	console.log("Error " + err);
});

设置

方法 作用
client.hmset("key",obj,function(err) {} 为一个Key一次设置多个哈希键/值,多用于JSON对象的写入
client.hgetall("key",function(err, object) {}) 读取一个Key的所有哈希键/值,多用于JSON对象读取

读写JSON的例子

//写入JavaScript(JSON)对象
client.hmset('sessionid', {
	username: 'wscats',
	password: 'password'
}, function(err) {
	console.log("错误", err)
})

//读取JavaScript(JSON)对象
client.hgetall('sessionid', function(err, object) {
	console.log(object)
})

时效性

30s后sessionid将自动删除

client.expire('sessionid', 30);

这里如果要写定时清理数据,有两个比较有用的时间计算js 取得当天0点 / 23:59:59 时间

可视化

可以安装redis可视化工具方便管理redis数据库
image

参考文章

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant