Project

cachers

0.0
No commit activity in last 3 years
No release in over 3 years
Dedicated classes to isolate redis logic in rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.21
~> 3.3

Runtime

~> 5.1
 Project Readme

Gem Version Code Climate Build Status Dependency Status

Cachers

Dedicated classes to isolate redis logic in rails.

Why

We did this gem to:

  • Prevent models to have too much code.
  • Have a convention of how to use cache in models.
  • Increase atomization in complex projects.

Install

Put this line in your Gemfile:

gem 'cachers'

Then bundle:

$ bundle

Usage

Definitions

Generate a cacher for a model:

$ bundle exec rails g cacher like

Add the logic to the cache and uncache methods:

class LikeCacher < Cachers::Base

  def cache
    sadd key, record.product.id
  end

  def uncache
    srem key, record.product.id
  end

  private

  def key
    "users/#{record.user.id}/likes"
  end

end

NOTE: Updates work automatically, there is no need to add a method to replace values.

Custom methods

You may want to add some other methods related to the cache:

class UserCacher < Cachers::Base

  def likes?(product)
    sismember key, product.id
  end

  private

  def key
    "users/#{record.id}/likes"
  end

end

The public methods will be available to call directly from the model:

user.likes? product

Cache management

If you want to manually control the cache, you can use this methods:

user.cache
user.recache
user.uncache

Or to it for all cacheable records:

$ bundle exec rake cachers:cache
$ bundle exec rake cachers:recache
$ bundle exec rake cachers:uncache

Contributing

Any issue, pull request, comment of any kind is more than welcome!

We will mainly ensure compatibility to Rails, AWS, PostgreSQL, Redis, Elasticsearch and FreeBSD. 

Credits

This gem is maintained and funded by museways.

License

It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.