2.6.0 • Published 3 years ago

@acheetahk/redis v2.6.0

Weekly downloads
-
License
BSD-2-Clause
Repository
github
Last release
3 years ago

codecov build npm npm.io npm.io

Please use version greater than 2.5.0, the clustering and redlock will be supported at v3.0.0 😄 ✨

Methods Nav

Installation

npm install @acheetahk/redis

cnpm install @acheetahk/redis

yarn add @acheetahk/redis

Dependencies

{
  "@types/ioredis": "4.17.7",
  "ioredis": "^4.17.3"
}

Usage

redis

import { BaseRedis } from '@acheetahk/redis';

const base = new BaseRedis(`<YOUR OPTIONS>`);

const redis = base.redis;

baseRedis - options

ParamTypeDefaultDescription
portnumber | string | Object6379Port of the Redis server, or a URL string(see the examples below), or the options object(see the third argument).
hoststring | Object"localhost"Host of the Redis server, when the first argument is a URL string, this argument is an object represents the options.
optionsObjectOther options.
options.portnumber6379Port of the Redis server.
options.hoststring"localhost"Host of the Redis server.
options.familystring4Version of IP stack. Defaults to 4.
options.pathstringnullLocal domain socket path. If set the port, host and family will be ignored.
options.keepAlivenumber0TCP KeepAlive on the socket with a X ms delay before start. Set to a non-number value to disable keepAlive.
options.noDelaybooleantrueWhether to disable the Nagle's Algorithm. By default we disable it to reduce the latency.
options.connectionNamestringnullConnection name.
options.dbnumber0Database index to use.
options.passwordstringnullIf set, client will send AUTH command with the value of this option when connected.
options.dropBufferSupportbooleanfalseDrop the buffer support for better performance. This option is recommended to be enabled when handling large array response and you don't need the buffer support.
options.enableReadyCheckbooleantrueWhen a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server not respond to any commands. To work around this, when this option is true, ioredis will check the status of the Redis server, and when the Redis server is able to process commands, a ready event will be emitted.
options.enableOfflineQueuebooleantrueBy default, if there is no active connection to the Redis server, commands are added to a queue and are executed once the connection is "ready" (when enableReadyCheck is true, "ready" means the Redis server has loaded the database from disk, otherwise means the connection to the Redis server has been established). If this option is false, when execute the command when the connection isn't ready, an error will be returned.
options.connectTimeoutnumber10000The milliseconds before a timeout occurs during the initial connection to the Redis server.
options.autoResubscribebooleantrueAfter reconnected, if the previous connection was in the subscriber mode, client will auto re-subscribe these channels.
options.autoResendUnfulfilledCommandsbooleantrueIf true, client will resend unfulfilled commands(e.g. block commands) in the previous connection when reconnected.
options.lazyConnectbooleanfalseBy default, When a new Redis instance is created, it will connect to Redis server automatically. If you want to keep the instance disconnected until a command is called, you can pass the lazyConnect option to the constructor: javascript var redis = new Redis({ lazyConnect: true }); // No attempting to connect to the Redis server here. // Now let's connect to the Redis server redis.get('foo', function () { });
options.tlsObjectTLS connection support. See https://github.com/luin/ioredis#tls-options
options.keyPrefixstring"''"The prefix to prepend to all keys in a command.
options.retryStrategyfunctionSee "Quick Start" section
options.maxRetriesPerRequestnumberSee "Quick Start" section
options.reconnectOnErrorfunctionSee "Quick Start" section
options.readOnlybooleanfalseEnable READONLY mode for the connection. Only available for cluster mode.
options.stringNumbersbooleanfalseForce numbers to be always returned as JavaScript strings. This option is necessary when dealing with big numbers (exceed the -2^53, +2^53 range).
options.enableAutoPipeliningbooleanfalseWhen enabled, all commands issued during an event loop iteration are automatically wrapped in a pipeline and sent to the server at the same time. This can improve performance by 30-50%.
options.autoPipeliningIgnoredCommandsstring[][]The list of commands which must not be automatically wrapped in pipelines.
options.maxScriptsCachingTimenumber60000Default script definition caching time.

onLock

import { BaseRedis } from '@acheetahk/redis';

const base = new BaseRedis(`<YOUR OPTIONS>`);

const [test, result] = await Promise.all([
  base.onLock('onLockTest', 5),
  base.onLock('onLockTest'),
]);

// result = false

onLock - args

ParamTypeDescription
keystringstring mutex lock key
ttlnumbermutex lock expiration time, units are seconds

freedLock

import { BaseRedis } from '@acheetahk/redis';

const base = new BaseRedis(`<YOUR OPTIONS>`);

const result1 = await base.onLock('onLockTest', 1000); // true
const result2 = await base.onLock('onLockTest'); // false
await base1.freedLock('onLockTest');
const result3 = await base1.onLock('onLockTest'); // true

freedLock - args

ParamTypeDescription
keystringstring mutex lock key

lock

import { BaseRedis } from '@acheetahk/redis';

const base = new BaseRedis(`<YOUR OPTIONS>`);

class Demo {
  @base.lock('key', 5, true)
  async doSomeThings(args: any) {
    const result = await `<YOUR PROMISE FUNCTION>`(args);
    return result;
  }
}

const demo = new Demo();

// success
try {
  const result = await demo.doSomeThings({ key: 'value'});
} catch (error) {
  // console.log(error);
}

// error
try {
  await Promise.all([
    demo.doSomeThings({ key: 'value'}),
    demo.doSomeThings({ key: 'value'})
  ]);
} catch (error) {
  // Error: Frequent operation'
}

lock - args

ParamTypeDescription
keystringstring mutex lock key
ttlnumbermutex lock expiration time, units are seconds
isAutobooleanwhether to automatically serialization args according to the bound function parameters, the default is false
messagestringfailure information, the default is Frequent operation

cache

import { BaseRedis } from '@acheetahk/redis';

let count = 0;
const base = new BaseRedis(`<YOUR OPTIONS>`);

class Demo {
  @base.cache('cacheKey', 3600, true) // 1h
  doSomeThings(num: number) {
    return num + count;
  }
}

const demo = new Demo();

const result = await demo.doSomeThings(1); // 1
const result1 = await demo.doSomeThings(2); // 1

await base.redis.del('cacheKey');
const result2 = await demo.doSomeThings(2); // 2

cache - args

ParamTypeDescription
keystringstring cache key
ttlnumberexpiration time, units are seconds
isAutobooleanwhether to automatically serialization args according to the bound function parameters, the default is false

hashCache

import { BaseRedis } from '@acheetahk/redis';

let count = 0;

const base = new BaseRedis(`<YOUR OPTIONS>`);

class Demo {
  @base.hashCache('hkey') // 1h
  doSomeThings(num: number) {
    return num + count;
  }
}

const demo = new Demo();

const result = await demo.doSomeThings(1); // 1
const result1 = await demo.doSomeThings(2); // 1

await base.redis.hdel('hkey', autoMd5([1]));
const result2 = await demo.doSomeThings(2); // 2

hashCache - args

ParamTypeDescription
hkeystringhash cache key

toLock

import { BaseRedis } from '@acheetahk/redis';
const base = new BaseRedis(`<YOUR OPTIONS>`);

const getResult = async () => { return await doSomeThings() };

try {
  await Promise.all([
    toLock(base, 'toLock', getResult, 5).withArgs(),
    toLock(base, 'toLock', getResult, 5).withArgs(),
  ]);
} catch (error) {
  // Error: Frequent operation
}

toLock - args

ParamTypeDescription
baseRedisBaseRedisinstance by BaseRedis
keystringstring mutex lock key
nextFunctionfunction to be executed
ttlnumbermutex lock expiration time, units are seconds
messagestringfailure information, the default is Frequent operation

getCache

import { BaseRedis } from '@acheetahk/redis';
const base = new BaseRedis(`<YOUR OPTIONS>`);

let info = 'before ';
const getResult = async (num: string, num2: string) => {
  const result = await doSomeThings();
  const value = info + result + num + num2;
  return value;
};

const result1 = await getCache(base, 'getCache', getResult, 10000).withArgs(123, 234); // 'before success123234';

info = 'now ';
const result2 = await getCache(base, 'getCache', getResult, 10000).withArgs(123, 234); // 'before success123234';

await base.redis.del('getCache');
const result3 = await getCache(base, 'getCache', getResult, 10000).withArgs(123, 234); // 'now success123234';

getCache - args

ParamTypeDescription
baseRedisBaseRedisinstance by BaseRedis
keystringstring mutex lock key
nextFunctionfunction to be executed
ttlnumberexpiration time, units are seconds

getHashCache

import { BaseRedis } from '@acheetahk/redis';
const base = new BaseRedis(`<YOUR OPTIONS>`);

let info = 'before ';

const getResult = async (num: string, num2: string) => {
  const result = await doSomeThings();
  const value = info + result + num + num2;
  return value;
};

const result1 = await getHashCache(base, 'getHashCache', 'key', getResult).withArgs(123, 234); // 'before success123234'

info = 'now ';
const result2 = await getHashCache(base, 'getHashCache', 'key', getResult).withArgs(123, 234); // 'before success123234'

await base.redis.hdel('getHashCache', 'key');
const result4 = await getHashCache(base, 'getHashCache', 'key', getResult).withArgs(123, 234); // 'now success123234'

await base.redis.del('getHashCache');

getHashCache - args

ParamTypeDescription
baseRedisBaseRedisinstance by BaseRedis
hkeystringhash cache key
keystringhash table key
nextFunctionfunction to be executed
2.6.0

3 years ago

2.5.2

3 years ago

2.5.3

3 years ago

2.5.1

3 years ago

2.5.0

3 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

1.21.0

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.0

3 years ago

1.17.1

3 years ago

1.16.0

3 years ago

1.15.1

3 years ago

1.15.0

3 years ago

1.14.0

3 years ago

1.13.0

3 years ago

1.12.0

3 years ago

1.9.0

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago