egg-xc-redis

1.1.7 • Public • Published

egg-xc-redis

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-xc-redis --save

Usage

// {app_root}/config/plugin.js
exports.xcRedis = {
  enable: true,
  package: 'egg-xc-redis',
};

Configuration

// {app_root}/config/config.default.js
exports.xcRedis = {
  {
  clients: {
    db1: { // instanceName. See below
      port: 6379, // Redis port
      host: 'localhost', // Redis host
      password: '',
      db: 1,
    },
    db2: {
      port: 6379,
      host: 'localhost',
      password: '',
      db: 2,
    },
  },
};
针对配置可覆盖

see config/config.default.js for more detail. ##API

 //获取redis链接 默认配置为db1
app.redis.getConn()

//获取链接实例 db为 db1,db2这种key值
app.redis.getConnInstance(db)

//释放redis链接 conn为redis链接
app.redis.doRelease(conn)

//统一设置赋值 conn为redis链接  key为键  
//value为string或object 
//value为string时
//subvalue为undefined则value为存储的值
//subvalue不为undefined则value为key对应对象的键,subvalue为该键的值
app.redis.set(conn, key, value,subvalue)

//统一获取值 conn为redis链接  key为键
// isObject和objectKey为undefined 时获取字符串值
// isObject为true,objectKey为undefined 时获取对象
// isObject为true,objectKey不为为undefined 时获取对象中objectKey对应的值
app.redis.get(conn, key,isObject, objectKey) 

 // 设置一个key的过期的秒数
app.redis.expire(conn, key, seconds)

// 查询一个key是否存在
app.redis.exists(conn, key)

//删除key
app.redis.del(conn, key)

//clear all keys
app.redis.flushdb(conn)

Example

赋值获取值

    const conn = app.redis.getConn();
    await app.redis.set(conn, 'name', 'xiaoming');
    const result = await app.redis.get(conn, 'name');
    assert.equal(result, 'xiaoming');
    app.redis.doRelease(conn);

赋值获取对象

   const conn = app.redis.getConn();
    await app.redis.set(conn, 'nameObj', { age: 12, job: 'programer' });
    const result = await app.redis.get(conn, 'nameObj', true,'job');
    app.logger.info('shoud get result get obj', result);
    assert.equal(result, 'programer');
    app.redis.doRelease(conn);

Questions & Suggestions

Please open an issue here.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i egg-xc-redis

Weekly Downloads

3

Version

1.1.7

License

MIT

Unpacked Size

14.4 kB

Total Files

15

Last publish

Collaborators

  • xudeping