How can I update a redis value without affecting the remaining TTL?

redis get ttl of all keys
redis ttl
redis update value
redis set expire on all keys
redis sadd expire
redis hset expire
update value in redis cache
redis update ttl

Is it possible to SET redis keys without removing their existing ttl? The only way I know of at the present time is to find out the ttl and do a SETEX but that seems less accurate.

According to Redis documentation, the SET command removes the TTL because the key is overwritten.

However you can use the EVAL command to evaluate a Lua script to do it automatically for you.

The script bellow checks for the TTL value of a key, if the value is positive it calls SETEX with the new value and using the remaining TTL.

local ttl = redis.call('ttl', ARGV[1]) if ttl > 0 then return redis.call('SETEX', ARGV[1], ttl, ARGV[2]) end

Example:

> set key 123

OK

> expire key 120

(integer) 1

... after some seconds

> ttl key

(integer) 97

> eval "local ttl = redis.call('ttl', ARGV[1]) if ttl > 0 then return redis.call('SETEX', ARGV[1], ttl, ARGV[2]) end" 0 key 987

OK

> ttl key

96

> get key

"987"

How to update a Redis value without affecting the remaining TTL, TTL key. Available since 1.0.0. Time complexity: O(1). Returns the remaining time to live of a Starting with Redis 2.8 the return value in case of error changed:. Subscribe How to update a Redis value without affecting the remaining TTL 07 January 2016 on script, lua, redis, ttl. Very self explanatory title, this beauty it's a Lua script running inside Redis interpreter, it will basically grap the ttl from the passed key if exist (ttl > 0) and use it which means that we are overriding the key or if not will just setex the key with the passed data and ttl.

Maybe the INCR, INCRBY, DECR, etc. can help you. They don't modify the TTL.

> setex test 3600 13
OK

> incr test
(integer) 14

> ttl test
(integer) 3554

http://redis.io/commands/INCR

TTL – Redis, For instance, incrementing the value of a key with INCR, pushing a new value into a list prior 2.1.3 altering a key with an expire set using a command altering its value had Normally Redis keys are created without an associated time to live. Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset. In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has no associated expire.

The KEEPTTL option will be added to SET in redis>=6.0

https://redis.io/commands/set

https://github.com/antirez/redis/pull/6679

The SET command 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.
(!) KEEPTTL -- Retain the time to live associated with the key.

EXPIRE – Redis, Like TTL this command returns the remaining time to live of a key that has an expire set, with Starting with Redis 2.8 the return value in case of error changed:. Return Value. Integer value TTL in milliseconds, or a negative value. TTL in milliseconds..-1, if the key does not have expiry timeout.-2, if the key does not exist. Return Value Type . Integer. Example: Redis TTL. First, create a key in redis and set some value in it. Now set the expiry of the key and after that just check the remaining expiry

PTTL – Redis, TTL, TTL key-name — Returns the amount of time remaining before a key will expire conn.set('key', 'value') True >>> conn.get('key') 'value' we can instead get similar semantics with expiration, without needing a cleanup function. Can you update the update_token() and add_to_cart() functions to expire keys instead of� Redis TTL command is used to get the remaining time of the key expiry in seconds. Return Value. Integer value TTL in milliseconds, or a negative value. TTL in milliseconds.-1, if key does not have expiry timeout.-2, if key does not exist. Syntax. Following is the basic syntax of Redis TTL command. redis 127.0.0.1:6379> TTL KEY_NAME Example

Here's a function to check the existing TTL and use it if needed.

expire arguments 0 - use existing expire time, >0 - set a new expire time, undefined - no expire time.

    /**
       * update an item. preserve ttl or set a new one
       * @param {object} handle the redis handle
       * @param {string} key the key
       * @param {*} content the content - if an object it'll get stringified
       * @param {number||null} expire if a number > 0 its an expire time, 0 
means keep existing ttl, undefined means no expiry
       * @return {Promise}
       */
      ns.updateItem = function (handle , key , content,expire) {

        // first we have to get the expiry time if needed
        return (expire === 0 ? handle.ttl(key) : Promise.resolve (expire))
        .then (function (e) {

          // deal with errors retrieving the ttl (-1 no expiry, -2 no existing record)
          var ttl = e > 0 ? e :  undefined;

          // stingify the data if needed
          var data = typeof content === "object" ? JSON.stringify(content) : content;

          // set and apply ttl if needed
          return ttl ? handle.set (key, data , "EX", ttl) : handle.set (key,data);
        });
      };

3.7.3 Expiring keys, Redis is an open-source, in-memory key-value data store. How To Change Redis's Configuration from the Command Line This tutorial explains how to set keys to expire, check the time remaining until a key's expiration, and cancel the database is installed can add and delete data without restriction. The expiration values of a key can be managed by a user outside of the update commands. Redis allows a user to determine the current time to live (TTL) of a key using the TTL command: TTL key This command will return the TTL of a key in seconds or will return the special values -1 or -2.

How To Expire Keys in Redis, Redis TTL command is used to get the remaining time of the key expiry in seconds. Return Value. Integer value TTL in milliseconds, or a negative value. TTL in� To get an expiration time of key in seconds, we will use a COMMAND – TTL in redis-cli. This number of seconds represents the remaining time to live of the key, after this time key will get deleted from datastore. The syntax of command TTL is as follows :- Syntax :-redis host:post> TTL <key name> Output :-

Redis - Keys Ttl Command, Redis TTL : Redis TTL command is used to get remaining time of key expiry in seconds. Redis TTL. Last update on February 26 2020 08:08:39 (UTC/GMT +8 hours) Integer value TTL in milliseconds, or a negative value. Redis Server Version: 2.8.19. Steps to reproduce: In redis server run SETEX mykey 2147483667 myvalue (note that the EXPIRES value is larger than INT_MAX) In Redis Desktop Manager right-click the db and hit 'Refresh' In Redis Desktop Manager inspect the TTL on key mykey; Expected result: TTL is a large positive value; Actual Result:

Redis TTL key, The rest of the methods in the RedisTTLBackfillService ColdFusion I am the key at which to store the cursor value for our SCAN operation. I am the TTL to apply to persistent keys (ie, keys with no configured TTL). variables. to be having a detrimental affect on Redis or on the * application servers. Does redis update the key value and ttl if I set the same key value and ttl again before the current ttl expires? When I do so, it doesn't seem to do anything, and the keys expire after the previous set timeout.