<commit_before># Stash.js [![Build Status](https://travis-ci.org/tadeuzagallo/stash.js.svg?branch=master)](https://travis-ci.org/tadeuzagallo/stash.js) [![Code Climate](https://codeclimate.com/github/tadeuzagallo/stash.js.png)](https://codeclimate.com/github/tadeuzagallo/stash.js) #

Inspired by the [php library](https://github.com/tedivm/Stash), Stash makes it easier to save your data on multiple layers of cache

## Usage ##

You can either install via [npm](https://www.npmjs.org)

```javascript
$ npm install stash.js
```

Or via [bower](http://bower.io/)

```
$ bower install stash
```

Or you can just grab a copy of the [stash.js](https://raw.githubusercontent.com/tadeuzagallo/stash.js/master/src/stash.js) file

### Basic Usage ###

*Syntax Updated:* in order to add new drivers, I had to replace the old sync syntax, the new one is based on promises, and it now depends on [bluebird](https://github.com/kriskowal/q)

```javascript
// Initialize a stash pool
var stash = new Stash.Pool();

// Short version  -- with Promise

stash.get('my/key/path').catch(function (err) {
  // Data is either inexistent or expired
  return slowFuncThatReturnsPromise().then(this.save);
});

// Long Version (stash.get does all of it internally)
var item = stash.getItem('my/key/path');

item.get().then(function (data) {
  item.isMiss().then(function (missed) {
    if (missed) {
      item.lock(); // Async lock
      actuallyFetchData(function (data) {
        item.set(data);
        callback(data);
      });
    } else {
      callback(data);
    }
  });
});
```

You can run the node.js samples on the `examples` folder to see a really basic demo

### Managing drivers ###

```javascript
var ephemeral = new Stash.Drivers.Ephemeral();
var localStorage = new Stash.Drivers.LocalStorage('my-custom-namespace');
var pool = new Stash.Pool([ephemeral, localStorage]); // It read on this order, and writes in reverse order
```

### Drivers ###

Right now there are just this two drivers

* Ephemeral - runtime only
* LocalStorage - saves on browser localStorage

I should create some extra drivers in a really near future... If you want to contribute with some driver, just fork e send me a pull request, will be happy to merge! =)

### Invalidation ###

There are 4 cache policies right now:

* `Stash.Item.SP_NONE` (default): Ignores the `lock`, if an item is expired, `isMiss` will just return `true` anyway
* `Stash.Item.SP_OLD`: will return the old value and the subsequent `item.isMiss()` calls will return `false` while another instance has the context `lock()`ed
* `Stash.Item.SP_PRECOMPUTE`: you should call `item.get(Stash.Item.SP_PRECOMPUTE, time)`, where `item.isMiss()` will eventually return `true` for one instance `time` seconds before the cache is expired
* `Stash.Item.SP_VALUE`: a default `value` should be passed along the `get()` call, this value will be returned if the cache is expired and `lock()`ed

## TODO ##

* Add more drivers
* ~~Suporte Node.js as well~~ Add Node.js exclusive drivers
* Maybe a jQuery plugin to integrate with ajax calls

## Contact ##

Feel free to share any doubts, thoughts or even complaints, either on the [issues](https://github.com/tadeuzagallo/stash.js/issues), via [email](mailto:<EMAIL>) or [send me a tweet](https://twitter.com/tadeuzagallo)!
<commit_msg>fix readme typo and link<commit_after># Stash.js [![Build Status](https://travis-ci.org/tadeuzagallo/stash.js.svg?branch=master)](https://travis-ci.org/tadeuzagallo/stash.js) [![Code Climate](https://codeclimate.com/github/tadeuzagallo/stash.js.png)](https://codeclimate.com/github/tadeuzagallo/stash.js) #

Inspired by the [php library](https://github.com/tedivm/Stash), Stash makes it easier to save your data on multiple layers of cache

## Usage ##

You can either install via [npm](https://www.npmjs.org)

```javascript
$ npm install stash.js
```

Or via [bower](http://bower.io/)

```
$ bower install stash
```

Or you can just grab a copy of the [stash.js](https://raw.githubusercontent.com/tadeuzagallo/stash.js/master/src/stash.js) file

### Basic Usage ###

*Syntax Updated:* in order to add new drivers, I had to replace the old sync syntax, the new one is based on promises, and it now depends on [bluebird](https://github.com/petkaantonov/bluebird)

```javascript
// Initialize a stash pool
var stash = new Stash.Pool();

// Short version  -- with Promise

stash.get('my/key/path').catch(function (err) {
  // Data is either inexistent or expired
  return slowFuncThatReturnsPromise().then(this.save);
});

// Long Version (stash.get does all of it internally)
var item = stash.getItem('my/key/path');

item.get().then(function (data) {
  item.isMiss().then(function (missed) {
    if (missed) {
      item.lock(); // Async lock
      actuallyFetchData(function (data) {
        item.set(data);
        callback(data);
      });
    } else {
      callback(data);
    }
  });
});
```

You can run the node.js samples on the `examples` folder to see a really basic demo

### Managing drivers ###

```javascript
var ephemeral = new Stash.Drivers.Ephemeral();
var localStorage = new Stash.Drivers.LocalStorage('my-custom-namespace');
var pool = new Stash.Pool([ephemeral, localStorage]); // It reads on this order, and writes in reverse order
```

### Drivers ###

Right now there are just this two drivers

* Ephemeral - runtime only
* LocalStorage - saves on browser localStorage

I should create some extra drivers in a really near future... If you want to contribute with some driver, just fork e send me a pull request, will be happy to merge! =)

### Invalidation ###

There are 4 cache policies right now:

* `Stash.Item.SP_NONE` (default): Ignores the `lock`, if an item is expired, `isMiss` will just return `true` anyway
* `Stash.Item.SP_OLD`: will return the old value and the subsequent `item.isMiss()` calls will return `false` while another instance has the context `lock()`ed
* `Stash.Item.SP_PRECOMPUTE`: you should call `item.get(Stash.Item.SP_PRECOMPUTE, time)`, where `item.isMiss()` will eventually return `true` for one instance `time` seconds before the cache is expired
* `Stash.Item.SP_VALUE`: a default `value` should be passed along the `get()` call, this value will be returned if the cache is expired and `lock()`ed

## TODO ##

* Add more drivers
* ~~Suporte Node.js as well~~ Add Node.js exclusive drivers
* Maybe a jQuery plugin to integrate with ajax calls

## Contact ##

Feel free to share any doubts, thoughts or even complaints, either on the [issues](https://github.com/tadeuzagallo/stash.js/issues), via [email](mailto:<EMAIL>) or [send me a tweet](https://twitter.com/tadeuzagallo)!

{'fraction_non_alphanumeric': 0.1333935561577838, 'fraction_numerical': 0.00030111412225233364, 'mean_word_length': 4.210196078431372, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 20, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1287871', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2061, 'n_words': 716}