Skip to content

Instantly share code, notes, and snippets.

@bradclawsie
Created May 30, 2012 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradclawsie/2833979 to your computer and use it in GitHub Desktop.
Save bradclawsie/2833979 to your computer and use it in GitHub Desktop.
loadredis.hs
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