Gem Version Build Status Code Climate

Readthis

Readthis is a drop in replacement for any ActiveSupport compliant cache, but emphasizes performance and simplicity. It takes some cues from Dalli (connection pooling), the popular Memcache client. Below are some performance comparisons against the only other notable redis cache implementation, redis-activesupport, which has been abandoned and doesn't actually comply to Rails 4.2 cache store behavior for fetch_multi.

Footprint & Performance

Footprint compared to redis-activesupport:

# Total allocated objects after require
readthis: 19,964
redis-activesupport: 78,630

Performance compared to dalli and redis-activesupport for *multi operations:

Calculating -------------------------------------
 readthis:read-multi   118.000  i/100ms
  redisas:read-multi    94.000  i/100ms
    dalli:read-multi    92.000  i/100ms
-------------------------------------------------
 readthis:read-multi      1.206k (± 4.6%) i/s -      6.018k
  redisas:read-multi    973.086  (± 4.4%) i/s -      4.888k
    dalli:read-multi    949.348  (± 4.1%) i/s -      4.784k

Comparison:
 readthis:read-multi:     1206.0 i/s
  redisas:read-multi:      973.1 i/s - 1.24x slower
    dalli:read-multi:      949.3 i/s - 1.27x slower

Calculating -------------------------------------
readthis:fetch-multi   114.000  i/100ms
 redisas:fetch-multi    82.000  i/100ms
   dalli:fetch-multi    97.000  i/100ms
-------------------------------------------------
readthis:fetch-multi      1.157k (± 5.0%) i/s -      5.814k
 redisas:fetch-multi    829.211  (± 4.2%) i/s -      4.182k
   dalli:fetch-multi    979.081  (± 3.8%) i/s -      4.947k

Comparison:
readthis:fetch-multi:     1157.2 i/s
   dalli:fetch-multi:      979.1 i/s - 1.18x slower
 redisas:fetch-multi:      829.2 i/s - 1.40x slower

Installation

Add this line to your application's Gemfile:

gem 'readthis'

Usage

Use it the same way as any other ActiveSupport::Cache::Store. Within a rails environment config:

config.cache_store = :readthis_store, ENV.fetch('REDIS_URL'), {
  expires_in: 2.weeks,
  namespace: 'cache'
}

Otherwise you can use it anywhere, without any reliance on ActiveSupport:

require 'readthis'

cache = Readthis::Cache.new(ENV.fetch('REDIS_URL'), expires_in: 2.weeks)

You'll want to use a specific database for caching, just in case you need to clear the cache entirely. Appending a number between 0 and 15 will specify the redis database, which defaults to 0. For example, using database 2:

REDIS_URL=redis://localhost:6379/2

Differences

Readthis supports all of standard cache methods except for the following:

  • cleanup - redis does this with ttl for us already
  • delete_matched - you really don't want to perform key matching operations in redis. They are linear time and only support basic globbing.