Created
May 30, 2012 05:48
-
-
Save bradclawsie/2833979 to your computer and use it in GitHub Desktop.
loadredis.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import qualified Data.DateTime as DATE | |
import qualified Control.Monad as C | |
import qualified Control.Monad.Trans as CT | |
import qualified Database.Redis as R | |
import qualified System.Random as SR | |
import qualified Data.ByteString.UTF8 as U | |
-- a nice find from the web - picking an elt at random | |
pick :: [a] -> IO a | |
pick xs = SR.randomRIO (0, (length xs - 1)) >>= return . (xs !!) | |
main = do | |
now <- C.liftM DATE.toSeconds $ DATE.getCurrentTime | |
words <- (C.liftM lines . readFile) "/etc/dictionaries-common/words" | |
-- make a list of tuples of (utc_second,random-word) | |
-- pairs a random word to each second of previous 24 hour period | |
rw <- mapM (\i -> pick words >>= \rw -> return (i,rw)) [(now - 86400)..now] | |
conn <- R.connect R.defaultConnectInfo | |
R.runRedis conn $ do | |
-- empty redis | |
fr <- R.flushall | |
CT.liftIO $ putStrLn $ show fr | |
-- set time -> word | |
mapM_ (\j -> R.set (U.fromString $ show $ fst j) (U.fromString $ snd j)) rw | |
return () | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment