Cache
Kind: global class
Properties
| Name | Type | Description |
|---|---|---|
| string | cacheKey | A unique identifier for the Cache instance. |
| string | entryKey | The property that defines the cache entry |
| number | lifetime | The entry's lifetime in milliseconds |
| any | original | An object based on which the cache's validation can be set |
- Cache
- new Cache()
- .set(key, value, options)
- .get(key) ⇒
T|Array.<T> - .del(key) ⇒
boolean - .stats() ⇒
CacheStats - .flush()
new Cache()
A simple key-value cache. Built to store typed and structured data. Qache combines simplicity and security - it clones all data and stores them instead of keeping their references in memory.
Example
const cache = new Cache<{
id: string;
firstName: string;
secondName: string;
}>({
cacheKey: 'default',
entryKey: 'id',
lifetime: 1000 * 60 * 5,
});
2
3
4
5
6
7
8
9
cache.set(key, value, options)
Adds an entry to the cache.
Kind: instance method of Cache
| Param | Description |
|---|---|
| key | Identifier of the cache entry |
| value | Value of the cache entry |
| options | Custom options for this cache entry |
Example
cache.set('/users/1', {
id: '1',
firstName: 'John',
secondName: 'Doe',
})
2
3
4
5
cache.get(key) ⇒ T | Array.<T>
Get a value from the cache.
Kind: instance method of Cache
Returns: T | Array.<T> - The value of the cache entry
| Param | Description |
|---|---|
| key | Identifier of the cache entry |
Example
const user = cache.get('/users/1');
console.log(user);
// Prints
// {
// id: '1',
// firstName: 'John',
// secondName: 'Doe',
// }
2
3
4
5
6
7
8
cache.del(key) ⇒ boolean
Deletes a single entry from the cache
Kind: instance method of Cache
Returns: boolean - Whether the entry was deleted
| Param | Description |
|---|---|
| key | Identifier of the cache entry |
Example
cache.del('/users/1');
// Returns true
2
cache.stats() ⇒ CacheStats
Get details about the cache instance
Kind: instance method of Cache
Returns: CacheStats - A list of details about the current cache instance
cache.flush()
Resets the cache instance. Does not reset schemata and datatype.
Kind: instance method of Cache
Validator
Kind: global class
Properties
| Name | Type | Description |
|---|---|---|
| Schema | schema | A schema object |
| ValidationMethod | validate | quick or deep |
new Validator()
Creates minimal object-validation schemata from primitive and complex types. There are two kinds of validation: quick and deep. - quick should be used when objects look exactly alike, including their property structure - deep should be used when objects are alike structurally, but their properties's indexes differ.
validator.validate(data)
Validates an item against the currently active schema
Kind: instance method of Validator
| Param | Description |
|---|---|
| data | The item to validate |
validator.validateList(data)
Validates an array of items against the currently active schema
Kind: instance method of Validator
| Param | Description |
|---|---|
| data | An array of items to validate |
validator.getSchema()
Get the currently active schema of this validator instance
Kind: instance method of Validator