zoukankan      html  css  js  c++  java
  • Redis string类型常用操作

     

    Redis 有 string、list、set、zset、hash数据类型。string类型是最基础的,其他类型都是在string类型上去建立的,所以了解熟悉string类型的常用操作对于学习redis非常必要。

    APPEND:向键的值后面追加字符串,并且返回追加后的值长度

    127.0.0.1:6379[7]> get hello
    "world"
    127.0.0.1:6379[7]> APPEND hello wonderful
    (integer) 14
    127.0.0.1:6379[7]> get hello
    "worldwonderful"

    SETBIT:设置或清除指定偏移位的值(比特位从左往右算)

    GETBIT:获取指定位的比特值

    BITCOUNT:获取指定字节范围中有多少位为 1

    127.0.0.1:6379[7]> set andy 'a'
    OK
    127.0.0.1:6379[7]> get andy
    "a"
    127.0.0.1:6379[7]> SETBIT andy 6 1
    (integer) 0
    127.0.0.1:6379[7]> SETBIT andy 7 0
    (integer) 1
    127.0.0.1:6379[7]> GET andy
    "b"
    127.0.0.1:6379[7]> GETBIT andy 7
    (integer) 0
    127.0.0.1:6379[7]> BITCOUNT andy 0 3
    (integer) 3

    INCR:值递增

    INCRBY:按照指定值递增

    INCRBYFLOAT:指定浮点数递增

    DECR:值递减

    DECRBY:按照指定值递减

    127.0.0.1:6379[7]> INCRBY counter 3
    (integer) 9
    127.0.0.1:6379[7]> DECRBY counter 2
    (integer) 7
    127.0.0.1:6379[7]> INCRBYFLOAT counter 1.6
    "8.6"
    # 如果值为非int型则会报错
    127.0.0.1:6379[7]> INCR key3
    (error) ERR value is not an integer or out of range

    GET:返回键的值

    GETRANGE:获取值指定范围

    GETSET:设置值并且返回原来的值

    127.0.0.1:6379[7]> GETSET key3 test_key3
    "value3"
    127.0.0.1:6379[7]> get key3
    "test_key3"
    127.0.0.1:6379[7]> GETRANGE key3 0 3
    "test"

    MGET:批量获取值,如果对应键不存在会使用 nil 代替,按指定顺序返回

    MSET:批量设置键值对,相比 SET 节省了网络时间,但如果一次性设置太多可能会导致阻塞

    127.0.0.1:6379[7]> MSET a 1 b 2 c 3 d 4
    OK
    127.0.0.1:6379[7]> MGET a b c d
    1) "1"
    2) "2"
    3) "3"
    4) "4"

    SET:设置键值对

    语法:set key value [expiration EX seconds|PX milliseconds] [NX|XX]
      expiration:设置过期时间,EX 以秒为单位, PX 以毫秒为单位
      NX:键必须不存在才能设置成功
      XX:键必须存在才能设置成功

    SETNX:同等于 SET 指定 NX 参数

    127.0.0.1:6379[7]> get test1
    (nil)
    127.0.0.1:6379[7]> GET redis
    "best"
    127.0.0.1:6379[7]> SETNX redis "good"
    (integer) 0
    127.0.0.1:6379[7]> SETNX test1 "good"
    (integer) 1

    SETEX:设置键值对,并且指定过期时间,同等于 SET 指定 EX 参数。

    127.0.0.1:6379[7]> SETEX test1 10 "test1"
    OK
    127.0.0.1:6379[7]> get test1
    "test1"
    127.0.0.1:6379[7]> get test1
    (nil)

    MSETNX:等同于 MSET 和 SETNX 结合

    PSETEX:等同于 SET 设置 PX 参数

    SETRANGE:修改值指定范围的内容

    127.0.0.1:6379[7]> set redis pest
    OK
    127.0.0.1:6379[7]> SETRANGE redis 0 b
    (integer) 4
    127.0.0.1:6379[7]> get redis
    "best"

    STRLEN:返回值的长度,以字节为单位,注意每个中文字符占3个字节

    参考文档:《Redis开发与运维》、Redis官方文档

    参考网络文件:理解setbit中的bit;https://www.zhihu.com/question/27672245

    理解SETBIT、BITCOUNT等命令推荐博客:https://blog.csdn.net/u011489043/article/details/78990162

    每天进步一点点~~~
  • 相关阅读:
    寄存器基础知识
    基础知识
    架构
    Mac下Apache服务器的初步搭建
    ios字体简单设定
    xcode中自定义log打印
    jQuery打印插件
    ionic3生命周期钩子
    ES5 数组方法map
    $compile的妙用
  • 原文地址:https://www.cnblogs.com/xingphimo/p/10844413.html
Copyright © 2011-2022 走看看