Quick Reference
Quick reference guide for all Redix operations
Getting the Interface
local Redix = exports.redix:GetInterface()Key-Value Operations
-- Save and retrieve
Redix.Save('key', {data = 'value'}, 3600) -- with expiration
Redix.Get('key', function(err, data) end)
Redix.GetMultiple({'key1', 'key2'})
Redix.SetMultiple({key1 = 'value1', key2 = 'value2'})
-- Key management
Redix.Exists('key')
Redix.GetKeys('pattern:*')
Redix.Invalidate('key')
Redix.InvalidateMultiple({'key1', 'key2'})
Redix.InvalidatePrefix('prefix')
Redix.Rename('oldkey', 'newkey')
Redix.Type('key')
-- Expiration
Redix.Expire('key', 3600)
Redix.TTL('key')
Redix.Persist('key')Hash Operations
-- Set and get
Redix.HSet('hash', 'field', 'value')
Redix.HSetMultiple('hash', {field1 = 'value1', field2 = 'value2'})
Redix.HGet('hash', 'field')
Redix.HGetMultiple('hash', {'field1', 'field2'})
Redix.HGetAll('hash')
-- Management
Redix.HDelete('hash', 'field1', 'field2')
Redix.HExists('hash', 'field')
Redix.HKeys('hash')
Redix.HValues('hash')
Redix.HLength('hash')
-- Numeric operations
Redix.HIncrementBy('hash', 'field', 10)
Redix.HIncrementByFloat('hash', 'field', 0.5)List Operations
-- Push and pop
Redix.LPush('list', 'value1', 'value2')
Redix.RPush('list', 'value1', 'value2')
Redix.LPop('list')
Redix.RPop('list')
-- Access
Redix.LRange('list', 0, -1) -- all elements
Redix.LIndex('list', 0)
Redix.LLength('list')
-- Modification
Redix.LSet('list', 0, 'newvalue')
Redix.LTrim('list', 0, 99)
Redix.LRemove('list', 0, 'value')Set Operations
-- Basic operations
Redix.SAdd('set', 'member1', 'member2')
Redix.SRemove('set', 'member1')
Redix.SMembers('set')
Redix.SIsMember('set', 'member1')
Redix.SCard('set')
-- Random and pop
Redix.SPop('set')
Redix.SRandMember('set', 5)
-- Set operations
Redix.SUnion({'set1', 'set2'})
Redix.SInter({'set1', 'set2'})
Redix.SDiff({'set1', 'set2'})Sorted Set Operations
-- Add and remove
Redix.ZAdd('zset', 100, 'member1', 200, 'member2')
Redix.ZRemove('zset', 'member1')
-- Retrieve by rank
Redix.ZRange('zset', 0, -1)
Redix.ZRevRange('zset', 0, 9) -- top 10
-- Retrieve by score
Redix.ZRangeByScore('zset', 0, 100)
-- Queries
Redix.ZCard('zset')
Redix.ZScore('zset', 'member')
Redix.ZRank('zset', 'member')
Redix.ZRevRank('zset', 'member')
-- Modifications
Redix.ZIncrBy('zset', 10, 'member')
Redix.ZCount('zset', 0, 100)
Redix.ZRemRangeByRank('zset', 0, 9)
Redix.ZRemRangeByScore('zset', 0, 100)Counter Operations
Redix.Increment('counter', 1)
Redix.Decrement('counter', 1)
Redix.IncrementFloat('counter', 0.5)JSON Operations
-- Basic operations
Redix.JSONSet('key', '$', {field = 'value'})
Redix.JSONGet('key', '$')
Redix.JSONGetMultiple('key', {'$.field1', '$.field2'})
Redix.JSONMGet({'key1', 'key2'}, '$')
Redix.JSONDel('key', '$.field')
-- Type and value operations
Redix.JSONType('key', '$.field')
Redix.JSONNumIncrBy('key', '$.counter', 1)
Redix.JSONNumMultBy('key', '$.value', 2)
-- Array operations
Redix.JSONArrAppend('key', '$.array', {'item1', 'item2'})
Redix.JSONArrIndex('key', '$.array', 'item1')
Redix.JSONArrInsert('key', '$.array', 0, {'newitem'})
Redix.JSONArrLen('key', '$.array')
Redix.JSONArrPop('key', '$.array', -1)
Redix.JSONArrTrim('key', '$.array', 0, 9)
-- String operations
Redix.JSONStrAppend('key', '$.string', 'append')
Redix.JSONStrLen('key', '$.string')
-- Object operations
Redix.JSONObjKeys('key', '$')
Redix.JSONObjLen('key', '$')
-- Utility
Redix.JSONToggle('key', '$.boolean')
Redix.JSONClear('key', '$.field')Pub/Sub Operations
-- Channel operations
Redix.Subscribe('channel', function(message)
print('Received:', message)
end)
Redix.Unsubscribe('channel')
Redix.Publish('channel', 'message')
-- Pattern operations
Redix.PSubscribe('pattern:*', function(channel, message)
print('From', channel, ':', message)
end)
Redix.PUnsubscribe('pattern:*')Utility Operations
-- Connection
Redix.Ping()
Redix.Info()
Redix.Info('stats')
-- Database
Redix.FlushDB()
Redix.FlushDB(false) -- synchronous
-- Custom commands
Redix.Execute('COMMAND', {'arg1', 'arg2'})
-- Direct access
local redisInstance = Redix.GetInstance()
-- Testing
Redix.Test()Common Patterns
Player Data
-- Save player
Redix.HSetMultiple('player:' .. id, {
name = 'John',
level = 50,
money = 10000
})
-- Load player
Redix.HGetAll('player:' .. id, function(err, data)
-- Use data
end)
-- Update field
Redix.HSet('player:' .. id, 'level', 51)
Redix.HIncrementBy('player:' .. id, 'money', 500)Leaderboards
-- Update score
Redix.ZAdd('leaderboard', score, playerName)
-- Get top 10
Redix.ZRevRange('leaderboard', 0, 9, 'WITHSCORES')
-- Get player rank
Redix.ZRevRank('leaderboard', playerName)Caching
-- Try cache first
Redix.Get('cache:' .. id, function(err, cached)
if cached then
return cached
end
-- Fetch from database
local data = FetchFromDB(id)
-- Cache for 5 minutes
Redix.Save('cache:' .. id, data, 300)
return data
end)Rate Limiting
Redix.Get('limit:' .. id, function(err, count)
if not count then
Redix.Save('limit:' .. id, 1, 60) -- 60 second window
return true
elseif count < 10 then
Redix.Increment('limit:' .. id)
return true
else
return false -- Rate limited
end
end)Sessions
-- Create session with 2-hour expiration
Redix.Save('session:' .. id, {
player = id,
started = os.time()
}, 7200)
-- Check session
Redix.Exists('session:' .. id)
-- Cleanup
Redix.Invalidate('session:' .. id)Error Handling
-- With callback
Redix.Get('key', function(err, data)
if err then
print('Error:', err)
return
end
if data then
-- Use data
else
print('Key not found')
end
end)
-- Without callback
local data = Redix.Get('key')
if data then
-- Use data
else
print('Key not found or error occurred')
endPerformance Tips
- Batch operations when possible
- Use hashes for related data instead of separate keys
- Set expiration on temporary data
- Use patterns for key naming
- Use counters for numeric values
- Use sorted sets for rankings
- Cache expensive queries
- Use pub/sub for real-time events
Key Naming Conventions
-- Entity types
'player:123'
'vehicle:456'
'gang:789'
-- With subtypes
'player:123:profile'
'player:123:inventory'
'player:123:stats'
-- Temporary data
'session:abc123'
'cache:query:xyz'
'temp:data:123'
-- Collections
'players:online'
'leaderboard:level'
'gang:789:members'Common Use Cases
- Player profiles: Hashes
- Inventory: JSON
- Leaderboards: Sorted Sets
- Online players: Sets
- Statistics: Hashes with HIncrementBy
- Sessions: Keys with expiration
- Cache: Keys with expiration
- Rate limiting: Keys with expiration and counters
- Cross-resource events: Pub/Sub
- Queues: Lists
- Tags/categories: Sets