CustomRedis

CustomRedis is based on Redis-backend library for creating easily a simple custom cache stores . It allows you to improve your application caching several kind of elements you'll use with the front-end as counters, hashes created from Active Record relations.

Installation

Add this line to your application's Gemfile:

gem 'custom_redis'

And then execute:

$ bundle

Or install it yourself as:

$ gem install custom_redis

Usage

The gem allows you to manage { key: value } elements easily autogenerating unique keys from ActiveRecord instances on a Redis server which must be running. If you have no idea how Redis works, please check Redis's homepage: First of all, after adding the gem to the Gemfile, you have to create the initializer CustomRedis.rb and add this line to start using custom_redis

    CustomRedis.new

It'll create the global $custom_redis variable. It's accesible from any part of your app to use all Redis functionabilities. Otherwise CustomRedis allows you to forget several methods and manage a cache quickly.

Forget when you have to execute a block of methods to set a value in Redis or just get it. With cr_eval method you can set the object, the type and an array of methods to execute over the object

    employer = Employer.find_by_id(35)

    #It returns a string if the value exists in Redis or execute the block of methods to insert the value 
    my_custom_redis_element = CustomRedis.cr_eval(employer, :string, :get_agreements, :first ,:build_html_resume)

Also you can use the method cr_eval to eval a set (an Active Record relation for instance) to be inserted in Redis after executing the block of methods

    # Returns an array of strings if the value exists or execute the block of methods to insert the value
    my_custom_redis_selements = CustomRedis.cr_eval(employer, { :set => :build_html_resume }, :get_agreements)

There are other methods to handle the management of Redis for your project

    # Delete the matched keys with the block of methods
    my_custom_redis_deleted_key = CustomRedis.cr_delete(employer, :get_agreements,:build_html_resume)

    # Delete the matched keys with a pattern
    my_custom_redis_flushed_keys = CustomRedis.cr_pflush('employer*get_agreements*build_html_resume')

    # Generate the custom redis key which use the method cr_eval
    my_custom_redis_key = CustomRedis.my_redis_key(employer, "this:is:the:block:of:methods")

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/lmbautista/custom_redis. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

custom_redis