Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ritikesh/memoize_until

A unique Memoization Pattern, rewritten in typescript. Original at https://github.com/freshworks/memoize_until
https://github.com/ritikesh/memoize_until

cachemanager javascript memoization ttl-cache-implementation typescript

Last synced: 27 days ago
JSON representation

A unique Memoization Pattern, rewritten in typescript. Original at https://github.com/freshworks/memoize_until

Awesome Lists containing this project

README

        

# MemoizeUntil
[![CircleCI](https://dl.circleci.com/status-badge/img/gh/ritikesh/memoize_until/tree/master.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/ritikesh/memoize_until/tree/master)
![Version](https://img.shields.io/npm/v/memoize_until.svg)
![Downlaods](https://img.shields.io/npm/dt/memoize_until.svg)

A unique Memoization Pattern which memoizes until the next time metric - could be until the next minute, hour, day, week.

## Get Started

Install `memoize_until` with:

```bash
npm install memoize_until --save-dev
```

## Usage

```javascript
const MemoizeUntil = require('memoize_until').MemoizeUntil

MemoizeUntil.fetch('min', 'default', () => {
return 'SomeComplexOperation';
})
```

#### options:
1. `min` : Metric until when you want to memoize. Can be one of ['min', 'hour', 'day', 'week']

2. `default` : All metric levels come with a 'default' key. More can be added by [initializing](#Initialisation) `MemoizeUntil` with extra keys

3. `cb`: callback function. This callback function must return a value which needs to be memoized/cached.

## Initialisation
To add custom keys to each metric, pass a custom object to the `init` function of `MemoizeUntil`.
```javascript
MemoizeUntil.init({
min: ['custom1', 'custom2']
})

MemoizeUntil.fetch('min', 'custom1', () => {
return 'SomeComplexOperation';
})
```

## Extend
To add custom keys during runtime to any metric, use the `extend` function of `MemoizeUntil`.
```javascript
let runtime_key = 'runtime_key';
MemoizeUntil.extend('min', runtime_key)

MemoizeUntil.fetch('min', runtime_key, () => {
return 'SomeComplexOperation';
})
```

## Nulls
if the value to be memoized is `undefined` or `null`, `MemoizeUntil` wraps it underneath a pseudo null-like object called `NullObject` and always returns `undefined`. This ensures that even nulls are memoized.

## License
[Ritikesh](https://ritikesh.github.io)
Licensed under the MIT license.