简体   繁体   English

django-pgcrypto-fields 需要很长时间才能加载

[英]django-pgcrypto-fields taking long time to load

I'm using django 2.2.13 and django-pgcrypto-fields 2.5.2 .我正在使用django 2.2.13django-pgcrypto-fields 2.5.2 Also i'm using email as authentication method.我也使用email作为身份验证方法。 email is stored as pgcrypto field. email存储为pgcrypto字段。 There are around 10000 active users.大约有10000个活跃用户。 When user tries to login it takes a long time (8-9 seconds).当用户尝试登录时,需要很长时间(8-9 秒)。 I tried to login from shell, it also takes a long time.我尝试从shell登录,也需要很长时间。

from django.contrib.auth import authenticate
user = authenticate(email='john@gmail.com', password='secret')

The authenticate function takes almost 7-8 seconds to execute.验证 function 需要将近 7-8 秒来执行。

user = authenticate(username='john', password='secret')

When I try to authenticate using username , it executes within 1 seconds当我尝试使用username进行身份验证时,它会在 1 秒内执行

from app.models import User
user = User.objects.filter(email=email).first()

The above query also takes a long time to execute (7-8 seconds).上述查询也需要很长时间才能执行(7-8 秒)。 I tried indexing the email column, but the outcome is same.我尝试索引 email 列,但结果是一样的。 How can I speed up authentication and filter queries for pgcrypto fields?如何加快 pgcrypto 字段的身份验证和过滤查询?

I have got a workaround solution.我有一个解决方法。 I have narrowed down user list by last_login and override _authenticate_by_email method of django-allauth我通过last_login缩小了用户列表并覆盖了django-allauth_authenticate_by_email方法

class CustomAuthenticationBackend(AuthenticationBackend):
    def _authenticate_by_email(self, **credentials):
        email = credentials.get('email', credentials.get('username'))
        if email:
            startdate= timezone.now() - timedelta(days=CACHED_AUTHENTICATION_DAY)
            user = User.objects.filter(last_login__gte=startdate, email=email).first()
            if user:
                if self._check_password(user, credentials["password"]):
                    return user
                return None
            for user in filter_users_by_email(email):
                if self._check_password(user, credentials["password"]):
                    return user
        return None

So if the CACHED_AUTHENTICATION_DAY is set to 30/60 days, then active users will be able to login very fast.因此,如果CACHED_AUTHENTICATION_DAY设置为 30/60 天,那么活跃用户将能够非常快速地登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM