f# - 我如何从F#调用Redis StringSet()

标签 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");
}

这是我希望将是等效的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

但是,这不会编译-'方法StringSet的重载不匹配'。 StringSet方法需要使用RedisKey和RedisValue类型的参数,并且C#中似乎发生了一些编译器魔术,将调用代码中的字符串转换为RedisKey和RedisValue。 F#中似乎不存在该魔术。有没有办法达到相同的结果?

最佳答案

这是工作代码,非常感谢@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"

关于f# - 我如何从F#调用Redis StringSet(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27274702/

相关文章:

f# - 数值基元及其后缀

function - 柯里化(Currying)一个函数以获得另一个函数 : unit -> 'a

azure - 在 Azure 上托管的 .net Core 应用程序上连接到 Redis 时出错

c# - 如何使用 GetEndPoints() 获取唯一端点列表

c# - stackexchange redis缓存性能

c# - StackExchange.Redis的正确使用方法

f# - 用于 Visual Studio Code 的 Ionide-fsharp

F# 递归终止

f# - 编写轮询 Windows 服务

redis - ConnectionMultiplexer.Connect 在连接到 Redis 服务器时中断