Skip to content

Instantly share code, notes, and snippets.

@aledalgrande
Created August 9, 2014 00:17
Show Gist options
  • Save aledalgrande/9b9fd78ee8cbd95912ed to your computer and use it in GitHub Desktop.
Save aledalgrande/9b9fd78ee8cbd95912ed to your computer and use it in GitHub Desktop.
Redis example
# Gemfile
gem 'redis', '~> 3.0.1'
gem 'hiredis', '~> 0.4.5'
# app/models/post.rb
class Post < ActiveRecord::Base
after_create :store_in_recent_list
def store_in_recent_list
$redis.lpush('recent_posts', self.id)
$redis.rpop('recent_posts')
end
def self.recent_posts
ids = $redis.lrange('recent_posts', 0, 19)
Post.where(id: ids)
end
end
# config/initializers/redis.rb
$redis = Redis.new(ENV['REDISTOGO_URL'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment