This is an example project of how to set up a library for quick work with Redis servers.
Modify it up for servers you currently work with, use sbt publishLocal, and then you can add
"com.turdwaffle" %% "redislib" % "1.0"
(insert correct version)
as a dependency in your (local sbt) projects build.sbt file and start hget-ing!.
A wonderful thing about Scala, is that you can use any of the wonderful Java libraries out there at no additional cost. In Java, I've used the Jedis library quite a lot. A lot of back end infrastruce I use is built on Redis, and I like to have some CLI apps that can hit the backend in a similar fashion to how the production code is. To that extent, this project is meant to be a small example of porting over your own wrapper/code to a scala libary that you can use as a versioned/managed dependency for production and CLI code. The basic sctructure can lend itself to a Database library as well.
This project only has 2 dependencies:
- Jedis
- Guice
The core of the library resides in a single file: RedisProvider.scala
This file contains 4 main components:
- The RedisPool trait. This will set up some basic connection pooling parameters.
- A RedisHost class that extends RedisPool. This will allow us to set a hostname, and a password if any, for our connection (2 examples provided).
- A RedisProvider class extends guices AbstractModule. This will bind a named instance of our RedisHost that we can access.
- A RedisProvider object. We use this to access our guice injector and get isntances of our RedisHostss
The code is heavily commented, so please check there for how the parts work.
The file Main.scala shows a quick usage of the implementation.
From there, you have a good skeleton to insert you own often-used code!