Skip to content

Commit

Permalink
Merge pull request #116 from NoRedInk/redis-add-sismember
Browse files Browse the repository at this point in the history
add redis sismember
  • Loading branch information
michaelglass authored Oct 21, 2024
2 parents 769039f + ee71cc5 commit 5facaac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nri-redis/nri-redis.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/NoRedInk/haskell-libraries/tree/trunk/nri-redis#readme>.
category: Web
Expand Down
4 changes: 4 additions & 0 deletions nri-redis/src/Redis/Handler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions nri-redis/src/Redis/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions nri-redis/src/Redis/Set.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Redis.Set
sadd,
scard,
srem,
sismember,
smembers,

-- * Running Redis queries
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

}

0 comments on commit 5facaac

Please sign in to comment.