-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Cache.hs
36 lines (30 loc) · 1.06 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
36
module Cache where
import Prelude
import Control.Monad
import qualified Data.ByteString.Char8 as BC
import Network.Socket (HostName, PortNumber)
import qualified Database.Memcache.Client as Memcache
import Model.Download (InfoHash(unInfoHash))
type Cache = Memcache.Client
newCache :: HostName -> PortNumber -> IO Cache
newCache host port = do
let specs = [Memcache.ServerSpec host port Memcache.NoAuth]
opts = Memcache.def
Memcache.newClient specs opts
getTorrentExists :: InfoHash -> Cache -> IO Bool
getTorrentExists infoHash client = do
let key = BC.concat ["E", unInfoHash infoHash]
expire = 3600
mVFV <- Memcache.gat client key expire
case mVFV of
Nothing -> return True
Just (_value, _flags, _version) -> return False
-- | Caching negative entries to avoid DB traffic on rogue torretns
setTorrentExists :: InfoHash -> Bool -> Cache -> IO ()
setTorrentExists infoHash exists client = do
let key = BC.concat ["E", unInfoHash infoHash]
value = "0"
expire = 3600
unless exists $
void $
Memcache.set client key value 0 expire