-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCache.hs
35 lines (27 loc) · 1.05 KB
/
Cache.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Cache where
import Control.Monad (void)
import Data.ByteString.Char8 (pack, unpack)
import Data.Int (Int64)
import Database.Redis
import BasicSchema
type RedisInfo = ConnectInfo
localRedisInfo :: RedisInfo
localRedisInfo = defaultConnectInfo
runRedisAction :: RedisInfo -> Redis a -> IO a
runRedisAction redisInfo action = do
connection <- connect redisInfo
runRedis connection action
cacheUser :: RedisInfo -> Int64 -> User -> IO ()
cacheUser redisInfo uid user = runRedisAction redisInfo $ void $ setex (pack . show $ uid) 3600 (pack . show $ user)
fetchUserRedis :: RedisInfo -> Int64 -> IO (Maybe User)
fetchUserRedis redisInfo uid = runRedisAction redisInfo $ do
result <- get (pack . show $ uid)
case result of
Right (Just userString) -> return $ Just (read . unpack $ userString)
_ -> return Nothing
deleteUserCache :: RedisInfo -> Int64 -> IO ()
deleteUserCache redisInfo uid = do
connection <- connect redisInfo
runRedis connection $ do
_ <- del [pack . show $ uid]
return ()