Recently i decided to move my blog from tumblr.com
to octopress engine
because it is just easier for me to maintain it and it looks nicer. The old blog is under http://no-fucking-idea.tumblr.com. My first post on new blog is dedicated to using redis with erlang.
Eredis
Wooga created a really nice (performance driven) redis driver for erlang. You can get it here https://github.com/wooga/eredis. It is really easy and nice.
Initial sample
On project page you can find simple examples how to use eredis. Examples there are all you need (but i need something for front page of my new blog so i will rewrite them and explain them :) ).
First you need to start your eredis application
1
|
|
Client is the “connection / state” we will be using with rest of queries.
To query things with redis we will use q
method from eredis module which takes “Connection / Client” state and list with params.
This api is very simple here are two most basic examples of get and set.
GET:
1
|
|
and SET:
1
|
|
From my point of view this is ideal candidate to be turned into gen_server behavior. We will pass “Connection / Client” as state and also we will build some “key” serialization methods around it to make it more durable and make our life easy if we will decide to refactor it later on.
Free Api wrapper
First thing i saw during development using Erlang is that you get free api if you follow simple patterns and encapsulate things into gen_server’s and applications.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
Public Api
This code listing has two important parts first at top it starts at line 26. This is the public API which will be used by developer. This is this free api. Later on i will explain how to change redis to mongodb and probably to other db engines without doing changes in rest of our app. From my perspective this is awesome feature. In most cases when i had to make app scale problem of having code that was glues to one db engine was heavy.
eunit tests
At line 60. starts the declaration of tests, using rebar
and eunit
is very easy and it is always good to have test coverage in case of refactoring. I’m a fan of test driven development so i like to cover in tests first things that i will use or i think they might be error prone. Here is used “test generators” to just to show how to write tests for gen_server.
Rebar
Before i will explain more i need to say little about rebar
. It is a command line tool that was developed by basho
to help create app. it is by far the best thing i found learning erlang to help me do boring stuff and eliminate a lot of rage writing app.src
files. To get rebar simply do (you can always go to https://github.com/basho/rebar to get most up to date informations about building it)
1 2 3 |
|
I use my own set of zsh scripts so all i did to add it to my path was to edit .furby
file in my home dir. I strongly suggest also adding it to $PATH
just to make your life easier.
Back to example_db!
To create app using rebar you just need to
1 2 3 4 5 6 |
|
This command created src
folder with scaffold of application
OTP pattern and supervisor
thats almost all we need :).
Now you can compile it using rebar compile
and run tests using rebar compile eunit
in out app currently we will see
1 2 3 4 5 6 7 8 |
|
Nothing to do because its empty. Lets add our db module.
But before this we need to add dependencies for eredis module. Lets create rebar.config
file and add it.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Now just run rebar get-deps
to get all dependencies downloaded.
After adding our example_db.erl
into src
directory we can run rebar compile eunit
to compile and run tests. We have added {cover_enabled, true}
in rebar.conf so also test code coverage will be generated for us.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
All seems to be fine! lets create file called start.sh
to test it out
1 2 |
|
and make it executable with chmod +x start.sh
And lets rock ‘n’ roll
1 2 3 4 5 6 7 8 9 10 |
|
Have fun :) Hope it was useful. You can download code for this blog post here https://github.com/JakubOboza/example_db-code
Huh ^___^
that was my first post on new blog ;)