[英]How do I call Redis StringSet() from F#
I am using StackExchange.Redis to access a Redis instance. 我正在使用StackExchange.Redis来访问Redis实例。
I have the following working C# code: 我有以下工作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");
}
Here is the what I would hope would be the equivalent F# code: 这是我希望的等效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
However this does not compile - 'No overloads match for method StringSet'. 但是这不会编译 - '没有重载匹配方法StringSet'。 The StringSet method expects arguments of type RedisKey and RedisValue, and there seems to be some compiler magic going on in C# to convert the strings in the calling code into RedisKey and RedisValue. StringSet方法需要RedisKey和RedisValue类型的参数,并且在C#中似乎有一些编译器魔法将调用代码中的字符串转换为RedisKey和RedisValue。 The magic does not appear to exist in F#. F#中似乎不存在魔法。 Is there a way of achieving the same result? 有没有办法达到同样的效果?
Here is the working code, many thanks to @Daniel: 这是工作代码,非常感谢@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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.