Commit a057d2cd authored by 段英荣's avatar 段英荣

modify

parent 69df59b5
import redis
from django.conf import settings
redis_client = redis.StrictRedis.from_url(settings.REDIS_URL)
\ No newline at end of file
......@@ -174,4 +174,4 @@ class ESPerform(object):
return result_dict
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return None
\ No newline at end of file
return {"total_count":0,"hits":[]}
\ No newline at end of file
......@@ -70,7 +70,7 @@ TEMPLATES = [
WSGI_APPLICATION = 'physical.wsgi.application'
REDIS_URL = "redis://127.0.0.1:6379"
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import traceback
import datetime
from libs.es import ESPerform
from libs.cache import redis_client
class PickUtils(object):
@classmethod
def refresh_redis_data(cls,have_read_celebrity_list,redis_cli,redis_key,key_exist):
try:
if key_exist:
redis_cli.set(redis_key,have_read_celebrity_list)
else:
now = datetime.datetime.now()
str_end_time = "%s-%s-%s 23:59:59" % (now.year, now.month, now.day)
end_time = datetime.datetime.strptime(str_end_time, "%Y-%m-%d %H:%M:%S")
valid_seconds = (end_time - now).seconds
redis_cli.setex(redis_key,have_read_celebrity_list,valid_seconds)
return True
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return False
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gm_rpcd.all import bind
import logging
import traceback
import json
from libs.cache import redis_client
from libs.es import ESPerform
from search.utils.pick import PickUtils
@bind("physical/search/pick_celebrity")
def pick_celebrity(user_id,pick_id,offset=0,size=10):
try:
redis_key = "physical:pick_celebrity:" + "user_id:" + str(user_id) + ":pick_id:" + str(pick_id)
have_read_pick_celebrity_ids = list()
try:
redis_celebrity_info = redis_client.get(redis_key)
have_read_pick_celebrity_ids = json.loads(redis_celebrity_info)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
q = dict()
q["query"] = {
"pick_id":pick_id,
"bool":{
"must_not":{
"terms":{
"id":have_read_pick_celebrity_ids
}
}
}
}
q["_source"] = {
"include":["id"]
}
result_dict = ESPerform.get_search_results(ESPerform.get_cli(),sub_index_name="pick_celebrity",query_body=q,offset=offset,size=size)
recommend_pick_celebrity_list = [item["id"] for item in result_dict["hits"]]
key_exist = len(have_read_pick_celebrity_ids) > 0
PickUtils.refresh_redis_data(have_read_celebrity_list=have_read_pick_celebrity_ids.extend(recommend_pick_celebrity_list),
redis_cli=redis_client,redis_key=redis_key,key_exist=key_exist)
return result_dict
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"total_count":0,"hits":[]}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment