-
Notifications
You must be signed in to change notification settings - Fork 0
/
users_data.py
53 lines (38 loc) · 1.02 KB
/
users_data.py
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
import os
import os.path
import json
import settings
import redis
redis = redis.from_url(settings.REDIS_URL)
redis_prefix = "users_"
def redis_key(user_name):
return redis_prefix + user_name
def load(user_name):
raw = redis.get(redis_key(user_name))
if raw:
return json.loads(raw)
else:
return None
def save(user_name, data):
redis.set(redis_key(user_name), json.dumps(data))
def save_users(users):
for user in users:
save(user['userName'], user)
def exists(user_name):
return redis.exists(redis_key(user_name))
def delete(user_name):
if exists(user_name):
redis.delete(redis_key(user_name))
def list_names():
names = []
for key in redis.keys():
if key.startswith(redis_prefix):
names.append(key[len(redis_prefix):])
return names
def list_users():
users = {}
for key in redis.keys():
if key.startswith(redis_prefix):
name = key[len(redis_prefix):]
users[name] = load(name)
return users