Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

309 Commits

Repository files navigation

redis-lua

About

redis-lua is a pure Lua client library for the Redis advanced key-value database.

Main features

  • Support for Redis >= 1.2
  • Command pipelining
  • Redis transactions (MULTI/EXEC) with CAS
  • User-definable commands
  • UNIX domain sockets (when available in LuaSocket)

Compatibility

This library is tested and works with Lua 5.1, Lua 5.2 (using a compatible version of LuaSocket) and LuaJit 2.0.

Examples of usage

Include redis-lua in your script

Just require the redis module assigning it to a variable:

localredis=require'redis'

Previous versions of the library defined a global Redis alias as soon as the module was imported by the user. This global alias is still defined but it is considered deprecated and it will be removed in the next major version.

Connect to a redis-server instance and send a PING command

localredis=require'redis'localclient=redis.connect('127.0.0.1', 6379)
localresponse=client:ping() -- true

It is also possible to connect to a local redis instance using UNIX domain sockets if LuaSocket has been compiled with them enabled (unfortunately it is not the default):

localredis=require'redis'localclient=redis.connect('unix:///tmp/redis.sock')

Set keys and get their values

client:set('usr:nrk', 10)
client:set('usr:nobody', 5)
localvalue=client:get('usr:nrk') -- 10

Sort list values by using various parameters supported by the server

for_,vinipairs({ 10,3,2,6,1,4,23 }) doclient:rpush('usr:nrk:ids',v)
endlocalsorted=client:sort('usr:nrk:ids', {
sort='asc', alpha=true, limit= { 1, 5 }
}) -- {1=10,2=2,3=23,4=3,5=4}

Pipeline commands

localreplies=client:pipeline(function(p)
p:incrby('counter', 10)
p:incrby('counter', 30)
p:get('counter')
end)

Variadic commands

Some commands such as RPUSH, SADD, SINTER and others have been improved in Redis 2.4 to accept a list of values or keys depending on the nature of the command. Sometimes it can be useful to pass these arguments as a list in a table, but since redis-lua does not currently do anything to handle such a case you can use unpack() albeit with a limitation on the maximum number of items which is defined in Lua by LUAI_MAXCSTACK (the default on Lua 5.1 is set to 8000, see luaconf.h):

localvalues= { 'value1', 'value2', 'value3' }
client:rpush('list', unpack(values))
-- the previous line has the same effect of the following one:client:rpush('list', 'value1', 'value2', 'value3')

Leverage Redis MULTI / EXEC transaction (Redis > 2.0)

localreplies=client:transaction(function(t)
t:incrby('counter', 10)
t:incrby('counter', 30)
t:get('counter')
end)

Leverage WATCH / MULTI / EXEC for check-and-set (CAS) operations (Redis > 2.2)

localoptions= { watch="key_to_watch", cas=true, retry=2 }
localreplies=client:transaction(options, function(t)
localval=t:get("key_to_watch")
t:multi()
t:set("akey", val)
t:set("anotherkey", val)
end)

Get useful information from the server

fork,vinpairs(client:info()) doprint(k..' => ' ..tostring(v))
end--[[redis_git_dirty => 0redis_git_sha1 => aaed0894process_id => 23115vm_enabled => 0hash_max_zipmap_entries => 64expired_keys => 9changes_since_last_save => 2role => masterlast_save_time => 1283621624used_memory => 537204bgsave_in_progress => 0redis_version => 2.0.0multiplexing_api => epolltotal_connections_received => 314db0 => {keys=3,expires=0}pubsub_patterns => 0used_memory_human => 524.61Kpubsub_channels => 0uptime_in_seconds => 1033connected_slaves => 0connected_clients => 1bgrewriteaof_in_progress => 0blocked_clients => 0arch_bits => 32total_commands_processed => 3982hash_max_zipmap_value => 512db15 => {keys=1,expires=0}uptime_in_days => 0]]

Dependencies

Links

Project

Related

Authors

Daniele Alessandri

Contributors

Leo Ponomarev

License

The code for redis-lua is distributed under the terms of the MIT/X11 license (see LICENSE).

About

A Lua client library for the redis key value storage system.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages