1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.conf import settings
from pytz import timezone
from datetime import datetime
import traceback
from libs.cache import redis_client
import json
import logging
from django.db import connection
def tzlc(dt, truncate_to_sec=True):
if dt is None:
return None
if truncate_to_sec:
dt = dt.replace(microsecond=0)
if dt.tzinfo is None:
return timezone(settings.TIME_ZONE).localize(dt)
else:
return timezone(settings.TIME_ZONE).normalize(dt)
def get_have_read_topic_id_list(device_id, user_id, query_type):
try:
if user_id and int(user_id) > 0:
redis_key = "physical:home_recommend" + ":user_id:" + str(user_id) + ":query_type:" + str(query_type)
else:
redis_key = "physical:home_recommend" + ":device_id:" + str(device_id) + ":query_type:" + str(query_type)
have_read_topic_id_list = list()
redis_field_list = [b'have_read_topic_list']
redis_field_val_list = redis_client.hmget(redis_key, redis_field_list)
if redis_field_val_list[0]:
have_read_topic_id_list = list(json.loads(redis_field_val_list[0]))
return have_read_topic_id_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list()
def get_have_read_lin_pictorial_id_list(device_id,user_id,query_type):
try:
if user_id and int(user_id) > 0:
redis_key = "physical:home_pictorial_recommend" + ":user_id:" + str(user_id) + ":query_type:" + str(query_type)
else:
redis_key = "physical:home_pictorial_recommend" + ":device_id:" + str(device_id) + ":query_type:" + str(query_type)
have_read_lin_pictorial_id_list = list()
redis_field_list = ['have_read_pictorial_list']
redis_field_val_list = redis_client.hmget(redis_key, redis_field_list)
if redis_field_val_list[0]:
have_read_lin_pictorial_id_list = list(json.loads(redis_field_val_list[0]))
return have_read_lin_pictorial_id_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list()
def is_connection_usable():
"""判断当前mysql的链接是否正常,不正常就close掉"""
try:
connection.connection.ping()
return True
except:
# logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return False