Created
February 18, 2012 19:50
-
-
Save releu/1860804 to your computer and use it in GitHub Desktop.
Likable
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
module Models | |
module Likable | |
extend ActiveSupport::Concern | |
def liked?(record) | |
$redis.sismember redis_key, record.id | |
end | |
def likes_count | |
$redis.scard redis_key | |
end | |
def like(record) | |
$redis.sadd record.redis_key, id | |
end | |
protected | |
def redis_key | |
"likes:#{id}:#{self.class.model_name}" | |
end | |
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
class Post < ActiveRecord::Base | |
include Models::Likable | |
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
class PostsController < ApplicationController | |
def like | |
post = Post.find(params[:id]) | |
current_user.like(post) | |
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
class User < ActiveRecord::Base | |
include Models::Likable | |
end |
потому что сообщения ищутся простым select * limit и кешируются, но кнопки лайк под сообщением без кеша и при выводе надо знать лайкнул или нет, чтобы вывести правильную кнопку. тогда получается + 50 запросов на страницу на кнопки. лучше они пойдут в редис
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
А почему редис?