node

redis使用(上手)

# 本地电脑安装

1. 下载
https://github.com/tporadowski/redis/releases

2. 启动
点击安装地址里面的 redis-cli.exe

# 在 node 中使用(入门级)

const redis = require('redis')
const client = redis.createClient(6379, '127.0.0.1')
client.on('ready', function (res) {
  console.log('ready')
})

client.on('error', err => {
  console.log(err, 'err')
})

client.set('str1', 'str1wewe', redis.print)
client.get('str1', function (err, reply) {
  console.log(reply, 'reply')
})

// 用Hash存取对象
/* client.hmset("ikc",{
  'itme': 'koadeom',
  'id': 666
})

client.hgetall("ikc", function(err, reply){
  console.log(reply,'reply');
}) */
// 用Hash存取对象

# 在 node 中使用(简单连接池)

https://blog.csdn.net/weixin_36541072/article/details/53944653?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

npm install redis-connection-pool

const redis = require('redis')
let redisPool = require('redis-connection-pool')('myRedisPool', {
    host: '127.0.0.1', // default
    port: 6379, //default
    max_clients: 30, // defalut
    perform_checks: false, // checks for needed push/pop functionality
    database: 0, // database number to use
    options: {
      auth_pass: 'password'
    } //options for createClient of node-redis, optional
  });

redisPool.set('test-key', 'foobar', function (err) {
  redisPool.get('test-key', function (err, reply) {
    console.log(reply); // 'foobar'
  });
});

上次更新: