Code Monkey home page Code Monkey logo

redis-async's Introduction

Redis Async

A Redis client library for OCaml Async applications. Provides a strongly-typed API with transparent (de)serialization for application-defined types. Includes support for client tracking.

Compatible with Redis versions 6 and higher. Internally, implements the RESP3 protocol.

Example Usage

Here are a few basic examples. Please see the included tests for additional detail and features.

Plain

Redis natively works with plain strings for many commands. Users of other Redis clients will be most familiar this style of interaction.

open Core
open Async
open Deferred.Or_error.Let_syntax

module Redis = Redis.Make (Redis.Bulk_io.String) (Redis.Bulk_io.String) in
let%bind redis =
  Redis.create
    ~where_to_connect:
      (Tcp.Where_to_connect.of_host_and_port
         (Host_and_port.create ~host:"localhost" ~port:6379))
    ()
in
let%map reply = Redis.echo redis "Hello, world!" in
print_endline reply

Typeful

Application-defined types can be used directly as Redis keys and values. Convenience functors that implement the required (de)serialization for Stringable and Binable types are provided.

open Core
open Async
open Deferred.Or_error.Let_syntax

module Key = struct
  module T = struct
    type t = { name : string }

    let to_string { name } = name
    let of_string name = { name }
  end

  include T
  include Redis.Bulk_io.Make_stringable (T)
end

module Value = struct
  module T = struct
    type t =
      { color  : string
      ; number : int
      }
    [@@deriving bin_io, sexp]
  end

  include T
  include Redis.Bulk_io.Make_binable (T)
end

module Redis = Redis.Make (Key) (Value) in
let%bind redis =
  Redis.create
    ~where_to_connect:
      (Tcp.Where_to_connect.of_host_and_port
         (Host_and_port.create ~host:"localhost" ~port:6379))
    ()
in
let key   = { Key.name    = "Jane"              } in
let value = { Value.color = "blue"; number = 42 } in
let%bind ()    = Redis.set redis key value in
let%map  value = Redis.get redis key       in
print_s ([%sexp_of: Value.t option] value)

redis-async's People

Contributors

aalekseyev avatar dwwoelfel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.