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

import json

from django.conf import settings

from operation.models import LatestAppInfo
from utils.redis.rkeys import LATEST_APP_INFO


r = settings.REDIS_CACHE


# 最新 APP 相关


def set_latest_app(src=0):
    """ 设置最新 APP 信息 """
    try:
        appinfo = LatestAppInfo.objects.filter(src=src)[0].data
    except IndexError:
        appinfo = {}
    r.set(LATEST_APP_INFO % src, json.dumps(appinfo))
    return appinfo


def get_latest_app(src=0):
    """ 获取最新 APP 信息 """
    return json.loads(r.get(LATEST_APP_INFO % src) or '{}') or set_latest_app(src)
