zoukankan      html  css  js  c++  java
  • Mac redis入门

    Mac redis入门

    一、Redis简介

    Redis是开源的key-value数据库,运行在内存中,但可以把数据持久化存到磁盘。Redis具有极高的性能,也为各种语言提供了丰富的接口,因此有着广泛的应用。

    二、Mac下安装

    直接用brew一行命令解决问题

    $ brew install redis
    

    启动服务

    $ redis-server
    

    测试连接

    另开一个终端,输入ping,如果输出pong表示连接成功

    $ redis-cli
    127.0.0.1:6379> ping
    PONG
    127.0.0.1:6379>
    

    三、redis数据类型

    1. String(字符串)

    用get、set访问和设置

    127.0.0.1:6379> set a 'hello'
    OK
    127.0.0.1:6379> get a
    "hello"
    127.0.0.1:6379> 
    

    2. Hash(哈希)

    用hget、hset访问和设置,哈希表内部的键用field来表示

    127.0.0.1:6379> HSET myhash field1 'hello' field2 'world'
    (integer) 2
    127.0.0.1:6379> HGET myhash field1
    "hello"
    127.0.0.1:6379> HGET myhash field2
    "world"
    

    3、List(列表)

    lpush、rpush表示从左、右插入数据,lpop、rpop表示从左、右弹出数据

    127.0.0.1:6379> LPUSH list a
    (integer) 1
    127.0.0.1:6379> LPUSH list b c
    (integer) 3
    127.0.0.1:6379> LRANGE list 0 5
    1) "c"
    2) "b"
    3) "a"
    127.0.0.1:6379> RPUSH list d
    (integer) 4
    127.0.0.1:6379> LRANGE list 0 5
    1) "c"
    2) "b"
    3) "a"
    4) "d"
    127.0.0.1:6379> LPOP list
    "c"
    127.0.0.1:6379> LRANGE list 0 5
    1) "b"
    2) "a"
    3) "d"
    127.0.0.1:6379>  
    

    4、Set(集合)

    数学意义上的无序不重复集合,元素类型为字符串,sadd添加元素,srem删除元素

    127.0.0.1:6379> SADD set redis
    (integer) 1
    127.0.0.1:6379> SADD set mongodb rabitmq
    (integer) 2
    127.0.0.1:6379> SADD set redis
    (integer) 0
    127.0.0.1:6379> SMEMBERS set
    1) "mongodb"
    2) "rabitmq"
    3) "redis"
    127.0.0.1:6379> srem set 'redis'
    (integer) 1
    127.0.0.1:6379> SMEMBERS set
    1) "mongodb"
    2) "rabitmq"
    

    5、Zset(Sorted set有序集合)

    与set类似,但根据score排序

    127.0.0.1:6379> ZADD sorted 1 hello
    (integer) 1
    127.0.0.1:6379> ZADD sorted 5 world
    (integer) 1
    127.0.0.1:6379> ZADD sorted 3 redis
    (integer) 1
    127.0.0.1:6379> ZRANGE sorted 0 5
    1) "hello"
    2) "redis"
    3) "world"
    127.0.0.1:6379> 
    

    三、Python接口

    # coding=utf-8
    import redis
    
    client = redis.Redis()
    
    
    print '---------------'
    print 'string测试'
    client.set('foo', 1)
    print client.get('foo')
    client.set('foo', 'you are fine')  # 已存在的键直接覆盖值
    print client.get('foo')
    client.delete('foo')  # 删除
    print client.get('foo')
    client.set('num', 5)
    client.incr('num')
    client.incrby('num', 5)
    print client.get('num')
    
    
    print '---------------'
    print 'hash测试'
    client.hset('car', 'price', 400)
    print client.hget('car', 'price')
    print client.exists('car')  # 测试key是否已经存在
    print client.hexists('car', 'price')  # 测试hash中的键是否存在
    print client.hexists('car', 'type')
    
    print client.hsetnx('car', 'name', 'jack')  # 字段不存在时才赋值,nx表示not exist
    print client.hsetnx('car', 'name', 'jom')
    
    
    print '---------------'
    print 'List测试'
    client.lpush('double_list', 1)
    client.lpush('double_list', 2)
    client.lpush('double_list', 3)
    client.rpush('double_list', 4)
    client.lpop('double_list')
    num = client.llen('double_list')
    print client.lrange('double_list', 0, num)
    
    
    print '---------------'
    print 'set测试'
    client.sadd('my_set', 1)
    client.sadd('my_set', 1)
    client.sadd('my_set', "abc")
    client.sadd('my_set', 123)
    client.sadd('my_set', -1)
    client.srem('my_set', 1)  # remove
    print client.smembers('my_set')
    
    
    print '---------------'
    print 'sorted set测试'
    client.zadd('sorted', 'field1', 1, 'field2', 2)
    client.zadd('sorted', 'field3', 4, 'field4', 3)
    print client.zrange('sorted', 0, 1)
    

    运行结果

    ---------------
    string测试
    1
    you are fine
    None
    11
    ---------------
    hash测试
    400
    True
    True
    False
    0
    0
    ---------------
    List测试
    ['2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '4', '4', '4', '4', '4']
    ---------------
    set测试
    set(['123', 'abc', '-1'])
    ---------------
    sorted set测试
    ['field1', 'field2']
    
  • 相关阅读:
    学习方法与经验总结
    工具综合症?资料收集狂?
    SpringMVC 过滤器Filter使用解析
    Spring 管理Filter和Servlet
    pom.xml配置详解
    开发Java web应用程序的介绍
    java web构建学习(概念基础)
    题目1474:矩阵幂
    题目1473:二进制数
    Python strip()方法
  • 原文地址:https://www.cnblogs.com/fanghao/p/8558252.html
Copyright © 2011-2022 走看看