Redis is a really fast key value store that has many different applications, from leaderboards to caching, Redis is like the swiss army knife of the web.

Let's take a look at how easy it is to use Redis with Dart!

First in your pubspec.yaml file add redis_client as a dependency

dependencies: redis_client: any

Now you can directly access RedisClient but lets make things a bit easier. First create a class, call it "Redis".

The purpose of this class is to wrap up the connection string and keep it in one place, rather than pass around your redis username/password across your codebase.

It also lets you use one instance of the RedisClient rather than creating an instance each time you want to use Redis.

library db.redis;

import 'dart:async';
import 'dart:io';

import 'package:redis_client/redis_client.dart';

class Redis {
  static String connectionString = "YOURCONNECTIONSTRING";

  static RedisClient client;
  static Future<RedisClient> getRedis() async {
    if (client != null) {
      return client;
    } else {
      client = await RedisClient.connect(connectionString);
      return client;

It's an easy to follow implementation, we have a static async method called getRedis, which checks if we have the client already initialized, if so we return the client if not we connect to it. Pretty simple to follow along!

Next comes the cool part. How do you use this class?


  RedisClient redis = await Redis.getRedis();
  await redis.set("key","val");
  String val = await redis.get("key");

It's that easy!