perf: 优化 api key 认证记录用户的时间

pull/12144/head
ibuler 2023-11-16 18:00:57 +08:00 committed by Bryan
parent 2630ea39a1
commit ded1b4bba1
1 changed files with 16 additions and 7 deletions

View File

@ -8,6 +8,7 @@ from django.utils.translation import gettext as _
from rest_framework import authentication, exceptions
from common.auth import signature
from common.decorators import delay_run
from common.utils import get_object_or_none, get_request_ip_or_data, contains_ip
from ..models import AccessKey, PrivateToken
@ -16,14 +17,22 @@ def date_more_than(d, seconds):
return d is None or (timezone.now() - d).seconds > seconds
def after_authenticate_update_date(user, token=None):
if date_more_than(user.date_api_key_last_used, 60):
user.date_api_key_last_used = timezone.now()
user.save(update_fields=['date_api_key_last_used'])
@delay_run(ttl=60)
def update_token_last_used(token):
token.date_last_used = timezone.now()
token.save(update_fields=['date_last_used'])
if token and hasattr(token, 'date_last_used') and date_more_than(token.date_last_used, 60):
token.date_last_used = timezone.now()
token.save(update_fields=['date_last_used'])
@delay_run(ttl=60)
def update_user_last_used(user):
user.date_api_key_last_used = timezone.now()
user.save(update_fields=['date_api_key_last_used'])
def after_authenticate_update_date(user, token=None):
update_user_last_used(user)
if token:
update_token_last_used(token)
class AccessTokenAuthentication(authentication.BaseAuthentication):