python 访问 redis

数据存储

2017-11-17

204

0

技术:windows redis 3.0 + windows python 3.6.1

运行环境:windows redis 3.0 + windows python 3.6.1

demo功能:提供python 对redis 的基本操作入门

安装python,和redis模块

安装python pass

安装redis 模块 pip install redis

set,get

#coding:utf8

import redis

# 创建连接
# 
config = {
          'host':'localhost',
          'port':6379,
		  'db':0
}

#set, get 一个字符串
def set():
    r = redis.Redis(**config)
    r.set('name', 'zhangsan')
    print (r.get('name'))


def pool_set():
    pool = redis.ConnectionPool(**config)
    r = redis.Redis(connection_pool=pool)
    r.set('name1', 'francis')   #添加
    print (r.get('name1'))   #获取

#带有效期的set    
def set_with_expire_time():
    r = redis.Redis(**config)
    r.set('name', 'francis with expire time', 5)
    print (r.get('name'))

#获取一个key的值
def get(key):
    r = redis.Redis(**config)
    print (r.get(key))

if __name__ == '__main__':
    get('name')

redis 模块api

http://redisdoc.com/

 

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think