<commit_before># HashCache

HashCache is a small collection of hashes that cache data.

## Installation

Add this line to your application's Gemfile:

    gem 'hash_cache'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install hash_cache

## Usage

HashCache provides the following classes.

### HashCache::TTL

TTL (Time To Live) remembers values for as many seconds as you tell it. The default is 3600 seconds (1 hours).

```ruby
require 'hash_cache/ttl'

h = HashCache::TTL.new(ttl: 5)
h[:a] = 'aardvark'
h[:a] #=> 'aardvark'
sleep(6)
h[:a] #=> nil
```

If you pass `refresh: true`, reading a value will reset its timer; otherwise, only writing will.

The `memoize` method fetches a key if it exists and isn't expired; otherwise, it calculates the value using the block and saves it.

```ruby
h = HashCache::TTL.new(ttl: 5)

# Runs the block
h.memoize(:a) { |k| calculation_for(k) }

# Returns the previously-calculated value
h.memoize(:a) { |k| calculation_for(k) }

sleep(6)

# Runs the block
h.memoize(:a) { |k| calculation_for(k) }
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

# Thanks

Original tests were adapted from [volatile_hash](https://github.com/satyap/volatile_hash).
<commit_msg>Add 'coming soon'<commit_after># HashCache

HashCache is a small collection of hashes that cache data.

## Installation

Add this line to your application's Gemfile:

    gem 'hash_cache'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install hash_cache

## Usage

HashCache provides the following classes.

### HashCache::TTL

TTL (Time To Live) remembers values for as many seconds as you tell it. The default is 3600 seconds (1 hours).

```ruby
require 'hash_cache/ttl'

h = HashCache::TTL.new(ttl: 5)
h[:a] = 'aardvark'
h[:a] #=> 'aardvark'
sleep(6)
h[:a] #=> nil
```

If you pass `refresh: true`, reading a value will reset its timer; otherwise, only writing will.

The `memoize` method fetches a key if it exists and isn't expired; otherwise, it calculates the value using the block and saves it.

```ruby
h = HashCache::TTL.new(ttl: 5)

# Runs the block
h.memoize(:a) { |k| calculation_for(k) }

# Returns the previously-calculated value
h.memoize(:a) { |k| calculation_for(k) }

sleep(6)

# Runs the block
h.memoize(:a) { |k| calculation_for(k) }
```

## Coming Soon

Other classes with different cache expiration strategies.

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

# Thanks

Original tests were adapted from [volatile_hash](https://github.com/satyap/volatile_hash).

{'fraction_non_alphanumeric': 0.11674084217733653, 'fraction_numerical': 0.009585758301951387, 'mean_word_length': 4.2178571428571425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '3035027', 'n_tokens_mistral': 1020, 'n_tokens_neox': 963, 'n_words': 375}