1.0.1 • Published 4 years ago

@huzapi/cache-connector v1.0.1

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
4 years ago

#Huz Api / Cache Connector

Redis implementation with promise and async support NOT COMPLETED YET

  • TDD (Test driven)
  • All parameters casted
  • All results are casted
  • All exceptions are handled
  • Fully JsDoc
  • index.d.ts included (built-in @types support)

Test (100% TDD)

npm test

Install to your project

npm i @huzapi/cache-connector

Options

const {RedisOption} = require('@huzapi/cache-connector');
// Default ttl time for set, if not set then, default value: 3600 (a hour)
// Signature (ttl: number = 3600)
RedisOption.setDefDurationSec(60 * 60 * 12);

// if you need to keep redis get statistics then set a callback function
// Signature (fn: (req: Express.Request, count: number) => void)
RedisOption.setGetCountFn(async (req, count) => {
    //change here
    YourLocalStorage.redisGetCount += count;
}); 

// if you need to keep redis set statistics then set a callback function
// Signature (fn: (req: Express.Request, count: number) => void)
RedisOption.setSetCountFn(async (req, count) => {
    //change here
    YourLocalStorage.redisSetCount += count;
}); 

// if you need to keep redis error statistics then set a callback function
// Signature (fn: (req: Express.Request, count: number) => void)
RedisOption.setErrorCountFn(async (req, count) => {
    //change here
    YourLocalStorage.redisErrorCount += count;
}); 

Initialization

const {RedisConnect} = require('@huzapi/cache-connector');
//You can register one or more servers

// register a mandatory server
// Not: If connection problems then it throws an error
// Signature (host: string = 'localhost', port: number = 6379, server-name: string = 'default') => RedisInstance
RedisConnect.mandatoryServer('localhost', 6379, 'default');

// register a optional server
// Not: If connection problems then it ignores cache-related operations
// Signature (host: string = 'localhost', port: number = 6379, server-name: string = 'default') => RedisInstance
RedisConnect.mandatoryServer('another-host', 6379, 'another');

// Get a server as RedisInstance instance
// Signature (name: string = 'default') => RedisInstance
RedisConnect.server('default');
RedisConnect.server(); // it same above, default of server-name is 'default' 

// Check a server exists or not
// Signature (name: string) => boolean
RedisConnect.hasServer('default'); // returns true
RedisConnect.hasServer('foo bar'); // returns false

// List registered server names
// Signature () => Array<string>
RedisConnect.serverNames(); // returns ['default', 'another']

Key Commands

const {RedisConnect} = require('@huzapi/cache-connector');

// Sets a timeout on key
// Signature (req: Express.Request, key: string, ttl?: number) => boolean
// Returns true if success
await RedisConnect.server().expireAsync(req, 'your key', 60 * 60 * 24); //default server
await RedisConnect.server('another').expireAsync(req, 'your key'); // use default ttl, ie: RedisOption.setDefDurationSec

// Fetches the remaining time to live of a key that has a timeout
// Signature (req: Express.Request, key: string) => number
// Note: returns >= 0: seconds, -2: key does not exist, -1: key exists but has no associated expire
await RedisConnect.server().ttlAsync(req, 'your-key');

// Removes the specified keys
// Signature (req: Express.Request, ...keys: Array<string>) => number
// Note: returns the number of keys that were removed
await RedisConnect.server().delAsync(req, 'your key');
await RedisConnect.server().delAsync(req, 'your key', 'another-key');

// Sets value of key with ttl
// Signature (req: Express.Request, key: string, value: any, ttl?: number) => string
// Note: returns "Ok" if success
// Note: null or undefined values will be ignored, not-saved
await RedisConnect.server().setexAsync(req, 'your-object', {a: "a"}, 3600);
await RedisConnect.server().setexAsync(req, 'your-array', ["a","b"], 3600);
await RedisConnect.server().setexAsync(req, 'your-text', "foo bar", 3600);
await RedisConnect.server().setexAsync(req, 'your-number', 54.66, 3600);
await RedisConnect.server().setexAsync(req, 'your-boolean', true, 3600);

// Sets value of key without any associated ttl
// Signature (req: Express.Request, key: string, value: any) => string
// Note: returns "Ok" if success
// Note: null or undefined values will be ignored, not-saved
await RedisConnect.server().setAsync(req, 'your-object', {a: "a"});
await RedisConnect.server().setAsync(req, 'your-array', ["a","b"]);
await RedisConnect.server().setAsync(req, 'your-text', "foo bar");
await RedisConnect.server().setAsync(req, 'your-number', 54.66);
await RedisConnect.server().setAsync(req, 'your-boolean', true);

// Fetches the value of key
// Signature (req: Express.Request, key: string) => any
await RedisConnect.server().getAsync(req, 'your-object'); // {a: "a"}
await RedisConnect.server().getAsync(req, 'your-array'); // ["a","b"]
await RedisConnect.server().getAsync(req, 'your-text'); // "foo bar"
await RedisConnect.server().getAsync(req, 'your-number'); // 54.66
await RedisConnect.server().getAsync(req, 'your-boolean'); // true

// Fetches keys with pattern
// Signature (req: Express.Request, pattern: string) => Array<string>
await RedisConnect.server().keysAsync(req, 'your-*'); // ['your-object', 'your-array', 'your-text', 'your-number', 'your-boolean'] 

Hash Commands

const {RedisConnect} = require('@huzapi/cache-connector');

// Fetches all fields/values of hash
// Signature (req: Express.Request, key: string) => Record<string, any>
await RedisConnect.server().hgetallAsync(req, 'your-hash');

// Sets some fields of hash
// Signature (req: Express.Request, key: string) => any
// Note: returns "Ok" if success
// Note: null or undefined values will be ignored, not-saved
await RedisConnect.server().hmsetAsync(req, 'your-hash', {field1: "value1", field2: 5}, 3600);

// Removes the specified fields from the hash stored at key
// Signature (req: Express.Request, key: string, ...members: Array<string>) => number
// Note: Returns the number of fields that were removed from the hash
await RedisConnect.server().hdelAsync(req, 'your-hash', 'field1', 'field2');

// Increments the number stored at field in the hash stored at key by increment
// Signature (req: Express.Request, key: string, member: string, increment: number) => number
// Note: Returns the value at field after the increment operation
// Note: key or member are absent, it will be created
await RedisConnect.server().hincrbyAsync(req, 'your-hash', 'field2', 1);
await RedisConnect.server().hincrbyfloatAsync(req, 'your-hash', 'field2', 1.3);

Set Commands

@TODO

1.0.1

4 years ago