-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedis.py
More file actions
93 lines (81 loc) · 2.22 KB
/
Copy pathRedis.py
File metadata and controls
93 lines (81 loc) · 2.22 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import redis
import time
import random
import string
from threading import Thread
import threading
from util import *
class Redis:
def __init__(self, host='localhost', port=6379, country='USA', url_type="traveltochina", culture="zh-CN"):
self.host = host
self.port = port
self.r = redis.StrictRedis(self.host,self.port)
self.country=country
self.url_type=url_type
self.culture=culture
self.prefix = self.country+":"+self.url_type +":"+self.culture+":"
self.session_num=100
self.timeout=300
self.lock = threading.Lock()
self.airline_prefix="airline:"
def assertRedis(self):
if self.r == None :
self.r = redis.StrictRedis(self.host,self.port)
def getValue(self, key):
try:
self.assertRedis()
if self.r.exists(key):
return self.r.get(key)
return ""
except Exception, exception:
print exception
def getSession(self):
i = random.randint(0,self.session_num-1)
print "i=%s" % i
key=self.prefix + ("%s" % i)
print "key=%s" % key
self.assertRedis()
if self.r.exists(key):
dict_str = self.r.get(key)
dict = eval(dict_str)
sid=dict['sid']
tid=dict['tid']
return (sid, tid)
print "retry get sid tid"
time.sleep(0.5)
sid=""
tid=""
(sid, tid) = GetCookieSession(self.country,self.url_type,self.culture)
dict={}
dict['sid']=sid
dict['tid']=tid
self.r.set(key, str(dict))
self.r.expire(key, self.timeout)
return (sid, tid)
def complete(self, prefix):
if self.r.exists(prefix):
return self.r.get(prefix)
(sid,tid)=self.getSession()
content=AutoComplete(prefix,sid, tid)
self.r.set(prefix,content)
self.r.expire(prefix, 8640000)
return content
def completeAirline(self, prefix):
inner_prefix=self.airline_prefix+prefix
if self.r.exists(inner_prefix):
return self.r.get(inner_prefix)
(sid,tid)=self.getSession()
content=AutoCompleteAirline(prefix,sid, tid)
self.r.set(inner_prefix,content)
self.r.expire(inner_prefix, 8640000)
return content
if __name__ == '__main__':
rdb = Redis()
(sid,tid)=rdb.getSession()
print "sid=%s tid=%s" % (sid, tid)
(sid,tid)=rdb.getSession()
print "sid=%s tid=%s" % (sid, tid)
(sid,tid)=rdb.getSession()
print "sid=%s tid=%s" % (sid, tid)
(sid,tid)=rdb.getSession()
print "sid=%s tid=%s" % (sid, tid)