Created
August 9, 2014 00:17
-
-
Save aledalgrande/9b9fd78ee8cbd95912ed to your computer and use it in GitHub Desktop.
Redis example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gemfile | |
gem 'redis', '~> 3.0.1' | |
gem 'hiredis', '~> 0.4.5' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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