(声明:本教程仅供本人学习使用,如有人使用该技术触犯法律与本人无关)
(如果有错误,还希望指出。共同进步)
# app资源前缀 class AppResourcesUrlConfig: # app图片资源前缀 resources_url = "http://************/" # 是否开启缓存 OPEN_CACHE = True
import redis # redis配置 def return_redis_cli(_config): redis_cli = redis.StrictRedis(**_config) return redis_cli # app资源配置读取 def app_resource_redis(): _config = dict(host="127.0.0.1", port=6379, db=15, decode_responses=True) return return_redis_cli(_config)
import json from utils_platform.redis_connection import app_resource_redis from utils_platform.redqueen.resources_configs import AppResourcesUrlConfig # 设置静态资源缓存 def cache_queryset(timeout, params='app_resource'): def wrapper(func): def inner(*args, **kwargs): if not AppResourcesUrlConfig.OPEN_CACHE: return func(*args, **kwargs) key = "{}_{}".format(params, func.__name__) cache = app_resource_redis() qs = cache.get(key) if qs is None: qs = func(*args, **kwargs) cache.set(key, json.dumps(qs), timeout) return qs return json.loads(qs) return inner return wrapper
# import os # # import sys # import django # os.chdir("../../") # # sys.path.append('.') # os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'matrix.local_settings') # django.setup() from matrix.settings import TEST_ENV from resource_management.models import Product from utils_platform.decorators import cache_queryset ADD_BOX_LIST = [261, 262, 263] if TEST_ENV else [164, 163, 162] # 小礼盒/大礼盒/豪华礼盒 ADD_PRODUCT_CATEGORY = { 4: "箱子(free)" } # 加载所有礼物信息,{id: {title, category, }} @cache_queryset(1800, params='product_all') def load_product_message(): products = Product.objects.all() return { product.id: { "title": product.title, "category": product.category, "icon": product.icon, "grade": product.grade } for product in products} # if __name__ == '__main__': # print(load_product_message())