Safe Haskell | None |
---|---|
Language | Haskell2010 |
A simple Redis library providing high level access to Redis features we use here at NoRedInk
As with our Ruby Redis access, we enforce working within a "namespace".
Synopsis
- handler :: Text -> Settings -> Acquire Handler
- data Handler
- data Settings = Settings {
- connectionInfo :: ConnectInfo
- clusterMode :: ClusterMode
- defaultExpiry :: DefaultExpiry
- queryTimeout :: QueryTimeout
- maxKeySize :: MaxKeySize
- decoder :: Decoder Settings
- jsonApi :: forall a field key. (ToJSON a, FromJSON a, Ord field) => (key -> Text) -> (field -> Text) -> (Text -> Maybe field) -> Api key field a
- textApi :: Ord field => (key -> Text) -> (field -> Text) -> (Text -> Maybe field) -> Api key field Text
- byteStringApi :: Ord field => (key -> Text) -> (field -> Text) -> (Text -> Maybe field) -> Api key field ByteString
- data Api key field a
- del :: Api key field a -> NonEmpty key -> Query Int
- exists :: Api key field a -> key -> Query Bool
- expire :: Api key field a -> key -> Int -> Query ()
- ping :: Api key field a -> Query ()
- hdel :: Api key field a -> key -> NonEmpty field -> Query Int
- hget :: Api key field a -> key -> field -> Query (Maybe a)
- hgetall :: Api key field a -> key -> Query (Dict field a)
- hkeys :: Api key field a -> key -> Query (List field)
- hmget :: Api key field a -> key -> NonEmpty field -> Query (Dict field a)
- hmset :: Api key field a -> key -> NonEmptyDict field a -> Query ()
- hset :: Api key field a -> key -> field -> a -> Query ()
- hsetnx :: Api key field a -> key -> field -> a -> Query Bool
- query :: HasCallStack => Handler -> Query a -> Task Error a
- transaction :: HasCallStack => Handler -> Query a -> Task Error a
- data Query a
- data Error
- = RedisError Text
- | ConnectionLost
- | DecodingError Text
- | DecodingFieldError Text
- | LibraryError Text
- | TransactionAborted
- | TimeoutError
- | KeyExceedsMaxSize Text Int
- map :: (a -> b) -> Query a -> Query b
- map2 :: (a -> b -> c) -> Query a -> Query b -> Query c
- map3 :: (a -> b -> c -> d) -> Query a -> Query b -> Query c -> Query d
- sequence :: List (Query a) -> Query (List a)
Creating a redis handler
handler :: Text -> Settings -> Acquire Handler Source #
Produce a namespaced handler for Redis access.
Settings required to initiate a redis connection.
Settings | |
|
Creating a redis API
jsonApi :: forall a field key. (ToJSON a, FromJSON a, Ord field) => (key -> Text) -> (field -> Text) -> (Text -> Maybe field) -> Api key field a Source #
Creates a json API mapping a key
to a json-encodable-decodable type
data Key = Key { fieldA: Text, fieldB: Text } data Val = Val { ... } myJsonApi :: Redis.Api Key Val myJsonApi = Redis.jsonApi (\Key {fieldA,
textApi :: Ord field => (key -> Text) -> (field -> Text) -> (Text -> Maybe field) -> Api key field Text Source #
Creates a Redis API mapping a key
to Text
byteStringApi :: Ord field => (key -> Text) -> (field -> Text) -> (Text -> Maybe field) -> Api key field ByteString Source #
Creates a Redis API mapping a key
to a ByteString
a API type can be used to enforce a mapping of keys to values. without an API type, it can be easy to naiively serialize the wrong type into a redis key.
Out of the box, we have helpers to support
- jsonApi
for json-encodable and decodable values
- textApi
for Text
values
- byteStringApi
for ByteString
values
Creating redis queries
del :: Api key field a -> NonEmpty key -> Query Int Source #
Removes the specified keys. A key is ignored if it does not exist.
expire :: Api key field a -> key -> Int -> Query () Source #
Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology.
ping :: Api key field a -> Query () Source #
Returns PONG if no argument is provided, otherwise return a copy of the argument as a bulk. This command is often used to test if a connection is still alive, or to measure latency.
hdel :: Api key field a -> key -> NonEmpty field -> Query Int Source #
Removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns 0.
hget :: Api key field a -> key -> field -> Query (Maybe a) Source #
Get the value of the field of a hash at key. If the key does not exist, or the field in the hash does not exis the special value Nothing is returned An error is returned if the value stored at key is not a hash, because HGET only handles string values.
hgetall :: Api key field a -> key -> Query (Dict field a) Source #
Returns all fields and values of the hash stored at key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash. Nothing in the returned value means failed utf8 decoding, not that it doesn't exist
hkeys :: Api key field a -> key -> Query (List field) Source #
Returns all field names in the hash stored at key. Empty list means key doesn't exist
hmget :: Api key field a -> key -> NonEmpty field -> Query (Dict field a) Source #
Returns the values associated with the specified fields in the hash stored at key.--
equivalent to modern hget https://redis.io/commands/hmget
hmset :: Api key field a -> key -> NonEmptyDict field a -> Query () Source #
Sets fields in the hash stored at key to values. If key does not exist, a new key holding a hash is created. If any fields exists, they are overwritten.
equivalent to modern hset https://redis.io/commands/hmset
hset :: Api key field a -> key -> field -> a -> Query () Source #
Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten.
hsetnx :: Api key field a -> key -> field -> a -> Query Bool Source #
Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.
Running Redis queries
query :: HasCallStack => Handler -> Query a -> Task Error a Source #
Run a Query
.
Note: A Query
in this library can consist of one or more queries in sequence.
if a Query
contains multiple queries, it may make more sense, if possible
to run them using transaction
transaction :: HasCallStack => Handler -> Query a -> Task Error a Source #
Run a redis Query in a transaction. If the query contains several Redis commands they're all executed together, and Redis will guarantee other requests won't be able change values in between.
In redis terms, this is wrappping the Query
in MULTI
and `EXEC
see redis transaction semantics here: https://redis.io/topics/transactions
A Redis query
Redis Errors, scoped by where they originate.
RedisError Text | |
ConnectionLost | |
DecodingError Text | |
DecodingFieldError Text | |
LibraryError Text | |
TransactionAborted | |
TimeoutError | |
KeyExceedsMaxSize Text Int |
map :: (a -> b) -> Query a -> Query b Source #
Used to map the type of a query to another type
useful in combination with transaction
map2 :: (a -> b -> c) -> Query a -> Query b -> Query c Source #
Used to combine two queries
Useful to combine two queries.
Redis.map2
(Maybe.map2 (,))
(Redis.get api1 key)
(Redis.get api2 key)
|> Redis.query redis