<commit_before># redis-cacheable

It is concern style redis caching helper.
It makes very easy to cache object.

## Installation

Add this line to your application's Gemfile:

    gem 'redis-cacheable'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install redis-cacheable

## Usage

### Plain Object

```ruby
class MyObject
  include RedisCacheable

  attr_reader :id, :name

  redis_key   :id          # optional (default: :id)
  redis_attrs :id, :name # method names

  def initialize(attributes)
    @id = attributes[:id]
    @name = attributes[:name]
  end
end

class ProcObject < MyObject
  redis_attrs ->(obj) { obj.id * 10 } # can give proc
end
```

```ruby
my_object = MyObject.new(id: 1, name: "my_object")
my_object.cache_to_redis # KEY = my_object.id, VALUE = MultiJson.dump({"id" => my_object.id, "name" => my_object.name})

MyObject.find_from_redis(1) # => {"id" => 1, "name" => "my_object"}

proc_object = ProcObject.new(id: 1, name: "proc_object")
proc_object.cache_to_redis # different namespace with MyObject
ProcObject.find_from_redis(1) # => 10
```

### ActiveRecord

If you use ActiveRecord, can include more specialized module.

```ruby
# id: integer
# name: string
# rate: float

class MyRecord < ActiveRecord::Base
  include RedisCacheable::ActiveRecord
  redis_attrs :id, :name, :rate # method names
end
```

```ruby
record = MyRecord.create(name: "my_record", rate: 4.5)
record.cache_to_redis
MyRecord.find_from_redis(record.id) #=> #<MyRecord id: 1, name: "my_record", rate: "4.5">

MyRecord.cache_all # cache all records
```

## Configuration

```ruby
RedisCacheable.configure do |config|
  config.host = "10.0.0.1"          #  redis host (default: "localhost")
  config.port = 6380                #  redis port (default: 6379)
  config.driver = :hiredis          #  redis port (default: :ruby)
  config.namespace_prefix = "myapp" #  see below.
  config.pool_size = 10             #  connection pool size (default: 5)
  config.timeout = 10               #  timeout seconds (default: 5)
end
```

`config.namespace_prefix` is useful, If multi application use same redis.

```ruby
RedisCacheable.configure do |config|
  config.namespace_prefix = "myapp" #  see below.
end
<commit_msg>Fix README<commit_after># redis-cacheable

It is concern style redis caching helper.
It makes very easy to cache object.

## Installation

Add this line to your application's Gemfile:

    gem 'redis-cacheable'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install redis-cacheable

## Usage

### Plain Object

```ruby
class MyObject
  include RedisCacheable

  attr_reader :id, :name

  redis_key   :id          # optional (default: :id)
  redis_attrs :id, :name # method names

  def initialize(attributes)
    @id = attributes[:id]
    @name = attributes[:name]
  end
end

class ProcObject < MyObject
  redis_attrs ->(obj) { obj.id * 10 } # can give proc
end
```

```ruby
my_object = MyObject.new(id: 1, name: "my_object")
my_object.cache_to_redis # KEY = my_object.id, VALUE = MultiJson.dump({"id" => my_object.id, "name" => my_object.name})

MyObject.find_from_redis(1) # => {"id" => 1, "name" => "my_object"}

proc_object = ProcObject.new(id: 1, name: "proc_object")
proc_object.cache_to_redis # different namespace with MyObject
ProcObject.find_from_redis(1) # => 10
```

### ActiveRecord

If you use ActiveRecord, can include more specialized module.

```ruby
# id: integer
# name: string
# rate: float

class MyRecord < ActiveRecord::Base
  include RedisCacheable::ActiveRecord
  redis_attrs :id, :name, :rate # method names
end
```

```ruby
record = MyRecord.create(name: "my_record", rate: 4.5)
record.cache_to_redis
MyRecord.find_from_redis(record.id) #=> #<MyRecord id: 1, name: "my_record", rate: "4.5">

MyRecord.cache_all # cache all records
```

## Configuration

```ruby
RedisCacheable::Configuration.configure do |config|
  config.host = "10.0.0.1"          #  redis host (default: "localhost")
  config.port = 6380                #  redis port (default: 6379)
  config.driver = :hiredis          #  redis port (default: :ruby)
  config.namespace_prefix = "myapp" #  see below.
  config.pool_size = 10             #  connection pool size (default: 5)
  config.timeout = 10               #  timeout seconds (default: 5)
end
```

`config.namespace_prefix` is useful, If multi application use same redis.

```ruby
RedisCacheable.configure do |config|
  config.namespace_prefix = "myapp" #  see below.
end

{'fraction_non_alphanumeric': 0.12455357142857143, 'fraction_numerical': 0.014732142857142857, 'mean_word_length': 3.746822033898305, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3646700', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1496, 'n_words': 433}