# -*- coding: utf-8 -*-

import json

from django.conf import settings

from utils.redis.rkeys import PROFILE_INFO


r = settings.REDIS_CACHE


# 用户相关


def set_profile_info(user):
    """ 设置用户信息 """
    r.set(PROFILE_INFO % user.user_id, json.dumps(user.data))
    return user.data


def set_profile_by_uid(user_id):
    """ 获取用户信息 """
    from account.models import UserInfo
    try:
        user = UserInfo.objects.get(user_id=user_id)
    except UserInfo.DoesNotExist:
        return {}
    return set_profile_info(user)


def get_profile_by_id(user_id):
    """ 获取用户信息 """
    return json.loads(r.get(PROFILE_INFO % user_id) or '{}') or set_profile_by_uid(user_id)
