• Stars
    star
    720
  • Rank 62,908 (Top 2 %)
  • Language
    Lua
  • License
    MIT License
  • Created over 15 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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:

local redis = 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

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

local redis = require 'redis'
local client = redis.connect('unix:///tmp/redis.sock')

Set keys and get their values

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

Sort list values by using various parameters supported by the server

for _,v in ipairs({ 10,3,2,6,1,4,23 }) do
    client:rpush('usr:nrk:ids',v)
end

local sorted = client:sort('usr:nrk:ids', {
     sort = 'asc', alpha = true, limit = { 1, 5 }
})      -- {1=10,2=2,3=23,4=3,5=4}

Pipeline commands

local replies = 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):

local values = { '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)

local replies = 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)

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

Get useful information from the server

for k,v in pairs(client:info()) do
    print(k .. ' => ' .. tostring(v))
end
--[[
redis_git_dirty => 0
redis_git_sha1 => aaed0894
process_id => 23115
vm_enabled => 0
hash_max_zipmap_entries => 64
expired_keys => 9
changes_since_last_save => 2
role => master
last_save_time => 1283621624
used_memory => 537204
bgsave_in_progress => 0
redis_version => 2.0.0
multiplexing_api => epoll
total_connections_received => 314
db0 => {keys=3,expires=0}
pubsub_patterns => 0
used_memory_human => 524.61K
pubsub_channels => 0
uptime_in_seconds => 1033
connected_slaves => 0
connected_clients => 1
bgrewriteaof_in_progress => 0
blocked_clients => 0
arch_bits => 32
total_commands_processed => 3982
hash_max_zipmap_value => 512
db15 => {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).

More Repositories

1

phpiredis

PHP extension for Redis based on Hiredis
C
494
star
2

predis-async

Asynchronous PHP client library for Redis built on top of ReactPHP
PHP
365
star
3

mercury

... because Sinatra is not the only one performing on the stage.
Lua
102
star
4

PredisServiceProvider

Predis service provider for the Silex microframework
PHP
68
star
5

hige

{{growing mustaches in your templates with Lua}}
Lua
34
star
6

redis-rdb

A set of utilities to handle Redis .rdb files with Ruby.
Ruby
34
star
7

lamestnews

Lamest News is a port to PHP of the application that powers Lamer News.
PHP
25
star
8

monolog-fluent

A simple Monolog handler for Fluent
PHP
20
star
9

ironruby-hpricot

A port of Hpricot to IronRuby
C#
9
star
10

ironruby-json

A port of Florian Frank's json library to IronRuby
C#
9
star
11

hpricot-pure

Simply Hpricot, just made "pure".
Ruby
6
star
12

bencoder

A Bencode serializer and deserializer in pure PHP.
PHP
5
star
13

couchdb-lua-viewserver

A CouchDB view server that allows writing view functions in Lua
Lua
5
star
14

couchdb-io-viewserver

This is an experiment for a CouchDB view server implemented in the Io language just for the fun of it.
Io
5
star
15

lazybag

Lazy values for lazy tables.
Lua
4
star
16

rayak

IronRuby + Kayak HTTP server + Rack
Ruby
3
star
17

niseredis

Niseredis will make a fool of you by trying its best to mimic Redis.
PHP
2
star
18

nrk.github.com

My GitHub homepage
2
star
19

PBKDF2ServiceProvider

Password-Based Key Derivation Function (PBKDF2) service provider for the Silex microframework
PHP
2
star
20

macchiato

From CoffeeScript to JavaScript, passing through PHP while sipping a Macchiato...
PHP
1
star