{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_HADDOCK prune not-home #-}

{- |
Copyright   : (c) 2018-2022 Tim Emiola
SPDX-License-Identifier: BSD3
Maintainer  : Tim Emiola <tim@emio.la>

Implements a 'Handle' that uses Redis as a persistent data store.
-}
module KeyedVals.Handle.Redis (
  -- * create a Handle
  new,

  -- * module re-exports
  module KeyedVals.Handle,
) where

import Control.Monad.IO.Unlift (MonadUnliftIO, liftIO)
import KeyedVals.Handle
import KeyedVals.Handle.Redis.Internal
import UnliftIO.Exception (throwIO)


{- | Construct a 'Handle' that uses Redis.

The instance is configured using the environment variable @REDIS_URL@.

If the value of @REDIS_URL@ is invalid or missing, this function throws an IOError.

The Handle uses a pool of connections; the size of the pool can be adjusted
by setting the environment variable @REDIS_MAX_CONNECTIONS@.
-}
new :: MonadUnliftIO m => m (Handle m)
new :: forall (m :: * -> *). MonadUnliftIO m => m (Handle m)
new =
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (Maybe ConnectInfo)
readEnvConnectInfo forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    (Just ConnectInfo
i) -> forall (m :: * -> *).
MonadUnliftIO m =>
ConnectInfo -> m (Handle m)
fromConnectInfo ConnectInfo
i
    Maybe ConnectInfo
Nothing -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO forall a b. (a -> b) -> a -> b
$ String -> IOError
userError String
"could not create a Handle that uses redis"