9951 explained code solutions for 126 technologies


lua-redisHow to check if key exists


local redis = (require 'redis').connect('127.0.0.1', 6379)
local does_exist = redis:exists('key')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

:exists

will return true if specified key exists

'key'

key name to check existence of

does_exist

will contain true if specified key exists


Usage example

local redis = (require 'redis').connect('127.0.0.1', 6379)
redis:set('key', 'val')

local exists = redis:exists('key')
print(exists)

local exists = redis:exists('key1')
print(exists)
output
true
false