Performance: Cache DB transaction locking of Karma::ActivityLog.add.

This commit is contained in:
Rolf Schmidt 2020-01-17 14:49:42 +01:00 committed by Thorsten Eckel
parent e0dc13949a
commit 22a25f266d

View file

@ -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