day11 redis的储存

settings.py

# 缓存配置
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

user.serializers.py(要储存的字段)

from django.core.cache import cache #导包手导不然会出错
    def login_date(self, validate_date):
        #登录
        token = uuid.uuid4().hex
        user = AXFUser.objects.filter(u_username = validate_date['u_username']).first()
        #cache使用redis进行存储
        #set存储的是字符串类型的值
        cache.set(token, user.id, timeout=60 * 60 * 7)
        res = {
            'token': token
        }
        return res

你可能感兴趣的:(day11 redis的储存)