gm_redis - Redis Server Interface!

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

nice, thank you

First I’ve ever heard of redis but cool nonetheless.

Just out of curiosity, what uses do you have for Redis that MySQL or a regular relational DB cannot serve?

I believe it’s all about speed in this case

It’s extremely fast, simple to use, great for caching, has libraries for pretty much everything, and can replace some of the more common uses for sockets with its message system.

So aside from messaging cases, why not use MySQL? It caches to memory, so the speeds vs Redis can’t be all that different, plus it is reliable with it’s data.

Forgive me, I am not really familiar on NoSQL options.

In webdev Redis (and others like memcached) is used for things like caching dynamic data and sessions, along with queueing for offloading expensive tasks (another process would be listening for events to run them asynchronously) and messaging. For garrysmod, it’s uses could be things like cross-server chat/sync, temporary storage of data, etc.

Nice, will be using this.

In addition to _Undefined’s response, it’s just a cleaner setup versus MySQL for data that’s meant to be temporary. SQL solutions may work but even as efficient or fast as MySQL gets, you’ll still be interpreting queries and using a data structure that’s meant for organized data. Redis is much more lightweight for that sort of use, strictly key-value, auto expiring keys. It’s just a cheaper platform to centralize the less critical data that you’d like multiple servers to access.

Nice work, thanks.
I’ve been looking for a longer version of a Redis module for GMod and now there is finally one.

What could cause this?

I’ve put the module on two servers. Loads fine on the first, fails to load on the second. Even including it directly like on the image results in that output^. The servers use the same scripts more or less.

What happens if the scripts in-use are identical?

Also, are OS versions different, or do you just have two different srcds directories on the same machine?

Same machine, different directories. I’ll try to make the scripts on the two instances identical when possible, though there shouldn’t be anything that could affect this. Other modules are loading fine.

:snip:

:snip:

Does it handle complex values like tables for example? Or should I serialize my data before sending to redis?

No