Kit*_*Kit 10 f# stackexchange.redis
我正在使用StackExchange.Redis来访问Redis实例.
我有以下工作C#代码:
public static void Demo()
{
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("xxx.redis.cache.windows.net,ssl=true,password=xxx");
IDatabase cache = connection.GetDatabase();
cache.StringSet("key1", "value");
}
Run Code Online (Sandbox Code Playgroud)
这是我希望的等效F#代码:
let Demo() =
let cx = ConnectionMultiplexer.Connect @"xxx.redis.cache.windows.net,ssl=true,password=xxx"
let cache = cx.GetDatabase()
cache.StringSet("key1", "value") |> ignore
Run Code Online (Sandbox Code Playgroud)
但是这不会编译 - '没有重载匹配方法StringSet'.StringSet方法需要RedisKey和RedisValue类型的参数,并且在C#中似乎有一些编译器魔法将调用代码中的字符串转换为RedisKey和RedisValue.F#中似乎不存在魔法.有没有办法达到同样的效果?
Kit*_*Kit 12
这是工作代码,非常感谢@Daniel:
open StackExchange.Redis
open System.Collections.Generic
let inline (~~) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit: ^a -> ^b) x)
let Demo() =
let cx = ConnectionMultiplexer.Connect @"xxx.redis.cache.windows.net,ssl=true,password==xxx"
let cache = cx.GetDatabase()
// Setting a value - need to convert both arguments:
cache.StringSet(~~"key1", ~~"value") |> ignore
// Getting a value - need to convert argument and result:
cache.StringGet(~~"key1") |> (~~) |> printfn "%s"
Run Code Online (Sandbox Code Playgroud)