-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.py
More file actions
66 lines (56 loc) · 1.63 KB
/
run.py
File metadata and controls
66 lines (56 loc) · 1.63 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
import redis
from flask import Flask
# redis
redis_cache = redis.Redis(host='localhost', port=6379, db=0, password="")
# flask app
app = Flask(__name__)
# set with expire
@app.route('/set/<string:key>/<string:value>/<int:expired>')
def set_with_expire(key, value, expired):
redis_cache.set(key, value, ex=expired)
return "OK"
# set
@app.route('/set/<string:key>/<string:value>')
def set(key, value):
if redis_cache.exists(key):
return f"{key} is already exists, please use `update` route to change the value!"
else:
redis_cache.set(key, value)
return "OK"
# update
@app.route('/update/<string:key>/<string:value>')
def update(key, value):
if redis_cache.exists(key):
redis_cache.set(key, value)
return "OK"
else:
return f"{key} is not exists"
# get
@app.route('/get/<string:key>')
def get(key):
if redis_cache.exists(key):
return redis_cache.get(key)
else:
return f"{key} is not exists"
# delete
@app.route('/delete/<string:key>')
def delete(key):
if redis_cache.exists(key):
redis_cache.delete(key)
return f"{key} deleted!"
else:
return f"{key} is not exists"
# expire
@app.route('/expire/<string:key>/<int:expired>')
def expire(key, expired):
if redis_cache.exists(key):
# ref: https://realpython.com/python-redis/
# ref: https://redis-py.readthedocs.io/en/stable/_modules/redis/client.html#Redis.expire
# redis_cache.expire(key, timedelta(seconds=expired))
print(f"Set expire {key} after {expired} seconds!")
redis_cache.expire(key, expired)
return "Done, expire set!"
else:
return f"{key} is not exists, please set this at the first"
if __name__ == "__main__":
app.run("127.0.0.1", port="5000", debug=True)