9951 explained code solutions for 126 technologies


lua-redisHow to set expiration for key


local redis = (require 'redis').connect('127.0.0.1', 6379)
redis:set('key', 'test')
redis:expire('key', 30)ctrl + c
require 'redis'

load Redis module for Lua

connect

connect to Redis server

'127.0.0.1', 6379

Redis host and port to connect to

:set

sets specified key value

:expire

set expiration (in seconds) for the specified key

'key'

key name to set expiration for

30

our key will expire in 30 seconds


Usage example

local redis = (require 'redis').connect('127.0.0.1', 6379)
redis:set('key', 'test')
redis:expire('key', 30)
print(redis:ttl('key'))
output
60