https://github.com/felixwieland/attic
Sophisticated caching library
https://github.com/felixwieland/attic
caching fallback lifetime persistent-storage
Last synced: about 2 months ago
JSON representation
Sophisticated caching library
- Host: GitHub
- URL: https://github.com/felixwieland/attic
- Owner: FelixWieland
- License: mit
- Created: 2019-12-11T20:49:37.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-03T11:21:16.000Z (about 2 years ago)
- Last Synced: 2025-03-11T17:48:47.791Z (2 months ago)
- Topics: caching, fallback, lifetime, persistent-storage
- Language: TypeScript
- Size: 286 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# attic

[](https://github.com/FelixWieland/attic/actions)
[](https://github.com/FelixWieland/attic/blob/master/LICENSE)Attic synchronizes persistent storage such as localStorage with an object in memory to reduce access time. In addition, it can track the lifetime of an element and discard it if it is too old. To minimize null values, you must specify a fallback (for example, an API query) each time you read from the cache. If an element does not exist or the lifetime of an element has expired, the fallback function is used and the result of this function is stored with the ID passed in the Get function. This means that the cache does not have to be filled explicitly via a set function (although the possibility exists in Attic).
# Installation
```sh
// with npm
npm install @fwieland/attic// with yarn
yarn add @fwieland/attic
```# Usage
Here is a quick example to get you started:
```js
import Attic from '@fwieland/attic';const cache = new Attic("storageName", {
lifetime: 10000, //10sec
})cache.get("id1")
.fallback(() => fetch('https://jsonplaceholder.typicode.com/todos/1').then(r => r.json())
.then(content => console.log(content));```
To reduce the boilerplate of extracting the json (in case of API-Requests) attic allows to specify a fallbackExtractor function.
```js
const cache = new Attic("storageName", {
fallbackExtractor: (r) => r.json(),
})cache.get("id1")
.fallback(() => fetch('https://jsonplaceholder.typicode.com/todos/1'))
.then(content => console.log(content));```
# Options
You can provide different options to customize and extend attic:
The provided examples are the default values of attic.```js
const cache = new Attic("storageName", {// spezifies the liftime of a item
liftime: null,// will get called on the fallback promise
fallbackExtractor: (e) => e,// On default attic will load the spezific item into memory if it is the first time requested.
// If synOnInit is true attic will load all items that are stored in the persitentCache into the memoryCache.
syncOnInit: false,// memoryCache, TS-Interface: ICache
memoryCache: new MemoryCache(),//localStorage, TS-Interface: ICache
persistentCache: new PersistentCache(),
})```