Use Redis via Python

Memorandum

The below codes is how to use redis via Python.

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
#!/usr/bin/python
# coding: UTF-8

import redis

r = redis.StrictRedis(host='localhost', port=6379)

# set/get string.
r.set('test1', 'aiueo')
r.expire('test1', 1000)
print r.get('test1') # aiueo

# set/get integer.
r.set('test2', 2)
print r.get('test2') # 2

# Check key exit.
print r.exists('test1') # True
print r.exists('test0') # False

# pattern match
keys = r.keys('test*')
if len(keys) > 0 :
for key in keys:
print '--------------------------------------------'
print key # test1, test2
print r.get(key) # aiueo, 2
print r.type(key) # Type : string, string
print r.ttl(key) # Expire : if not set, set "-1"

r.append('test1', '_kkkkkkk')
print r.get('test1') # aiueo_kkkkkkk

# delete cache.
r.delete('test1')
print r.exists('test1') # False

Thanks.

Author

Kenzo Tanaka

Posted on

2015-11-10

Updated on

2020-05-07

Licensed under

コメント