-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilelistCache.hs
More file actions
48 lines (42 loc) · 1.56 KB
/
FilelistCache.hs
File metadata and controls
48 lines (42 loc) · 1.56 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
--- |
--- | This module contains a mgmt-functions to maintain a cache of filelists
--- |
--- Copyright : (c) Florian Richter 2011
--- License : GPL
---
module FilelistCache where
import Control.Concurrent.STM
import Control.Monad
import qualified Data.Map as M
import qualified Data.ByteString.Lazy as L
import Control.DeepSeq
import FilelistCacheTypes
import Config
import FilelistTypes
import Filelist
import DCToClient
-- | download filelist of some user.
-- | the filelist will be cached, so the next request should be fast
getFilelistCached :: AppState -> Nick -> IO [TreeNode]
getFilelistCached appState nick = do
result <- atomically $ do
cache <- readTVar (appFilelistCache appState)
case M.lookup nick cache of
Just FlCEInProgress -> retry
Just (FlCETreeNode tree) -> return $ Just tree
Nothing -> do
-- not in cache, tell everybody, we're going to download it
let newcache = M.insert nick FlCEInProgress cache
writeTVar (appFilelistCache appState) newcache
return Nothing
case result of
Just tree -> return tree
Nothing -> do
filelist <- downloadFilelist appState nick
let tree = xmlBzToTreeNode $ L.fromChunks [filelist]
atomically $ do
cache <- readTVar (appFilelistCache appState)
let newcache = tree `deepseq` M.insert nick (FlCETreeNode tree) cache
newcache `deepseq` writeTVar (appFilelistCache appState) newcache
return tree
-- vim: sw=4 expandtab