gm_redis - Redis Server Interface
We commisioned it, and MetaMan saved the day!
This module provides full interactivity to your Redis server, it’s pretty cut-and-dry. Ridiculously fast in-memory data store or key-value database (with some config), great for hugely fast cross-server interoperability vs SQL for things that don’t require a schema, and doubles as a message broker so you can broadcast messages in a timely fashion to any number of connected servers.
Let’s get some documentation:
[lua]
– Current version
redis.Version
– Current version number
redis.VersionNum
[/lua]
Client object, used for commands:
[lua]
– Creates a redis_client object
redis.CreateClient()
– returns object redis_client, string error
– Connects to the database
redis_client:Connect(string ip, number port)
– returns bool success, string error
– Disconnects the database
redis_client:Disconnect(string ip, number port)
– Sends a command & arguments and calls the callback on completion
redis_client:Send(table command_args, function callback)
– returns bool success
– Commits your commands to the server (you must call this)
redis_client:Commit()
– returns bool success, string error
– Polls the database (the module doesn’t call this for you like mysql modules)
redis_client:Poll()
– returns bool success
– Is the database connected?
redis_client:IsConnected()
– returns bool connected
– Called when the databse is disconnected (handle reconnections here)
redis_client:OnDisconnected()
– Example:
local client = redis.CreateClient()
client:Connect(‘localhost’, 6379)
client:Send({‘SET’, ‘MyKey’, ‘MyValue’})
client:Send({‘GET’, ‘MyKey’}, function(self, value)
print(self, value)
end)
client:Commit() – commit both commands, you can easily do thousands at a time if you wanted with little to no performance impact
hook.Add(‘Think’, client, function()
redis_client:Poll()
end)
[/lua]
Subscriber object, used to listen for channel broadcasts:
[lua]
– Creates a redis_subscriber object
redis.CreateSubscriber()
– returns object redis_subscriber, string error
– Connects to the database
redis_subscriber:Connect(string ip, number port)
– returns bool success, string error
– Disconnects the database
redis_subscriber:Disconnect(string ip, number port)
– Subscribe to a channel
redis_subscriber:Subscribe(string channel, function callback)
– returns bool success, string error
– Unsubscribe from a channel
redis_subscriber:Unsubscribe(string channel, function callback)
– returns bool success, string error
– Subscribe to a channel
redis_subscriber:PSubscribe(string channel, function callback)
– returns bool success, string error
– Unsubscribe from a channel
redis_subscriber:PUnsubscribe(string channel, function callback)
– returns bool success, string error
– Commits your commands to the server (you must call this)
redis_subscriber:Commit()
– bool success, string error
– Polls the database (the module doesn’t call this for you like mysql modules)
redis_subscriber:Poll()
– returns bool success
– Is the database connected?
redis_subscriber:IsConnected()
– returns bool connected
– Called when the database is disconnected (handle reconnections here)
redis_subscriber:OnDisconnected()
– Called when a message is received
redis_subscriber:OnMessage(string channel, string message)
– Example:
local subscriber = redis.CreateSubscriber()
subscriber.OnMessage = function(self, channel, message)
print(self, channel, message)
end
subscriber:Connect(‘localhost’, 6379)
subscriber:Subscribe(‘MyChannel’)
subscriber:Commit()
hook.Add(‘Think’, subscriber, function()
redis_subscriber:Poll()
end)
client:Send({‘PUBLISH’, ‘MyChannel’, ‘Hello world!’}) – You use redis_client from above to publish to channels. redis_subscriber is just for listening
client:Commit()
[/lua]
You should probably have some basic understanding of Redis before trying to use the module.
Downloads:
Git: https://github.com/SuperiorServers/gm_redis
Binaries: https://github.com/SuperiorServers/gm_redis/releases
Example extension/wrapper #1: https://github.com/SuperiorServers/gm_redis/blob/master/includes/modules/redis.lua
Example extension/wrapper #2: https://github.com/SuperiorServers/dash/tree/master/lua/dash/libraries/server/redis