Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redisにuserをキャッシュさせた #13

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Conversation

cureseven
Copy link
Owner

@cureseven cureseven commented Apr 24, 2021

#1

@cureseven
Copy link
Owner Author

みほん
#8

ruby/app.rb Outdated
users.each do |u|
redis.zadd *["users:#{u['name']}", u['id'], u.to_json]
end
users.close
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closeいらない

redis.zadd *["users:#{u['name']}", u['id'], u.to_json]
end
users.close
users
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initの処理じゃないからこれもいらなそう

ruby/app.rb Outdated
def initialize_user
users = db.prepare('SELECT * FROM users').execute
users.each do |u|
redis.zadd *["users:#{u['name']}", u['id'], u.to_json]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 users.each do |u|
      redis.zadd *["users:#{u['name']}", u['id'], u.to_json]
    end

こう書くと良さそう

 redis.mset users.map{|h| ["user_name:#{h['name']}", h['id']]}.flatten

end

# キャッシュにあればそれを返す.なければinitializeして返す
def get_user_by_name(name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  def get_user_by_name(name)
    id = redis.get "user_name:#{name}"
    JSON.load(redis.get("users:#{id}"))
  end

@@ -372,6 +392,7 @@ def initialize_channel_message_count
statement.execute(display_name, user['id'])
statement.close
end
initialize_user
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こんなかんじにすれば良さそう

  def set_user(user)
    redis.set "users:#{user['id']}", user.to_json
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants