# tiny-cache

> Tiny JavaScript library to easily store and retrieve data items (with size limit, expiration, local & session storage binding in option).

Latest version **0.1.0** (published 2015-05-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install tiny-cache
pnpm add tiny-cache
yarn add tiny-cache
bun add tiny-cache
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2015-05-07 |
| First published | 2015-05-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Guillaume Tournier |
| Maintainers | gtournie |
| Keywords | tiny-cache, cache, js, javascript, cookie, storage, localStorage, sessionStorage, limit, expiration, browser, node |

## Links

- npm: https://www.npmjs.com/package/tiny-cache
- Repository: https://github.com/gtournie/tiny-cache
- Homepage: http://github.com/gtournie/tiny-cache
- Issues: https://github.com/gtournie/tiny-cache/issues
- npm.io page: https://npm.io/package/tiny-cache

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.1.0 (latest) — 2015-05-07

## README

Cache Lib
=========

### Cache ###

Small lib (only 1.1kB gzipped and minified) which provides a key/value pair storage.

You can use it like a simple cache mechanism but you can also limit the size of
the storage, set an expiry date, or bind the storage to the localStorage or 
the  sessionStorage.

### Support ###

This code works with nodejs and has been tested on the following browsers:

* Microsoft Internet Explorer for Windows, version 6.0 and higher
* Mozilla Firefox 1.0 and higher
* Apple Safari 2.0.4 and higher
* Opera 9.25 and higher
* Chrome 1.0 and higher

#### [Documentation](DOC.md) ####

#### Examples ####

    // limit
    var cache = new Cache({ limit: 3 });
    cache.setItem('1', 'pear')
        .setItem('2', 'apple')
        .setItem('3', 'banana');
    
    cache.length;      /* => 3            */
    cache.getItem(1);  /* => 'pear'       */
    cache.getItem(3);  /* => 'banana'     */
        
    cache.setItem('4', 'strawberry');
    cache.length;      /* => 3             */
    cache.getItem(1);  /* => undefined    */
    cache.getItem(2)]; /* => 'strawberry' */
    
    var oneMinute = 1 * 60 * 60;
    
    // localStorage binding + maxAge &amp; expires options
    var localCache = new Cache({ storage: localStorage, maxAge: oneMinute });
    localCache.setItem(1, 'pear');
    localCache.setItem(2, 'apple', { expires: new Date(+new Date() + 2 * oneMinute) });
    localCache.setItem(3, 'banana', { maxAge: 3 * oneMinute });
    
    localCache.length;     /* => 3 */
    setTimeout(function() { localCache.length; /* => 2 */ }, oneMinute + 10);
    setTimeout(function() { localCache.length; /* => 1 */ }, 2 * oneMinute + 10);
    setTimeout(function() { localCache.length; /* => 0 */ }, 3 * oneMinute + 10);
    
    /* If you use a persistent storage (like localStorage or sessionStorage), expires 
    and maxAge options will do their jobs even if you refresh your browser */
    
    /* Example: (set keyPrefix to avoid conflicts between cache instances) */
    var localCache = new Cache({ storage: localStorage, keyPrefix: 'test' });
    localCache.setItem(1, 'orange', { maxAge: oneMinute });
    
    /* Now, refresh your browser, and: */
    var localCache = new Cache({ storage: localStorage, keyPrefix: 'test' });
    localCache.getItem(1); /* => 'orange' */
    setTimeout(function() { localCache.length; /* => 0 */ }, oneMinute + 10);

---
_Source: https://npm.io/package/tiny-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
