-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
みほん |
ruby/app.rb
Outdated
users.each do |u| | ||
redis.zadd *["users:#{u['name']}", u['id'], u.to_json] | ||
end | ||
users.close |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
#1