Commits

Daniel Lyons committed 7dd68eb

improved caching

  • Participants
  • Parent commits c6a6390

Comments (0)

Files changed (2)

File src/Schedool/Cache.hs

 -- | The idea here is if we have a cache with this key, we look it up and
 --   return it. Otherwise, we execute the supplied IO action and cache the
 --   result before returning it.
-tryCache :: String -> IO String -> IO String
+tryCache :: (Read a, Show a) => String -> IO a -> IO a
 tryCache key recalc = do
   -- first, make sure we have the cache directory
   createDirectoryIfMissing True cacheDirectory
   exists <- doesFileExist filePath
   -- if it exists, return that content
   if exists
-     then readFile filePath
+     then readFile filePath >>= return . read
      else do
            -- since it doesn't, we execute the IO action and cache that
            content <- recalc
-           writeFile filePath content
+           writeFile filePath (show content)
            return content

File src/Schedool/Data.hs

                      ,getSections) 
     where 
 
+import Schedool.Cache
 import Schedool.Mirror
 import Schedool.Parse
 import Schedool.Section
 
 import Control.Monad
 
--- local caching is no longer implemented.
+-- | The current list of departments. Uses two levels of caching.
+getDepartments :: IO [Department]
+getDepartments = tryCache "departments.hs" readDepartments
 
 -- | The current list of Departments.
-getDepartments :: IO [Department]
-getDepartments = openDepartmentData >>= return . parseDepartments
+readDepartments :: IO [Department]
+readDepartments = openDepartmentData >>= return . parseDepartments
+
+-- | The curretn list of sections. Uses two levels of caching.
+getSections :: IO [Section]
+getSections = tryCache "sections.hs" readSections
 
 -- | The current list of Sections.
-getSections :: IO [Section]
-getSections = do
+readSections :: IO [Section]
+readSections = do
   -- get all the departments
-  depts <- getDepartments
+  depts <- readDepartments
   -- get each department's sections and concatenate all of them into one big list
-  liftM concat $ forM depts getSection
+  liftM concat $ forM depts readSection
 
-getSection :: Department -> IO [Section]
-getSection dept = do
-  -- get the section HTML
+readSection :: Department -> IO [Section]
+readSection dept = do
+  -- read the section HTML
   sectionHTML <- openSectionData dept
   -- pass it off to the parser
   return $ parseSections sectionHTML