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
- makeApi :: (key -> Text) -> Api key
- data Api key
- del :: Api key -> NonEmpty key -> Query Int
- exists :: Api key -> key -> Query Bool
- expire :: Api key -> key -> Int -> Query ()
- ping :: Api key -> Query ()
- get :: Api key -> key -> Query (Maybe Int)
- incr :: Api key -> key -> Query Int
- incrby :: Api key -> key -> Int -> Query Int
- set :: Api key -> key -> Int -> Query ()
- 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
makeApi :: (key -> Text) -> Api key Source #
Creates a Redis API to help enforce consistent key serialization
myJsonApi :: Redis.Counter.Api Key myJsonApi = Redis.counter.makeApi (\Key {fieldA, fieldB}-> Text.join "-" [fieldA, fieldB, "v1"])
a API type can be used to help enforce a consistent key usage. Without an API type, it can be easy to naiively change key serialization.
Creating redis queries
del :: Api key -> NonEmpty key -> Query Int Source #
Removes the specified keys. A key is ignored if it does not exist.
expire :: Api key -> 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 -> 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.
get :: Api key -> key -> Query (Maybe Int) Source #
Get the value of key. If the key does not exist the special value Nothing is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.
incr :: Api key -> key -> Query Int Source #
Increments the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.
incrby :: Api key -> key -> Int -> Query Int Source #
Increments the number stored at key by increment. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.
set :: Api key -> key -> Int -> Query () Source #
Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.
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