相关文章推荐
暴躁的石榴  ·  翻译 - Dolibarr ERP CRM ...·  3 周前    · 
不羁的饺子  ·  Debezium | Apache Flink·  3 周前    · 
追风的板凳  ·  list.stream().filter过滤 ...·  1 年前    · 
内向的冲锋衣  ·  pandas ...·  2 年前    · 

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

The SET command supports a set of options that modify its behavior:

  • EX seconds -- Set the specified expire time, in seconds (a positive integer).
  • PX milliseconds -- Set the specified expire time, in milliseconds (a positive integer).
  • EXAT timestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds (a positive integer).
  • PXAT timestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds (a positive integer).
  • NX -- Only set the key if it does not already exist.
  • XX -- Only set the key if it already exists.
  • KEEPTTL -- Retain the time to live associated with the key.
  • GET -- Return the old string stored at key, or nil if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.
  • Note: Since the SET command options can replace SETNX , SETEX , PSETEX , GETSET , it is possible that in future versions of Redis these commands will be deprecated and finally removed.

    Examples Code samples for data structure store quickstart pages: https://redis.io/docs/latest/develop/get-started/data-store/ import redis r = redis . Redis ( host = "localhost" , port = 6379 , db = 0 , decode_responses = True ) res = r . set ( "bike:1" , "Process 134" ) print ( res ) # >>> True res = r . get ( "bike:1" ) print ( res ) # >>> "Process 134" const client = createClient (); client . on ( 'error' , err => console . log ( 'Redis Client Error' , err )); await client . connect (). catch ( console . error ); await client . set ( 'bike:1' , 'Process 134' ); const value = await client . get ( 'bike:1' ); console . log ( value ); // returns 'Process 134' await client . close (); public void run () { UnifiedJedis jedis = new UnifiedJedis ( "redis://localhost:6379" ); String status = jedis . set ( "bike:1" , "Process 134" ); if ( "OK" . equals ( status )) System . out . println ( "Successfully added a bike." ); String value = jedis . get ( "bike:1" ); if ( value != null ) System . out . println ( "The name of the bike is: " + value + "." ); jedis . close (); rdb := redis . NewClient ( & redis . Options { Addr : "localhost:6379" , Password : "" , // no password docs DB : 0 , // use default DB err := rdb . Set ( ctx , "bike:1" , "Process 134" , 0 ). Err () if err != nil { panic ( err ) fmt . Println ( "OK" ) value , err := rdb . Get ( ctx , "bike:1" ). Result () if err != nil { panic ( err ) fmt . Printf ( "The name of the bike is %s" , value ) public void Run () var muxer = ConnectionMultiplexer . Connect ( "localhost:6379" ); var db = muxer . GetDatabase (); bool status = db . StringSet ( "bike:1" , "Process 134" ); if ( status ) Console . WriteLine ( "Successfully added a bike." ); var value = db . StringGet ( "bike:1" ); if ( value . HasValue ) Console . WriteLine ( "The name of the bike is: " + value + "." );

    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]
        return redis.call("del",KEYS[1])
        return 0
    

    The script should be called with EVAL ...script... 1 resource-name token-value

    Return information
  • If GET was not specified, any of the following:
  • Nil reply: Operation was aborted (conflict with one of the XX/NX options). The key was not set.
  • Simple string reply: OK: The key was set.
  • If GET was specified, any of the following:
  • Nil reply: The key didn't exist before the SET. If XX was specified, the key was not set. Otherwise, the key was set.
  • Bulk string reply: The previous value of the key. If NX was specified, the key was not set. Otherwise, the key was set.
  • If GET was not specified, any of the following:
  • Null reply: Operation was aborted (conflict with one of the XX/NX options). The key was not set.
  • Simple string reply: OK: The key was set.
  • If GET was specified, any of the following:
  • Null reply: The key didn't exist before the SET. If XX was specified, the key was not set. Otherwise, the key was set.
  • Bulk string reply: The previous value of the key. If NX was specified, the key was not set. Otherwise, the key was set.
  • Starting with Redis version 2.6.12: Added the EX, PX, NX and XX options.
  • Starting with Redis version 6.0.0: Added the KEEPTTL option.
  • Starting with Redis version 6.2.0: Added the GET, EXAT and PXAT option.
  • Starting with Redis version 7.0.0: Allowed the NX and GET options to be used together.
  •