This is the second part of the tutorial series, if you missed PART I, you might be able to catch up from here.
For the entirety of this part, we will be working on building the storage layer of our short link generator/forwarder, so the main steps we will be having are :
Setup the Store Service
Storage API design and Implementation
Unit & Integration Testing
Recall, we installed Redis and loaded the needed Go depencies in the previous chapter, so for this one we will be putting them to use.
Note : In a real world workload, Redis storage alone can easily be overwhelmed, so a traditional RDBMS like Postgres or Mysql is used as a background storage for the least requested data ( cold values ) And Redis is used only to hold hot values that are often used/requested and have to maintain a super fast retrieval time.
Turn up your editor, buckle up and there we go ! 🚀
II. 1. Store Service Setup 🛠
First thing first, we are gonna have to create the our store folder in the project, So go inside the project directory, create a sub-directory named store and go ahead and create 2 empty Go files : store_service.go and store_service_test.go (Where we will be writing unit tests for the storage later )
We will start by setting up our wrappers around Redis, the wrappers will be used as interface for persisting and retrieving our application data mapping.
After defining wrapper structs we can finally be able to initialize the store service, in this case our Redis client.
II. 2. Storage API design and Implementation ⚙️
Now that we have initialized successfully our data store service, it's time to think about what storage API to expose for our needs in particular for our shortener server.
The next step is obviously implementing our storage API, and will all our setup done, it should be fairly straightforward.
II. 3. Unit & Integration Testing 🧪
For preserving the best practices and avoiding unintentional regressions in the future, we are going to have to think about unit and integration tests for our storage layer implementation
Now let's go and install the testing tools we will need to this part.
go get github.com/stretchr/testify
Recall we initially created store_service_test.go file in the store directory, if you haven't done so yet, please go and do it.
Our project directory tree should look something similar to the tree below.