Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions lib/mobility-core/src/Kernel/Storage/Hedis/Cache.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{-# LANGUAGE AllowAmbiguousTypes #-}

{-
Copyright 2022-23, Juspay India Pvt Ltd

Expand Down Expand Up @@ -27,16 +25,16 @@ import qualified Crypto.Hash as Hash
import qualified Data.ByteArray as BA
import Data.String.Conversions (cs)
import qualified Data.Text as T
import Data.Typeable (typeRep)
import Kernel.Prelude
import Kernel.Storage.Hedis.Config (HedisFlow)
import Kernel.Storage.Hedis.Queries (ExpirationTime, del, hDel, hSetExp, safeHGet)
import Text.Hex (encodeHex)

-- | The hash key (Redis bucket) for type @b@: @prefix:TypeName@, e.g.
-- @buildRedisHashKey \@RiderConfig "ConfigPilot" == "ConfigPilot:RiderConfig"@.
buildRedisHashKey :: forall b. Typeable b => Text -> Text
buildRedisHashKey prefix = prefix <> ":" <> show (typeRep (Proxy :: Proxy b))
-- | The Redis bucket is just the prefix; the cached type is no longer part of
-- the key, so deletes by bucket are type-free, e.g.
-- @buildRedisHashKey "ConfigPilot" == "ConfigPilot"@.
buildRedisHashKey :: Text -> Text
buildRedisHashKey prefix = prefix

buildRedisCacheField :: [Text] -> Maybe Text
buildRedisCacheField fieldFrags =
Expand All @@ -49,7 +47,7 @@ buildRedisCacheField fieldFrags =

withRedisCache ::
forall b m env.
(ToJSON b, FromJSON b, Typeable b, HedisFlow m env) =>
(ToJSON b, FromJSON b, HedisFlow m env) =>
Text ->
[Text] ->
ExpirationTime ->
Expand All @@ -60,7 +58,7 @@ withRedisCache hashPrefix fieldFrags ttlInSeconds fn =
then case buildRedisCacheField fieldFrags of
Nothing -> fn
Just field -> do
let hashKey = buildRedisHashKey @b hashPrefix
let hashKey = buildRedisHashKey hashPrefix
mbRes <- safeHGet hashKey field
case mbRes of
Just res -> pure res
Expand All @@ -70,23 +68,20 @@ withRedisCache hashPrefix fieldFrags ttlInSeconds fn =
pure res
else fn

-- | Delete a single cached entry created by 'withRedisCache'. The cached type
-- must be supplied (via @TypeApplications@) so the same hash/field is targeted,
-- e.g. @delRedisCache \@RiderConfig "ConfigPilot" ["blr", "auto", "x"]@.
-- | Delete a single cached entry created by 'withRedisCache', e.g.
-- @delRedisCache "ConfigPilot" ["blr", "auto", "x"]@.
delRedisCache ::
forall b m env.
(Typeable b, HedisFlow m env) =>
HedisFlow m env =>
Text ->
[Text] ->
m ()
delRedisCache hashPrefix fieldFrags =
case buildRedisCacheField fieldFrags of
Nothing -> pure ()
Just field -> hDel (buildRedisHashKey @b hashPrefix) [field]
Just field -> hDel (buildRedisHashKey hashPrefix) [field]

delRedisCacheBucket ::
forall b m env.
(Typeable b, HedisFlow m env) =>
HedisFlow m env =>
Text ->
m ()
delRedisCacheBucket hashPrefix = del (buildRedisHashKey @b hashPrefix)
delRedisCacheBucket hashPrefix = del (buildRedisHashKey hashPrefix)
Loading