<commit_before># `hash_cache`

`hash_cache` is a Ruby gem, providing a small collection of hashes that cache data.

## Usage

`hash_cache` 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 hour).

```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) }
```

## Installation

Add this line to your application's Gemfile:

    gem 'hash_cache'

And then execute:

    $ bundle
<commit_msg>Document LRU<commit_after># `hash_cache`

`hash_cache` is a Ruby gem, providing a small collection of caches with good performance and hash-like access patterns. Each is named for its cache expiration strategy - when it will drop a key.

## Usage

`hash_cache` 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 hour).

```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) }
```

### HashCache::LRU

LRU (Least Recently Used) remembers as many keys as you tell it to, dropping the least recently used key on each insert after its limit is reached.

```ruby
require 'hash_cache/lru'
h = HashCache::LRU.new(max_keys: 3)
h[:a] = "aardvark"
h[:b] = "boron"
h[:c] = "cattail"
h[:d] = "dingo"
puts h[:a] # => nil
```

## Installation

Add this line to your application's Gemfile:

    gem 'hash_cache'

And then execute:

    $ bundle

{'fraction_non_alphanumeric': 0.12804399057344854, 'fraction_numerical': 0.007462686567164179, 'mean_word_length': 4.396186440677966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, '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': '4563999', 'n_tokens_mistral': 940, 'n_tokens_neox': 889, 'n_words': 332}