1.1.1 • Published 1 year ago

@comparaonline/idempotence-lib v1.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
1 year ago

SetUp

Initialize redis with the provided utility

import { RedisConfig } from '@comparaonline/idempotence-lib/redis-config';
import { initializeIdempotenceRedis } from '@comparaonline/idempotence-lib/initialize-idempotence-redis';

const config: RedisConfig = {
  host: 'localhost'
};

initializeIdempotenceRedis(config);

Usage

executeWithStringKey

Signature:

executeWithStringKey<T = any>(key: string, fn: () => T, expireTime: number): Promise<T | undefined>
ParamTypeDescription
keystringThe key to use to verify uniqueness.
fnfunctionThe function to execute in case us the first time the key is used.
expireTimenumberThe expiration time in seconds to keep the result asociated to the key. By default is 10 days.

The result will be undefined if the function is called again with the same key and it didn't obtain the result for the first call yet.

Use example:

import { IdempotenceManager } from '@comparaonline/idempotence-lib/idempotence-manager';

const myFn = async (event: any) => {
  const result = IdempotenceManager.executeWithStringKey(event.id, () => {
    // process Event
    return Math.random();
  });
  console.log(result);
};

executeWithObjectKey

Signature:

executeWithObjectKey<T = any>(objToHash: any, fn: () => T, expireTime: number): Promise<T | undefined>
ParamTypeDescription
objToHashanyThe object that will be hashed to a unique key.
fnfunctionThe function to execute in case us the first time the key is used.
expireTimenumberThe expiration time in seconds to keep the result asociated to the key. By default is 10 days.

The result will be undefined if the function is called again with the same key and it didn't obtain the result for the first call yet.

Use example:

import { IdempotenceManager } from '@comparaonline/idempotence-lib/idempotence-manager';

const myFn = async (event: any) => {
  const result = IdempotenceManager.executeWithObjectKey(event, () => {
    // process Event
    return Math.random();
  });
  console.log(result);
};

removeStrokesByObjectKey

Signature:

removeStrokesByObjectKey(objToHash: any): Promise<void>
ParamTypeDescription
objToHashanyThe object that will be hashed to a unique key.

The function removes the record associated with a given object from the Redis database.

Use example:

import { IdempotenceManager } from '@comparaonline/idempotence-lib';

const myFn = async (event: any) => {
  IdempotenceManager.removeStrokesByObjectKey(event);
};
1.1.1

1 year ago

1.1.0

1 year ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago