From ee71cc5316688088c12ee9b5a8cde12a8d133a60 Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Wed, 16 Oct 2024 21:21:57 -0400 Subject: [PATCH] add redis sismember --- nri-redis/nri-redis.cabal | 2 +- nri-redis/src/Redis/Handler.hs | 4 ++++ nri-redis/src/Redis/Internal.hs | 5 +++++ nri-redis/src/Redis/Set.hs | 12 ++++++++++-- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/nri-redis/nri-redis.cabal b/nri-redis/nri-redis.cabal index b7743624..189b9af5 100644 --- a/nri-redis/nri-redis.cabal +++ b/nri-redis/nri-redis.cabal @@ -5,7 +5,7 @@ cabal-version: 1.18 -- see: https://github.com/sol/hpack name: nri-redis -version: 0.2.0.1 +version: 0.2.0.2 synopsis: An intuitive hedis wrapper library. description: Please see the README at . category: Web diff --git a/nri-redis/src/Redis/Handler.hs b/nri-redis/src/Redis/Handler.hs index dc2aac2b..5a68eab5 100644 --- a/nri-redis/src/Redis/Handler.hs +++ b/nri-redis/src/Redis/Handler.hs @@ -305,6 +305,10 @@ doRawQuery query = Database.Redis.srem (toB key) (NonEmpty.toList vals) |> PreparedQuery |> map (Ok << Prelude.fromIntegral) + Internal.Sismember key val -> + Database.Redis.sismember (toB key) val + |> PreparedQuery + |> map Ok Internal.Smembers key -> Database.Redis.smembers (toB key) |> PreparedQuery diff --git a/nri-redis/src/Redis/Internal.hs b/nri-redis/src/Redis/Internal.hs index 490d8866..c63c05c7 100644 --- a/nri-redis/src/Redis/Internal.hs +++ b/nri-redis/src/Redis/Internal.hs @@ -111,6 +111,7 @@ cmds query'' = Sadd key vals -> [unwords ("SADD" : key : List.map (\_ -> "*****") (NonEmpty.toList vals))] Scard key -> [unwords ["SCARD", key]] Srem key vals -> [unwords ("SREM" : key : List.map (\_ -> "*****") (NonEmpty.toList vals))] + Sismember key _ -> [unwords ["SISMEMBER", key , "*****"]] Smembers key -> [unwords ["SMEMBERS", key]] Zadd key vals -> [unwords ("ZADD" : key : List.concatMap (\(_, val) -> ["*****", Text.fromFloat val]) (Dict.toList vals))] Zrange key start stop -> [unwords ["ZRANGE", key, Text.fromInt start, Text.fromInt stop]] @@ -167,6 +168,7 @@ data Query a where Sadd :: Text -> NonEmpty ByteString -> Query Int Scard :: Text -> Query Int Srem :: Text -> NonEmpty ByteString -> Query Int + Sismember :: Text -> ByteString -> Query Bool Smembers :: Text -> Query (List ByteString) Zadd :: Text -> Dict.Dict ByteString Float -> Query Int Zrange :: Text -> Int -> Int -> Query [ByteString] @@ -312,6 +314,7 @@ mapKeys fn query' = Sadd key vals -> Task.map (\newKey -> Sadd newKey vals) (fn key) Scard key -> Task.map Scard (fn key) Srem key vals -> Task.map (\newKey -> Srem newKey vals) (fn key) + Sismember key val -> Task.map (\newKey -> Sismember newKey val) (fn key) Smembers key -> Task.map Smembers (fn key) Zadd key vals -> Task.map (\newKey -> Zadd newKey vals) (fn key) Zrange key start stop -> Task.map (\newKey -> Zrange newKey start stop) (fn key) @@ -354,6 +357,7 @@ mapReturnedKeys fn query' = Sadd key vals -> Sadd key vals Scard key -> Scard key Srem key vals -> Srem key vals + Sismember key val -> Sismember key val Smembers key -> Smembers key Zadd key vals -> Zadd key vals Zrange key start stop -> Zrange key start stop @@ -411,6 +415,7 @@ keysTouchedByQuery query' = Sadd key _ -> Set.singleton key Scard key -> Set.singleton key Srem key _ -> Set.singleton key + Sismember key _ -> Set.singleton key Smembers key -> Set.singleton key Zadd key _ -> Set.singleton key Zrange key _ _ -> Set.singleton key diff --git a/nri-redis/src/Redis/Set.hs b/nri-redis/src/Redis/Set.hs index 7a50fe3a..d122ec29 100644 --- a/nri-redis/src/Redis/Set.hs +++ b/nri-redis/src/Redis/Set.hs @@ -29,6 +29,7 @@ module Redis.Set sadd, scard, srem, + sismember, smembers, -- * Running Redis queries @@ -103,7 +104,11 @@ data Api key a = Api -- | Returns all the members of the set value stored at key. -- -- https://redis.io/commands/smembers - smembers :: key -> Internal.Query (Set.Set a) + smembers :: key -> Internal.Query (Set.Set a), + -- | Returns if member is a member of the set stored at key. + -- + -- https://redis.io/docs/latest/commands/sismember/ + sismember :: key -> a -> Internal.Query Bool } -- | Creates a json API mapping a 'key' to a json-encodable-decodable type @@ -148,5 +153,8 @@ makeApi Codec.Codec {Codec.codecEncoder, Codec.codecDecoder} toKey = smembers = \key -> Internal.Smembers (toKey key) |> Internal.WithResult (Prelude.traverse codecDecoder) - |> Internal.map Set.fromList + |> Internal.map Set.fromList, + sismember = \key val-> + Internal.Sismember (toKey key) (codecEncoder val) + }