lscache-dev-beta
v0.0.12
Published
Develop and deploy applications with the LSQ Cache module, the module provides first class TypeScript support and makes it easy to call LSQ Cache API services.
Downloads
16
Readme
LSQ-Cache NPM module
Develop and deploy applications with the LSQ Cache module, the module provides first-class TypeScript support and makes it easy to call LSQ Cache API services.
Getting Started
Assuming that you have Node JS (Recommended is 16.x or 18.x) setup on your machine, install lsq-cache
by running below command
npm install lsq-cache
Usage
- For LSQ Lapp development, import the
lsq-cache
before the main method as shown below.
const CacheApp = require('lsq-cache').CacheApp;
function main(queryString, body, callback) {
(async function () {
try {
const myCacheApp = new CacheApp(
'<cache token here>', // refer 'Initialize the module' section to obtain the token
'21', // values could be 1, 11, 21, 31
);
const setItemRes = await myCacheApp.item.set('k1', 'v1', 60 * 20);
console.log(setItemRes);
const getItemRes = await myCacheApp.item.get('k1');
console.log(getItemRes);
callback(null, {
setItemRes,
getItemRes,
});
} catch (error) {
console.log(error);
}
})();
}
- For non lapp development, please import the module to create a new instance of the cache app.
const CacheApp = require('lsq-cache').CacheApp;
async function main() {
try {
const myCacheApp = new CacheApp(
'<cache token here>', // refer 'Initialize the module' section to obtain the token
'21', // values could be 1, 11, 21, 31
);
const setItemRes = await myCacheApp.item.set('k1', 'v1', 60 * 20);
console.log(setItemRes);
const getItemRes = await myCacheApp.item.get('k1');
console.log(getItemRes);
} catch (error) {
console.log(error);
}
}
main();
Initialize the module
To use the module, user is required to pass the following arguments to the CacheApp
class constructor:
- Token - Please contact the LeadSquared support team for enabling cache service for the tenant and obtaining the token.
- Region - Valid values are
1, 11, 21, 31
(1 for SGP, 11 for US, 21 for MUM and 31 for IR)
Data Types
Item
get(key_name)
For the key, gets the value if it exists or else returns null
Arguments
- key_name (string | number): Name of the key
Example:
myCacheApp.item.get('key_name'); // returns string
set(key_name, key_value, ttl)
For the key, sets (overrides if already present) a value, Optionally a time to live (TTL) attribute can be set to automatically expire the key and its value
Arguments
- key_name (string | number): Name of the key
- key_value (any): Value of the key
- ttl (number): Time to live (seconds), this argument is optional
Example:
myCacheApp.item.set('key_name', 'key_value'); // returns boolean
// with TTL
myCacheApp.item.set('key_name', 'key_value', 600); // returns boolean
exists(key_name)
For the key, checks if a value exists by returning 1 or else returns 0
Arguments
- key_name (string | number): Name of the key
Example:
myCacheApp.item.exists('key_name'); // returns 0 if key doesnt exist else 1
getExpiry(key_name)
For the key, gets the expiry lifetime (in seconds) for it
Arguments
- key_name (string | number): Name of the key
Example:
myCacheApp.item.getExpiry('key_name'); // returns number
setExpireAt(key_name, ttl)
For the key, sets the expiry lifetime (in seconds) for it
Arguments
- key_name (string | number): Name of the key
- ttl (number): Time to live (seconds)
Example:
myCacheApp.item.setExpireAt('key_name', 600); // returns boolean
increment(key_name, incrBy)
For the key, increments the value and returns the incremented value. Optionally a custom increment value can be provided or else the default value is 1
Arguments
- key_name (string | number): Name of the key
- incrBy (number): Increment, this argument is optional
Example:
myCacheApp.item.increment('key_name'); // returns number
// with custom incrBy
myCacheApp.item.increment('key_name', 3); // returns number
decrement(key_name, decrBy)
For the key, decrements the value and returns the decremented value. Optionally a custom decrement value can be provided or else the default is 1
Arguments
- key_name (string | number): Name of the key
- decrBy (number): Decrement, this argument is optional
Example:
myCacheApp.item.decrement('key_name'); // returns number
// with custom decrBy
myCacheApp.item.decrement('key_name', 3); // returns number
delete(key_name)
For the key, deletes the key if it exists and returns 1 if key is deleted else 0
Arguments
- key_name (string | number): Name of the key
Example:
myCacheApp.item.delete('key_name'); // returns number
scan(page_index)
For the page index, returns the list of keys created in the namespace along with the page index of the next set of results
NOTE:
- The first page will begin with pageIndex
0
- This method should be called recursively until you get the same page index consecutively to get the complete list of items
Arguments
- page_index (number): Page Index
Example:
myCacheApp.item.scan(10); // returns { "nextIndex": number, "resultSet": string[] }
Set
add(key_name, key_value)
For the key, adds an item (i.e. the key_value argument) to a cache app set data structure
NOTE: Sets cannot contain duplicate records
Arguments
- key_name (string | number): Name of the set
- key_value (any): Item to be added to the set
Example:
myCacheApp.set.add('key_name', 'key_value'); // returns number
get(key_name)
For the key, gets all the items in the form of a array of string (i.e. a list data structure is returned)
Arguments
- key_name (string | number): Name of the set
Example:
myCacheApp.set.get('key_name'); // returns list (array of string)
exists(key_name, key_value)
For the key, checks if the key_value is a member in the cache app set data structure
Arguments
- key_name (string | number): Name of the key
- key_value (any): Item to be added to the set
Example:
myCacheApp.set.exists('key_name', 'key_value'); // returns boolean
delete(key_name, key_value)
For the key, removes the key_value from the cache app set data structure if it exists
Arguments
- key_name (string | number): Name of the key
- key_value (any): Item to be removed fromthe set
Example:
myCacheApp.set.delete('key_name', 'key_value'); // returns number
Hashset
add(key_name, key_value)
For the key, creates a hashset for it from a given dictionary data structure (i.e from key_value argument)
NOTE: The dictionary must be valid in the format {“key”: ”value”, ...}
Arguments
- key_name (string | number): Name of the hashset
- key_value (object): JS object i.e Dictionary data structure
Example:
myCacheApp.hashSet.add('key_name', { field1: 'value1', field2: 'value2' }); // returns boolean
addOrUpdateField(key_name, field_name, field_value)
For the key, if the field (i.e the field_name argument) is already present in the hashset then it will be updated with the field value (i.e the field_value argument) or else the field (i.e the field_name argument) with the field value (i.e the field_value argument) will be added to the existing hashset
NOTE:
- If the key is not present then a new hashset will be created
- The field_value will always be of stored as string or number type in the cache app
Arguments
- key_name (string | number): Name of the hashset
- field_name (string | number): Name of the field in the hashset
- field_value (any): Value of the field in the hashset
Example:
myCacheApp.hashSet.add_field('key_name', 'field1', 'field_value_1'); // returns number
get(key_name, field_name)
For the key, retrieves the hashset for it, optionally if the field name (i.e the field_name argument) can be passed to get its field value
Arguments
- key_name (string | number): Name of the hashset
- field_name (string | number): Name of the field in the hashset, this is an optional argument
Example:
myCacheApp.hashSet.get('key_name'); // returns complete JS object i.e dictionary data structure
myCacheApp.hashSet.get('key_name', 'field_name1'); // returns string
delete(key_name, field_name)
For the key, deletes a field (i.e. the field_name argument) from the hashset if it exists and returns 1 or else returns 0
NOTE: To delete the entire hashset, use myCacheApp.item.delete(key_name)
Arguments
- key_name (string | number): Name of the hashset
- field_name (string | number): Name of the field in the hashset
Example:
myCacheApp.hashSet.delete('key_name', 'field_name1'); // returns number
Info
getMemory(key_name)
For the key, gets the memory (in bytes) consumed by it and its corresponding value
Arguments
- key_name (string | number): Name of the key
Example:
myCacheApp.info.getMemory('key_name'); // returns number
getType(key_name)
For the key, gets the data type of its corresponding key
Arguments
- key_name (string | number): Name of the key
Example:
myCacheApp.info.getType('key_name'); // returns string