Skip to content

Instantly share code, notes, and snippets.

@xiaoDC
Forked from bkonkle/RedisProvider.re
Created March 26, 2018 09:29
Show Gist options
  • Save xiaoDC/669af5c1a5d4b37fe53f979df949bc08 to your computer and use it in GitHub Desktop.
Save xiaoDC/669af5c1a5d4b37fe53f979df949bc08 to your computer and use it in GitHub Desktop.
type ioRedis;
[@bs.new] [@bs.module] external ioRedis : unit => ioRedis = "ioredis";
[@bs.send] external set : (ioRedis, string, string, string, int) => Js.Promise.t(string) = "";
[@bs.send] external get : (ioRedis, string) => Js.Promise.t(Js.Nullable.t(string)) = "";
[@bs.send] external on : (ioRedis, string, unit => unit) => unit = "";
type t = {
set: (~key: string, ~ttl: int, string) => Js.Promise.t(string),
get: (~key: string) => Js.Promise.t(option(string))
};
/**
* Initialize a new Redis provider
*/
let make = () : Js.Promise.t(t) => {
let client = ioRedis();
Js.Promise.make(
(~resolve, ~reject) => {
on(
client,
"ready",
() =>
[@bs]
resolve({
set: (~key, ~ttl, value) => set(client, key, value, "PX", ttl),
get: (~key) =>
get(client, key)
|> Js.Promise.then_((data) => Js.Promise.resolve(Js.Nullable.to_opt(data)))
})
|> ignore
);
on(client, "error", (err) => [@bs] reject(err))
}
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment