我正在寻找如何使用
hedis通过Unix域套接字连接到redis服务器,如hackage页面中所宣传的那样:
Connect via TCP or Unix Domain Socket:
TCP sockets are the default way
to connect to a Redis server. For connections to a server on the same
machine,Unix domain sockets offer higher performance than the
standard TCP connection.
从ConnectInfo
的构造函数,以及defaultConnectInfo
,似乎我们应该填写connectPort,因为它的类型PortID有一个名为UnixSocket的构造函数.但它只显示UnixSocket是一个字符串,没有格式细节等.
那么如何通过Unix域套接字填写connectPort进行连接?谢谢.
更新:我试了一下,发现并不难.以下是我的问候世界.
{-# LANGUAGE OverloadedStrings #-} import Control.Monad.Trans import Database.Redis myConnectInfo :: ConnectInfo myConnectInfo = defaultConnectInfo { connectPort = UnixSocket "/tmp/redis.sock" } main :: IO () main = do conn <- connect myConnectInfo runRedis conn $do set "hello" "hello" set "world" "world" hello <- get "hello" world <- get "world" liftIO $print (hello,world)
我根本不是Haskell用户,我无法测试它,但我会说你必须在这个字符串中提供套接字文件的路径.
代替:
connectPort = PortNumber 6379
你将会拥有:
connectPort = UnixSocket "/tmp/redis.sock"
当然,应使用以下参数在服务器端Redis配置文件中声明/tmp/redis.sock:
# Specify the path for the unix socket that will be used to listen for # incoming connections. There is no default,so Redis will not listen # on a unix socket when not specified. # unixsocket /tmp/redis.sock unixsocketperm 755
请注意,默认情况下,unix域套接字参数已注释掉.