• redis 字符串


      Redis的字符串就是一个由字节组成的序列,它们和很多编程语言里面的字符串没有什么明显

    的不同,跟C或者C++风格的字符数组也相去不远。在Redis里面,字符串可以存储以下3中类型

    的值。

      1)字节串(byte string)

           2)整数

           3)浮点数

      用户可以通过给定一个任意的数值,对存储着整数或者浮点数的字符串执行自增

    (increment)或者自减(decrement)操作,在有需要的时候,Redis还会将整数转换成

    浮点数。整数的取值范围和系统的长整数(long integer)的取值范围相同(在32位系统上,

    整数就是32位有符号整数,在64位系统上,整数就是64位有符号整数),而浮点数的取值范围

    和精度则与IEEE 754 标准的双精度浮点数(double)相同。Redis明确地区分字节串、整数和

    浮点数的做法是一种优势,比起只能够存储字节串的做法,Redis的做法在数据表现方面具有更

    大的灵活性。

      本节将对Redis里面最简单的结构--字符串进行讨论,介绍基本的数值自增和自减操作,

    以及二进制位(bit)和子串(substring)处理命令。

      当用户将一个值存储到Redis字符串里面的时候,如果这个值可以被解释(interpret)为十进

    制整数或者浮点数,那么Redis会察觉到这一点,并允许用户对这个字符串执行各种INCR*和

    DECR*操作。如果用户对一个不存在的键或者一个保存了空串的键执行自增或者自减操作,那么

    Redis在执行操作时会将这个键的值当作是0来处理。如果用户尝试对一个值无法被解释为整数或

    者浮点数的字符串键执行自增或者自减操作,那么Redis将向用户返回一个错误

      除了自增和自减操作之外,Redis还拥有对字节串的其中一部分内容进行读取或者写入

    的操作(这些操作也可以用于整数或者浮点数,但这种用法并不常见)

    GetRange命令是由以前的substr命令改名而来的

      在使用SETRANGE或者SETBIT命令对字符串进行写入的时候,如果字符串当前的长度不能

    满足写入的要求,那么Redis会自动地使用空字节(null)来将字符串扩展至所需的长度,然后

    才执行写入或者更新操作。在使用getrange读取字符串的时候,超过字符串末尾的数据会被视为

    是空串,而在使用getbit读取二进制位串的时候,超过字符串末尾的二进制位会被视为是0.

    SET 设置存储在给定键中的值

    官方文档说明 https://redis.io/commands/set

    Available since 1.0.0.
    
    Time complexity: O(1)
    
    Set key to hold the string value. If key already holds a value, it is overwritten, 
    regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation. Options Starting with Redis 2.6.12 SET supports a set of options that modify its behavior: •EX seconds -- Set the specified expire time, in seconds. •PX milliseconds -- Set the specified expire time, in milliseconds. •NX -- Only set the key if it does not already exist. •XX -- Only set the key if it already exist. Note: Since the SET command options can replace SETNX, SETEX, PSETEX,
    it is possible that in future versions of Redis these three commands will be deprecated and finally removed. Return value Simple string reply: OK if SET was executed correctly.
    Null reply: a Null Bulk Reply is returned if the SET operation was not performed because the user specified the NX or XX option
    but the condition was not met. Examples redis> SET mykey "Hello" "OK" redis> GET mykey "Hello" redis> Patterns Note: The following pattern is discouraged in favor of the Redlock algorithm which is only a bit more complex to implement,
    but offers better guarantees and is fault tolerant. The command SET resource-name anystring NX EX max-lock-time is a simple way to implement a locking system with Redis. A client can acquire the lock if the above command returns OK (or retry after some time if the command returns Nil),
    and remove the lock just using DEL. The lock will be auto-released after the expire time is reached. It is possible to make this system more robust modifying the unlock schema as follows: •Instead of setting a fixed string, set a non-guessable large random string, called token. •Instead of releasing the lock with DEL, send a script that only removes the key if the value matches. This avoids that a client will try to release the lock after the expire time deleting the key created by another client that acquired the lock later. An example of unlock script would be similar to the following: if redis.call("get",KEYS[1]) == ARGV[1] then return redis.call("del",KEYS[1]) else return 0 end The script should be called with EVAL ...script... 1 resource-name token-value

    Get 获取存储在给定键中的值

     官方说明  https://redis.io/commands/get

    Available since 1.0.0.
    
    Time complexity: O(1)
    
    Get the value of key. If the key does not exist the special value nil is returned. 
    An error is returned if the value stored at key is not a string, because GET only handles string values. Return value Bulk string reply: the value of key, or nil when key does not exist. *Examples redis> GET nonexisting (nil) redis> SET mykey "Hello" "OK" redis> GET mykey "Hello" redis>

     DEL 删除存储在给定键中的值(这个命令可以用于所有类型)

    官方说明 https://redis.io/commands/del

    Available since 1.0.0.
    
    Time complexity: O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).
    
    Removes the specified keys. A key is ignored if it does not exist.
    
    Return value
    Integer reply: The number of keys that were removed.
    
    *Examples
    redis> SET key1 "Hello"
    "OK"
    redis> SET key2 "World"
    "OK"
    redis> DEL key1 key2 key3
    (integer) 2
    redis> 
  • 相关阅读:
    phpcms列表页内容如何替换?
    如何用phpcms将静态网页生成动态网页?
    简单介绍phpcms以及phpcms如何安装?
    注册账号的验证
    js跳转页面
    流程审核处理
    关于XML(可扩展标记语言)的基础知识与写法
    php随机获取验证码
    流程管理
    HDU 5894 hannnnah_j’s Biological Test
  • 原文地址:https://www.cnblogs.com/toUpdating/p/9876481.html
Copyright © 2020-2023  润新知