Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Redis.SortedSet
Description
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
- handlerAutoExtendExpire :: Text -> Settings -> Acquire HandlerAutoExtendExpire
- type Handler = Handler' 'NoAutoExtendExpire
- type HandlerAutoExtendExpire = Handler' 'AutoExtendExpire
- data Settings = Settings {
- connectionInfo :: ConnectInfo
- clusterMode :: ClusterMode
- defaultExpiry :: DefaultExpiry
- queryTimeout :: QueryTimeout
- maxKeySize :: MaxKeySize
- decoder :: Decoder Settings
- jsonApi :: forall a key. (ToJSON a, FromJSON a, Ord a) => (key -> Text) -> Api key a
- textApi :: (key -> Text) -> Api key Text
- byteStringApi :: (key -> Text) -> Api key ByteString
- data Api key a
- del :: Api key a -> NonEmpty key -> Query Int
- exists :: Api key a -> key -> Query Bool
- expire :: Api key a -> key -> Int -> Query ()
- ping :: Api key a -> Query ()
- zadd :: Api key a -> key -> NonEmptyDict a Float -> Query Int
- zrange :: Api key a -> key -> Int -> Int -> Query (List a)
- zrangeByScoreWithScores :: Api key a -> key -> Float -> Float -> Query [(a, Float)]
- zrank :: Api key a -> key -> a -> Query (Maybe Int)
- zrevrank :: Api key a -> key -> a -> Query (Maybe Int)
- query :: HasCallStack => Handler' x -> Query a -> Task Error a
- transaction :: HasCallStack => Handler' x -> 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.
handlerAutoExtendExpire :: Text -> Settings -> Acquire HandlerAutoExtendExpire Source #
Produce a namespaced handler for Redis access. This will ensure that we extend all keys accessed by a query by a configured default time (see Settings.defaultExpiry)
type Handler = Handler' 'NoAutoExtendExpire Source #
This is a type alias of a handler parametrized by a value that indicates that the auto extend feature is disabled. Note: The tick in front of NoAutoExtendExpire is not necessary, but good practice to indicate that we are lifting a value to the type level.
type HandlerAutoExtendExpire = Handler' 'AutoExtendExpire Source #
This is a type alias of a handler parametrized by a value that indicates that the auto extend feature is enabled. Note: The tick in front of AutoExtendExpire is not necessary, but good practice to indicate that we are lifting a value to the type level.
Settings required to initiate a redis connection.
Constructors
Settings | |
Fields
|
Creating a Redis API
jsonApi :: forall a key. (ToJSON a, FromJSON a, Ord a) => (key -> Text) -> Api key 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,
byteStringApi :: (key -> Text) -> Api key ByteString Source #
Creates a Redis API mapping a key
to a ByteString
Creating Redis queries
del :: Api key a -> NonEmpty key -> Query Int Source #
Removes the specified keys. A key is ignored if it does not exist.
expire :: Api key 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 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.
zadd :: Api key a -> key -> NonEmptyDict a Float -> Query Int Source #
Adds all the specified members with the specified scores to the sorted set. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
zrange :: Api key a -> key -> Int -> Int -> Query (List a) Source #
Returns the specified range of elements in the sorted set. The order of elements is from the lowest to the highest score. Elements with the same score are ordered lexicographically. The start and stop arguments represent zero-based indexes, where 0 is the first element, 1 is the next element, and so on. These arguments specify an inclusive range, so for example, ZRANGE myzset 0 1 will return both the first and the second element of the sorted set.
The indexes can also be negative numbers indicating offsets from the end of the sorted set, with -1 being the last element of the sorted set, -2 the penultimate element, and so on.
Out of range indexes do not produce an error.
zrangeByScoreWithScores :: Api key a -> key -> Float -> Float -> Query [(a, Float)] Source #
Like zrange
, but with the bounds being scores rather than offsets,
and with the result including the scores for each returned result.
zrank :: Api key a -> key -> a -> Query (Maybe Int) Source #
Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.
zrevrank :: Api key a -> key -> a -> Query (Maybe Int) Source #
Returns the rank of member in the sorted set stored at key, with the scores ordered from high to low. The rank (or index) is 0-based, which means that the member with the highest score has rank 0.
Running Redis queries
query :: HasCallStack => Handler' x -> 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' x -> 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.
Constructors
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