nri-redis: An intuitive hedis wrapper library.

[ bsd3, library, web ] [ Propose Tags ] [ Report a vulnerability ]
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.2.0.2, 0.2.0.3
Change log CHANGELOG.md
Dependencies aeson (>=2.0 && <2.2), async (>=2.2.2 && <2.3), base (>=4.16.4.0 && <4.19), bytestring (>=0.10.8.2 && <0.12), conduit (>=1.3.0 && <1.4), containers (>=0.6.0.1 && <0.7), cryptohash-sha1 (>=0.11.101.0 && <0.12), haskell-src-meta (>=0.8.12 && <0.9), hedis (>=0.14.0 && <0.16), megaparsec (>=9.2.2 && <9.6), modern-uri (>=0.3.1.0 && <0.4), nri-env-parser (>=0.1.0.0 && <0.3), nri-observability (>=0.1.0 && <0.3), nri-prelude (>=0.1.0.0 && <0.7), pcre-light (>=0.4.1.0 && <0.4.2), resourcet (>=1.2.0 && <1.4), safe-exceptions (>=0.1.7.0 && <1.3), template-haskell (>=2.16 && <3.0), text (>=1.2.3.1 && <2.1), unordered-containers (>=0.2.0.0 && <0.3), uuid (>=1.3.0 && <1.4) [details]
License BSD-3-Clause
Copyright 2024 NoRedInk Corp.
Author NoRedInk
Maintainer haskell-open-source@noredink.com
Category Web
Home page https://github.com/NoRedInk/haskell-libraries/tree/trunk/nri-redis#readme
Bug tracker https://github.com/NoRedInk/haskell-libraries/issues
Source repo head: git clone https://github.com/NoRedInk/haskell-libraries(nri-redis)
Uploaded by michaelglass at 2024-12-02T09:19:04Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 666 total (29 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for nri-redis-0.2.0.3

[back to package description]

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
# we also support unix sockets via a scheme we provide:
# REDIS_CONNECTION_STRING=redis+unix:///path/to/redis.sock[?db=DB_NUM]

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