You can extend the functionality of your Redis 4.x installation by writing custom modules in C using the Redis Module SDK. Since Redis 4.x is only available on Unix-based systems, you need to write your Redis modules on a Unix-like system such as MacOS and use compilers like gcc. (Redis for Windows is only supported up until Redis 3.2.) Your Redis module must be a Unix shared library. This shared library can be loaded into Redis when Redis is first started or can be loaded dynamically into an already-running instance of Redis.
I have attempted to document the process of writing a Redis module using gcc and using Visual Studio Code as my development environment. The example shown below comes right out of the Redis Module SDK.
Note that the Redis Module SDK is still under development. For example, it does not yet have an API that supports SET-based functions.
Prerequisites
Make sure that the Gnu gcc compiler is installed on the Mac. Open up a terminal and just enter the command
Redis Gui Mac
gcc
Open Microsoft’s Visual Studio Code. It’s helpful to install the official Microsoft C/C++ extension.
In this article, we will be installing Redis data store on Windows and Mac OS. While installing Redis on Windows, we will be using Bash on Ubuntu on Windows application to set up the Redis locally. In the mac system, we will be using Homebrew as well as.tar file to install it. Installing Redis is really simple on a Mac OS. Follow these steps and you are good to go:Download the package from Internet. This website uses cookies and other tracking technology to analyse traffic, personalise ads and learn how we can improve the experience for our visitors and customers. Since Redis 4.x is only available on Unix-based systems, you need to write your Redis modules on a Unix-like system such as MacOS and use compilers like gcc. (Redis for Windows is only supported up until Redis 3.2.) Your Redis module must be a Unix shared library. Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bitmaps.
Download the Source
Clone the Git repo for the Redis Module SDK. The main Github site is here. In a Terminal window, navigate to the directory where you want the Git repo to be downloaded to. Then enter the command
git clone https://github.com/RedisLabs/RedisModulesSDK.git
Modify the Source
After the source code is downloaded, edit the file rmutil/sds.h and change line 82 to
(Change the “void*” to “struct sdshdr##T*” in order to silence the Mac’s gcc compiler)
Build the Source and the Example Module
In the Terminal, go to the root directory of the Redis Module SDK, and just enter the command
make
This will build the single library (librmutil.a) that you need to link your custom modules with. It also builds the example that comes with the Redis Module SDK. It will also build the shared library (module.so) that is the custom module that you will load into Redis.
Using Visual Studio Code
Mac Redis Client
Run Visual Studio Code. Open the main directory that the Module SDK is in. We need to create JSON-based configuration files that tell Visual Studio Code how to build the application and how to run/debug the application. These configuration files go into the .vscode subdirectory under your project.
The tasks.json file will tell Visual Studio Code how to run the make command.
To run the example, you need to launch the command
/usr/local/bin/redis-4.0.6/bin/redis-server –loadmodule ./module.so
launch.json
tasks.json
Running the Module
In Visual Studio Code, run the debugger. This will launch a copy of Redis with your new module loaded. You can put breakpoints into your module’s code and watch Redis execute the module.
While the debugger is running a copy of Redis, open up a Terminal and run the redis-cli program. In redis-cli, enter the commands:
127.0.0.1:9979> EXAMPLE.HGETSET foo bar baz
(nil)
127.0.0.1:9979> EXAMPLE.HGETSET foo bar vaz
“baz”
127.0.0.1:9979> EXAMPLE.PARSE SUM 5 2
(integer) 7
127.0.0.1:9979> EXAMPLE.PARSE PROD 5 2
(integer) 10
127.0.0.1:9979> EXAMPLE.TEST
PASS
(nil)
127.0.0.1:9979> EXAMPLE.HGETSET foo bar vaz
“baz”
127.0.0.1:9979> EXAMPLE.PARSE SUM 5 2
(integer) 7
127.0.0.1:9979> EXAMPLE.PARSE PROD 5 2
(integer) 10
127.0.0.1:9979> EXAMPLE.TEST
PASS