zoukankan      html  css  js  c++  java
  • Redis键值数据类型之字符串

    字符串类型

    1. 获得当前key="bar"对应value的类型

    type bar

    2. 字符串类型可以存储任何形式的字符串, 当存储的字符串是整数形式时, Redis提供了一个命令"incr", 让当前键值递增,并返回递增后的值

    127.0.0.1:6379>  incr num
    (integer) 1
    127.0.0.1:6379>  incr num
    (integer) 2
    127.0.0.1:6379>  get num
    "2"
    127.0.0.1:6379>  type num
    string

    如果字符串不是整型数字,则会报错

    127.0.0.1:6379> set str abcde
    OK
    127.0.0.1:6379> get str
    "abcde"
    127.0.0.1:6379> incr str
    (error) ERR value is not an integer or out of range

    3. 指定增加减少的整数


    127.0.0.1:6379> set bar 0 OK 127.0.0.1:6379> incrby bar 1 (integer) 2 127.0.0.1:6379> incrby bar 3 (integer) 5 127.0.0.1:6379> get bar "5"

    减少 127.0.0.1:6379> decr bar (integer) 4 127.0.0.1:6379> decrby 3 (error) ERR wrong number of arguments for 'decrby' command 127.0.0.1:6379> decrby bar 3 (integer) 1

    同时还有加上浮点数  incrbyfloat bar 1.1

    4. 向尾部追加值, 获取字符串长度

    127.0.0.1:6379> set key hello
    OK
    127.0.0.1:6379> append key " world!"
    (integer) 12
    127.0.0.1:6379> get key
    "hello world!"
    127.0.0.1:6379> strlen key
    (integer) 12

    5.同时获得/设置多个键值

    127.0.0.1:6379> mset key1 v1 key2 v2 key3 v3
    OK
    127.0.0.1:6379> get key2
    "v2"
    127.0.0.1:6379> mget key2 key3
    1) "v2"
    2) "v3"

     总结:

    总结图片来自: https://www.cnblogs.com/liqingwen/p/6919308.html

    参考书籍《Redis入门指导》——李子骅 编著

  • 相关阅读:
    Thinking in Java
    Interview Common Sample Codes
    Longest Common Substring
    Mac键盘按键符号
    ElasticSearch
    Variables and Arithmetic Expression
    Associative Containers
    Container Adaptors
    string Type
    初识 tk.mybatis.mapper 通用mapper
  • 原文地址:https://www.cnblogs.com/smuzoey/p/11680017.html
Copyright © 2011-2022 走看看