Using Redis with Magic is really simple. Magic handles the lifecycle of the Redis container so you can focus on your application logic.
1. Import the redis package from Magic that provides the driver using the following command:
go get -u github.com/Liphium/magic/pkg/databases/redis@latest
2. You can now use the driver in your code like this:
driver := redis.NewDriver("redis:8") // Supports Redis 7 and 8
If you want to now register the driver, it is just the following code in your PlanDeployment function:
ctx.Register(driver)
Learn more here if you don’t know what that is yet.
As with every driver in Magic, the Redis driver provides environment variables you can use to connect to the instance (ctx is your mconfig.Context):
driver.Host(ctx): The hostname of the database (returns 127.0.0.1).driver.Port(ctx): The port of the Redis container on your local system.driver.Password(): The password for the Redis server (always "redis").While some values might seem predictable, it’s still best practice to use these methods instead of hardcoding values, as the underlying networking or defaults might change in future versions of Magic.
The Redis driver supports Magic’s instruction system to clear all data. Both “clear tables” and “drop tables” (methods on the runner) will lead to the entire Redis data being dropped.
Magic v3