zoukankan      html  css  js  c++  java
  • redis的安装和使用

    REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。

    Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

    它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

    Redis命令参考:http://redisdoc.com/

    Redis教学演练:http://try.redis.io/

    Redis官网安装:https://redis.io/download


    Download

    Redis uses a standard practice for its versioning: major.minor.patchlevel. An even minor marks a stable release, like 1.2, 2.0, 2.2, 2.4, 2.6, 2.8. Odd minors are used for unstable releases, for example 2.9.x releases are the unstable versions of what will be Redis 3.0 once stable.

    Installation

    Download, extract and compile Redis with:

    $ wget http://download.redis.io/releases/redis-5.0.3.tar.gz
    $ tar xzf redis-5.0.3.tar.gz
    $ cd redis-5.0.3
    $ make

    The binaries that are now compiled are available in the src directory. Run Redis with:

    $ src/redis-server

    You can interact with Redis using the built-in client:

    $ src/redis-cli
    redis> set foo bar
    OK
    redis> get foo
    "bar"

    还可以直接使用yum安装:yum install redis。yum安装的redis配置文件位置/etc/redis.conf。相关命令:service redis start|stop|restart…… 日志默认位置:/var/log/redis/


    一、redis-cli远程连接时报下面错误:方法见里面的说明。redis-server默认是安全模式下启动的。
    redis-cli -h 2.2.2.2

    Redis 性能测试

    Redis 性能测试是通过同时执行多个命令实现的。

    redis-benchmark -n 10000  -q -h 10.157.119.60

    Redis 数据类型:

    string、list、hash、set、有序set

    Redis客户端工具:pip install redis

    redis连接实例是线程安全的,可以直接将redis连接实例设置为一个全局变量,直接使用。如果需要另一个Redis实例(or Redis数据库)时,就需要重新创建redis连接实例来获取一个新的连接。redis-py使用connection pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销。默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数Redis,这样就可以实现多个Redis实例共享一个连接池

    import redis    # 导入redis模块,通过python操作redis 也可以直接在redis主机的服务端操作缓存数据库
    
    pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)   # host是redis主机,需要redis服务端和客户端都起着 redis默认端口是6379
    r = redis.Redis(connection_pool=pool)
    r.set('gender', 'male')     # key是"gender" value是"male" 将键值对存入redis缓存
    print(r.get('gender'))      # gender 取出键male对应的值

    管道(pipeline)

    redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。

    import redis
    import time
    
    pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
    r = redis.Redis(connection_pool=pool)
    
    # pipe = r.pipeline(transaction=False)    # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
    # pipe = r.pipeline(transaction=True)
    pipe = r.pipeline() # 创建一个管道
    
    pipe.set('name', 'jack')
    pipe.set('role', 'sb')
    pipe.sadd('faz', 'baz')
    pipe.incr('num')    # 如果num不存在则vaule为1,如果存在,则value自增1
    pipe.execute() # 所谓的原子性是指上面的set、sadd、incr要么全成功,要么全失败
    
    print(r.get("name"))
    print(r.get("role"))
    print(r.get("num"))

    另外一种写法:

    pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute()
    print(r.get("name"))
    print(r.get("role"))
    print(r.get("num"))

    注意python3.5的写法:
    r.zadd('zset2', {'n1':11,'n2':22})可以
    r.zadd('zset2', n1,11,n2,22)不可以
    
    r.mget({"name1":'zhangsan', "name2":'lisi'})可以
    r.mset(name1='zhangsan', name2='lisi')不可以

    参考: 1、http://www.runoob.com/redis/redis-tutorial.html

    2、https://www.jianshu.com/p/2639549bedc8

    3、https://segmentfault.com/a/1190000015191422

    4、http://python.jobbole.com/87305/

    5、https://juejin.im/post/5a3100b76fb9a04528467c37

    6、https://redis.io/download

  • 相关阅读:
    北京理工大学复试上机--2014
    北京理工大学复试上机--2013
    北京理工大学复试上机--2012
    北京理工大学复试上机--2011
    北京理工大学复试上机--2010
    Python的逻辑结构和函数
    Python的概述
    BOM
    正则表达式的匹配
    jQuery的插件和跨域、ajax
  • 原文地址:https://www.cnblogs.com/shengulong/p/10132287.html
Copyright © 2011-2022 走看看