redis概览

1.首先进入到redis安装目录,cmd运行 redis-server,开启一个redis

2.另外打开一个面板,运行 redis-cli.exe -h 127.0.0.1 -p 6379 启动redis对6379端口的接受

3.然后是在nodejs运行的js,首先安装redis,然后链接redis。

4.redis存取数据的方式

var redis = require ('redis')
var client = redis.createClient(6379, '127.0.0.1')
client.on('error', function (err) {
    console.log('Err ' + err)
})

client.set('color', 'red', redis.print)
client.get('color', function (err, value) {
    if (err) throw err;
    console.log('get: ' + value)
})
client.hmset('camping', {
    'shelter': '2-person tent',
    'cooking': 'campstove'
}, redis.print)
client.hget('camping', 'cooking', function (err, value) {
    if(err) throw err
    console.log('will be cooking with: ' + value)

})
client.hkeys('camping', function (err, keys) {
    if(err) throw err
    keys.forEach(function(key, i) {
        console.log( ' 00' + key)
    })
})
client.lpush('tasks', 'aaaaaa', 'bbbbb', 'cccccccc')
client.lpush('tasks', 'eeeee', 'fffffff', 'ggggggggg')
client.lrange('tasks', 0, -1, function (err, item) {
    if(err) throw err
    console.log(' ' + item)
})

client.sadd('ipadd', '204.10.0.0', redis.print)
client.sadd('ipadd', '204.10.0.0', redis.print)
client.sadd('ipadd', '204.10.0.2', redis.print)

client.smembers('ipadd', function (err, members) {
    if(err) throw err
    console.log(members)
})

随机浏览