MyNixOS website logo
Description

An intuitive hedis wrapper library.

Redis

Reviewed last on 2020-10-14

This library provides functions for working with Redis and JSON-serialized haskell records.

Because JSON encoding is quite lenient for containers (e.g. lists, maps), using Hedis alone makes it easy to accidentally write a record to redis, and only later discover that, when reading, you actually get back a list of that record.

This library helps avoid those problem by levering specific Key-Value mapping that we call an API.

At present this library implements a subset of the available redis commands. If you miss a command please feel free to add it!

How to use me

  1. make a handler

(optional) Set up some environmental variables.

these are the defaults:

REDIS_CONNECTION_STRING=redis://localhost:6379
REDIS_CLUSTER=0 # 1 is on
REDIS_DEFAULT_EXPIRY_SECONDS=0 # 0 is no expiration
REDIS_QUERY_TIMEOUT_MILLISECONDS=1000 # 0 is no timeout
main : IO ()
main =
  -- use nri-env-parser to parse settings from the environment
  settings <- Environment.decode Redis.decoder
  -- get a handler
  Conduit.withAcquire (Redis.handler settings) <| \redisHandler ->
    callSomeRedisFuncs redisHandler
  1. Define a codec type
data Key = Key
  { userId :: Int,
    quizId :: Int
  }

data User =
  User {
    name :: Text,
    favoriteColor :: Text
  }
  deriving (Generic)
-- payload needs this!
instance Aeson.ToJSON User
instance Aeson.FromJSON User

-- using this enforces this key-value mapping
redisApi :: Redis.Api Key User
redisApi =
  Redis.Hash.jsonApi toKey
  where
    toKey :: Key -> Text
    toKey Key {userId, quizId} =
      Text.join
        "-"
        [ "quiz",
          Text.fromInt userId,
          Text.fromInt quizId,
          "v1" -- good idea to version your keys!
        ]
  1. use the codec to read and writee!
_ <- Redis.query handler (Redis.set redisApi (Key 1 2) (User "alice" "orange"))
Redis.query (Redis.get redisApi (Key 1 2))  -- Just (User "alice" "orange")
-- or, perhaps better
[ (Redis.set redisApi (Key 1 2) (User "alice" "orange"))
, (Redis.get redisApi (Key 1 2))
]
  |> Redis.sequence
  |> Redis.transaction -- Transaction ensures these commands are run together

FAQ

Your default env variables cause a collision. What do I do?

add a prefix, and use decoderWithEnvVarPrefix instead of decoder


Metadata

Version

0.1.0.4

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows