redis 101 in a No-line F# script
Redis is a popular (the most popular?) key-value store db, with support for pubsub, streaming, and a ton of other features via its extensible modules.
And the stack-exchange client is the de-facto battle-tested and future proof dotnet redis driver for your app.
Here is the gist of it, following this awesome tutorial.
Setup Dotnet-sdk and Ionide (skip if you have it!)
brew install dotnet // or google install dotnet-sdk for other osbrew install --cask visual-studio-code // if you don't have it yet
And only thing left, install ionide extension for vscode, verify the dotned installation with
dotnet --version //prints the version, if so, all good!
now you are ready to write .fsx files in F#, which is the most awesome dotnet language.
Just to be sure as f. doublecheck that fsharp interactive works too…
dotnet fsi"hello world!";; //prints hello world to interactive it! cool#quit;; //to exit
Spin up Redis
First thing first, run redis on your local docker instance, or install redis locally if that’s your preference (chose docker!), don’t forget to publish the redis default port 6379 for your local apps to talk to your doocker redis instance.
docker run --name maredis -p 6379:6379 -d redis
you should see something like this in your docker hub
now to check redis logs in your container you can execute/attach an interactive session (sh) in your container,
this is exactly equivalent to running from a terminal… (just a shortcut)
docker exec -it <YOUR_CONTAINER_ID> /bin/sh
And then run the redis monitor command via redis-cli which is the command line interface to your redis instance
redis-cli monitor
this will show you live what’s going on inside the belly of the whale! (redis in docker), a conforting OK message will tap on your shoulder telling you all is fine and well, like in the best fairy tales.
The Code
Create a new fsx script file in the folder of your choice, and start up vscode
mkdir test_dir && cd test_dir && touch test_redis.fsx && code .
from here follow along and play around, pics should be self explanatory..
you can run and execute portions of your code with alt/option + enter.
Connect
Database and Repository
Spinning the db for a run in this small task workflow, I am using here the async APIs here (StringSetAsync, StringGetAsync) but sync apis could also be used, as they are simpler…
PubSub
Monitoring Results
the messages are mixed as i ran first the pubsub example “PING”, then the database example “MSET”, “MGET”, seeing redis responding and processing it’s batch of work independently and with no issues!
Here again link to the complete file/gist if you wanna try it out.