https://github.com/theonlybeardedbeast/timed-memory-cache
Timed memory cache
https://github.com/theonlybeardedbeast/timed-memory-cache
cache memory-storage nodejs storage ttl ttl-cache typescript
Last synced: 26 days ago
JSON representation
Timed memory cache
- Host: GitHub
- URL: https://github.com/theonlybeardedbeast/timed-memory-cache
- Owner: TheOnlyBeardedBeast
- License: mit
- Created: 2021-03-11T18:28:58.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-12T08:58:24.000Z (about 5 years ago)
- Last Synced: 2025-10-20T22:51:49.437Z (7 months ago)
- Topics: cache, memory-storage, nodejs, storage, ttl, ttl-cache, typescript
- Language: TypeScript
- Homepage:
- Size: 148 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TimedMemoryCache
A cache which stores its entries for some time.
# Class: Cache
## Type parameters
| Name | Default |
| :--- | :-------- |
| `T` | _unknown_ |
## Table of contents
### Constructors
- [constructor](README.md#constructor)
### Accessors
- [length](README.md#length)
### Methods
- [clear](README.md#clear)
- [get](README.md#get)
- [has](README.md#has)
- [remove](README.md#remove)
- [set](README.md#set)
## Constructors
### constructor
\+ **new Cache**(`options?`: [_ICahceOptions_](./docs/interfaces/icahceoptions.md)): [_Cache_](README.md)
#### Type parameters:
| Name | Default |
| :--- | :-------- |
| `T` | _unknown_ |
#### Parameters:
| Name | Type |
| :--------- | :-------------------------------------------------------- |
| `options?` | [_ICahceOptions_](./docs/interfaces/icahceoptions.md) |
**Returns:** [_Cache_](README.md)
#### Examples:
```typescript
import Cache from 'timed-memory-cache';
const cache = new Cache();
```
## Accessors
### length
• get **length**(): _number_
Returns the number of entries in the cache
**Returns:** _number_
#### Examples:
```typescript
const cacheEntryCount = cache.length;
```
---
## Methods
### clear
▸ **clear**(): _void_
Clears the cache
**Returns:** _void_
#### Examples:
```typescript
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:
```typescript
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:
```typescript
const valueExists = cache.has('myKey');
```
### remove
▸ **remove**(`key`: _string_): _void_
Removes an entry from the cache
#### Parameters:
| Name | Type |
| :---- | :------- |
| `key` | _string_ |
**Returns:** _void_
#### Examples:
```typescript
cache.remove('myKey');
```
### set
▸ **set**(`key`: _string_, `value`: T, `options?`: [_ICacheEntryOptions_](./docs/interfaces/icacheentryoptions.md)): _void_
Adds a new entry into the cache
#### Parameters:
| Name | Type |
| :--------- | :------------------------------------------------------------------ |
| `key` | _string_ |
| `value` | T |
| `options?` | [_ICacheEntryOptions_](./docs/interfaces/icacheentryoptions.md) |
**Returns:** _void_
#### Examples:
```typescript
cache.set('myKey', 'myValue', { ttl: 120 * 1000 }, onRemove:(key,value) => { console.log("entry was removed after 120 seconds")});
```