Permalink
Browse files

[FEAT] adding redis implementation for likes

  • Loading branch information...
1 parent 945e352 commit 65f4281a92563d83cad450fe549054111b3e7fd7 Elad Meidar committed Jul 13, 2014
Showing with 74 additions and 7 deletions.
  1. +3 −0 Gemfile
  2. +4 −0 Gemfile.lock
  3. +49 −0 app/models/concerns/likes.rb
  4. +7 −7 config/initializers/fog.rb
  5. +2 −0 config/initializers/redis.rb
  6. +9 −0 config/redis.yml
View
3 Gemfile
@@ -37,6 +37,9 @@ gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
+gem "hiredis", "~> 0.4.0"
+gem "redis", ">= 2.2.0", require: ["redis", "redis/connection/hiredis"]
+
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
View
4 Gemfile.lock
@@ -84,6 +84,7 @@ GEM
multi_json (~> 1.0)
formatador (0.2.5)
hike (1.2.3)
+ hiredis (0.4.5)
i18n (0.6.9)
inflecto (0.0.2)
ipaddress (0.8.0)
@@ -138,6 +139,7 @@ GEM
rdoc (4.1.1)
json (~> 1.4)
redcarpet (3.1.2)
+ redis (3.0.7)
sass (3.2.19)
sass-rails (4.0.3)
railties (>= 4.0.0, < 5.0)
@@ -187,6 +189,7 @@ DEPENDENCIES
faker
figaro
fog
+ hiredis (~> 0.4.0)
jbuilder (~> 1.2)
jquery-rails
mini_magick
@@ -195,6 +198,7 @@ DEPENDENCIES
rails (= 4.0.5)
rails_12factor
redcarpet
+ redis (>= 2.2.0)
sass-rails (~> 4.0.2)
sdoc
sqlite3
View
49 app/models/concerns/likes.rb
@@ -0,0 +1,49 @@
+# @user_like = Likes.new(current_user, @post)
+# @user_like.like!
+# @user_like.unlike!
+# @user_like.liked_posts
+# @user_like.liked_posts_count
+# @user_like.post_user_likes
+
+class Likes
+
+ # Adding a like to the post key
+ def self.user_likes_post(user, post)
+ REDIS.multi do
+ REDIS.sadd self.post_key(post), user.id
+ REDIS.sadd self.user_key(user), post.id
+ end
+ return true
+ end
+
+ # Removing a like from the post key
+ def self.user_dislikes_post(user, post)
+ REDIS.multi do
+ REDIS.srem self.post_key(post), user.id
+ REDIS.srem self.user_key(user), post.id
+ end
+ return true
+ end
+
+ # Return total likes count per post
+ def self.total_likes_for_post(post)
+ REDIS.scard self.post_key(post)
+ return true
+ end
+
+ # Return all post ids the user liked
+ def self.user_liked_posts(user)
+ REDIS.scard self.user_key(user)
+ return true
+ end
+
+ protected
+
+ def self.user_key(user)
+ "users:#{user.id}:liked_posts"
+ end
+
+ def self.post_key(key)
+ "posts:#{post.id}:liking_users"
+ end
+end
View
14 config/initializers/fog.rb
@@ -1,9 +1,9 @@
CarrierWave.configure do |config|
- config.fog_credentials = {
- provider: 'AWS',
- aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
- aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
- }
- config.fog_directory = ENV['AWS_BUCKET']
- config.fog_public = true
+ # config.fog_credentials = {
+ # provider: 'AWS',
+ # aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
+ # aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
+ # }
+ # config.fog_directory = ENV['AWS_BUCKET']
+ # config.fog_public = true
end
View
2 config/initializers/redis.rb
@@ -0,0 +1,2 @@
+redis_conf = YAML::load(File.read(Rails.root.join("config", "redis.yml")))[Rails.env]
+REDIS = Redis.new(redis_conf)
View
9 config/redis.yml
@@ -0,0 +1,9 @@
+default:
+ host: localhost
+ port: 6379
+development:
+ db: 1
+test:
+ db: 2
+production:
+ db: 3

0 comments on commit 65f4281

Please sign in to comment.