Created
March 16, 2022 21:25
-
-
Save jkone27/cb73740c9892cad709bdd28768fcd713 to your computer and use it in GitHub Desktop.
learning redis
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
#r "nuget: StackExchange.Redis" | |
open StackExchange.Redis | |
open System.Threading.Tasks | |
// https://stackexchange.github.io/StackExchange.Redis/Basics | |
let redis = ConnectionMultiplexer.Connect("localhost") | |
let db = redis.GetDatabase() | |
let set values = | |
values | |
|> List.map (fun (k,v) -> k |> RedisKey, v.ToString() |> RedisValue ) | |
|> dict | |
|> Seq.toArray | |
|> db.StringSetAsync | |
let get keys = | |
keys | |
|> Seq.map RedisKey | |
|> Seq.toArray | |
|> db.StringGetAsync | |
let workflow () = | |
task { | |
let! setResult = | |
[ | |
"TEST_A", 100 | |
"TEST_B", 200 | |
] | |
|> set | |
let! getResult = [ "TEST_A" ] |> get | |
return getResult | |
} | |
workflow().GetAwaiter().GetResult() | |
//PUBSUB | |
let sub = redis.GetSubscriber() | |
let channel = sub.Subscribe("TEST_PUB_SUB") | |
do channel.OnMessage(fun channelMessage -> | |
task { | |
let! wait = Task.Delay(1000) | |
printfn "%A" channelMessage.Message | |
} :> Task | |
) | |
sub.Publish("TEST_PUB_SUB", "hello") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment