From 22a25f266daf5948c8fc751a003139bebbc1157b Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Fri, 17 Jan 2020 14:49:42 +0100 Subject: [PATCH] Performance: Cache DB transaction locking of Karma::ActivityLog.add. --- app/models/karma/activity_log.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/models/karma/activity_log.rb b/app/models/karma/activity_log.rb index 38263899b..a3aacbc73 100644 --- a/app/models/karma/activity_log.rb +++ b/app/models/karma/activity_log.rb @@ -20,6 +20,15 @@ add karma activity log of an object object_id = ObjectLookup.by_name(object) end + # scheduler transactions causes a lot of calls + # so we try to cache the add process + # to skip the time loss of the transaction + # to increase performance + if !force + cache = Cache.get("Karma::ActivityLog.add::#{activity.once_ttl.seconds}::#{action}::#{user.id}::#{object}::#{o_id}") + return cache if cache + end + Karma::ActivityLog.transaction do last_activity = Karma::ActivityLog.where(user_id: user.id).order(id: :desc).lock(true).first latest_activity = Karma::ActivityLog.where( @@ -48,6 +57,10 @@ add karma activity log of an object score: activity.score, score_total: local_score_total, ) + + if !force + Cache.write("Karma::ActivityLog.add::#{activity.once_ttl.seconds}::#{action}::#{user.id}::#{object}::#{o_id}", true, expires_in: activity.once_ttl.seconds) + end end # set new karma level