如何从f#调用Redis StringSet() ?

时间:2022-10-14 20:58:01

I am using StackExchange.Redis to access a Redis instance.

我使用课件。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'. 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. The magic does not appear to exist in F#. Is there a way of achieving the same result?

然而,这并没有编译—“没有重载匹配方法StringSet”。StringSet方法期望类型RedisKey和RedisValue的参数,并且在c#中似乎有一些编译器魔术,将调用代码中的字符串转换为RedisKey和RedisValue。魔法在f#中似乎不存在。是否有办法达到同样的效果?

1 个解决方案

#1


11  

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"

#1


11  

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"