-
-
Save xiaoDC/669af5c1a5d4b37fe53f979df949bc08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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