Blame view

src/python/redisCommon.py 1.06 KB
1b20cfdb   zhouhaihai   赛季更新完善 无尽冒险排行榜
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
  from redis import Redis 
  import msgpack
  
  redisConf = {
  	"host" : "127.0.0.1",
  	"port" : 6100,
  	"db" : 1,
  	"password" : None,
  }
  
  
  def numberUnpack(n) :
  	if isinstance(n, bytes) :
  		try:
  			return int(n)
  		except ValueError:
  			try:
  				return float(n)
  			except ValueError:
  				pass
  		return n.decode()
  	else :
  		return n
  
  def stringUnpack(s) :
  	if isinstance(s, bytes) :
  		return n.decode()
  	else :
  		return s
  
  def tableUnpack(t) :
  	if isinstance(t, bytes) :
  		return msgpack.unpackb(t, raw = False) #解包
  	else :
  		return t
  
  redisUnpack = {
  	"number" : numberUnpack,
  	"string" : stringUnpack,
  	"table" : tableUnpack,
  	"default" : numberUnpack,
  }
  def commonPack(w) :
  	return w
  
  def tablePack(t):
  	return msgpack.packb(t, use_bin_type = True)
  
  redisPack = {
  	"default" : commonPack,
  	"table" : tablePack,
  }
  
  
  
  
  redis = Redis(
  	host = redisConf["host"], 
  	port = redisConf["port"], 
  	db = redisConf["db"],
  	password = redisConf["password"]
  )
  
  pipe = redis.pipeline()
  
  def decode(w, t = "default"):
  	return redisUnpack[t](w)
  
  def encode(w, t = "default") :
  	return redisPack[t](w)