macos上安装Redis
安装: brew install redis
启动: redis-server

redis-cli

ping 、ssh

redis客户端(redis-cli):

检测是否启动成功:

➜  ~ redis-cli
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ping
PONG

远程连接:

➜  ~ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379>

防治中文乱码

redis-cli --raw

存取、设置过期

为了看起来清晰,以下均用小写

key-value存取:

127.0.0.1:6379> set name tyrad
OK
127.0.0.1:6379> get name
"tyrad"
127.0.0.1:6379> set name  uu
OK
127.0.0.1:6379> get name
"uu"

检查是否存在

127.0.0.1:6379> exists name
(integer) 1

设置过期时间(秒)

127.0.0.1:6379> expire name 3
127.0.0.1:6379> get name
(nil)

设置新值并返回旧值

127.0.0.1:6379> getset name  小明
"tyrad"

hash

存取

127.0.0.1:6379> hmset mykvs name "myname" address "myadress"
OK
127.0.0.1:6379> hgetall mykvs
1) "name"
2) "myname"
3) "address"
4) "myadress"

获取allkey

127.0.0.1:6379> hkeys mykvs
1) "name"
2) "address"

检测是否存在

127.0.0.1:6379> hexists mykvs  name
(integer) 1

获取指定的值

127.0.0.1:6379> hmget mykvs name
1) "myname"

继续存值

127.0.0.1:6379> hmset  mykvs old "18" sex "female"
OK
127.0.0.1:6379> hmget mykvs name sex
1) "myname"
2) "female"

删除

127.0.0.1:6379> hdel mykvs name
(integer) 1

列表

集合

python使用举例

# -*- coding: UTF-8 -*-

from flask import Flask
from redis import Redis

app = Flask(__name__)
redis = Redis(host='127.0.0.1', port=6379)

@app.route('/')
def hello():
    count = redis.incr('hits')
    return 'Hello World! 该页面已被访问 {} 次。\n'.format(count)

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)