I'm looking for how to connect to a redis server via Unix domain socket using hedis, as advertised in the hackage page:
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.
From the constructors of ConnectInfo
, as well as the defaultConnectInfo
, it seems that we should fill in the connectPort
, since it has type PortID
which has a constructor named UnixSocket
. But it only shows UnixSocket
is a String
, without details of format, etc.
So how to fill in the connectPort
to connect via Unix domain socket? Thanks.
UPDATE: I tried it out and found it's not that hard. Below is my hello world.
{-# 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)
Best Answer
I'm not a Haskell user at all, and I cannot test it, but I would say you just have to provide the path of the socket file in this string.
Instead of:
you would have:
Of course, /tmp/redis.sock should be declared in the server-side Redis configuration file using the following parameters:
Please note by default, unix domain socket parameters are commented out.