Version: v0.9.0

Cache / Redis

Cache systems help us to retrieve the data much faster than following the conventional path. This is especially significant when working with databases. We have already seen that getting data from the database is an expenseive task. In fact, this is usually one of the biggest bottlenecks in web applications.

Kalgan delegates cache support to the external crate kalgan_cache, which basically it's a wrapper for crate redis..

Configuring Redis

As we already said, the Cache system is based on crate Redis. Redis is an high-speed in-memory key-value database, cache system and message broker.

The first we must do is installing Redis in our machine. For Debian based systems we can do it as follows:

sudo apt install redis-server

Once installed, Redis should be already running. We can check it:

sudo systemctl status redis

If it's inactive we can start it as follows:

sudo systemctl start redis

Any issues regarding the installation and configuration of Redis is out of the scope of theses docs. If you're having troubles you might find a solution in Redis Official Site.

Using Redis

It's a fact that we're going to need to use Redis CLI to test that our code works as expected. However we're not going to spend too much time with Redis internals. You can find all the information in redis-cli, the Redis command line interface.

We can find below some of the basic commands to work with the data stored in Redis:

  • Openning the CLI in interactive mode:

    redis-cli
  • Check whether the server is running:

    127.0.0.1:6379> ping # PONG should be returned
  • Monitor all the commands received by a Redis instance in real time:

    127.0.0.1:6379> monitor
  • Print all the keys stored in Redis:

    127.0.0.1:6379> keys *
  • Delete all the keys:

    127.0.0.1:6379> flushdb
  • Get value by key:

    127.0.0.1:6379> get {key}
  • Delete data by key:

    127.0.0.1:6379> del {key}

Configuring Cache

Now that we have Redis up and running, we must configure Kalgan. Firstly, we must add the feature cache in Cargo.toml:

...
[dependencies]
kalgan = { version = "0.9.0", features = ["cache"] }

And then we must set in our settings file the server address of Redis. For example:

...
cache:
  server: redis://127.0.0.1/
...

Using Cache

Kalgan provides the struct kalgan::cache::Cache :

use redis::Connection;

pub struct Cache {
    connection: Connection
}

This object count with the following public methods:

Cache::new() Creates the instance. We must pass cache.server as argument.
Cache.insert() Inserts a record in Redis database with the given arguments key-value.
Cache.delete() Deletes a record in Redis database by key.
Cache.exists() Checks if a record in Redis database exists.
Cache.get() Gets a record in Redis database by key.

Working with cache values is pretty straightforward. Let's see an example:

use kalgan::http::{ request::Request, response::Response };
use kalgan::cache::Cache;
use kalgan::settings;

pub(crate) fn foobar(request: &Request) -> Response {
    ...
    let mut cache = Cache::new(settings::get_string("cache.server").unwrap());
    cache.insert("Key1", "Hello World");
    dbg!(cache.get("Key1").unwrap()); // returns "Hello World"
    ...
}