Skip to content

TheOnlyBeardedBeast/timed-memory-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TimedMemoryCache

A cache which stores its entries for some time.

Class: Cache<T>

Type parameters

Name Default
T unknown

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

+ new Cache<T>(options?: ICahceOptions<T>): Cache<T>

Type parameters:

Name Default
T unknown

Parameters:

Name Type
options? ICahceOptions<T>

Returns: Cache<T>

Examples:

import Cache from 'timed-memory-cache';

const cache = new Cache<string>();

Accessors

length

• get length(): number

Returns the number of entries in the cache

Returns: number

Examples:

const cacheEntryCount = cache.length;

Methods

clear

clear(): void

Clears the cache

Returns: void

Examples:

cache.clear();

get

get(key: string): T | undefined

Returns the value for a given key or undefined if nothing found

Parameters:

Name Type
key string

Returns: T | undefined

Examples:

const value = cache.get('myKey');

has

has(key: string): boolean

Check if an entry with given key exists

Parameters:

Name Type
key string

Returns: boolean

Examples:

const valueExists = cache.has('myKey');

remove

remove(key: string): void

Removes an entry from the cache

Parameters:

Name Type
key string

Returns: void

Examples:

cache.remove('myKey');

set

set(key: string, value: T, options?: ICacheEntryOptions<T>): void

Adds a new entry into the cache

Parameters:

Name Type
key string
value T
options? ICacheEntryOptions<T>

Returns: void

Examples:

cache.set('myKey', 'myValue', { ttl: 120 * 1000 }, onRemove:(key,value) => { console.log("entry was removed after 120 seconds")});