Created
June 14, 2012 05:01
-
-
Save bradclawsie/2928070 to your computer and use it in GitHub Desktop.
loadredis-faster.hs
This file contains 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 | |
import qualified Data.Vector as V | |
main = do | |
now <- C.liftM DATE.toSeconds $ DATE.getCurrentTime | |
-- make a vector out of the list of dict words | |
words <- (C.liftM V.fromList . C.liftM lines . readFile) "/etc/dictionaries-common/words" | |
let lw = V.length words | |
-- match a random word to each second in the last 24 hour range | |
rw <- mapM (\i -> SR.randomRIO (0,(lw-1)) >>= \ri -> return (i,words V.! ri)) [(now - 86400)..now] | |
conn <- R.connect R.defaultConnectInfo | |
R.runRedis conn $ | |
-- empty redis and then set time->word | |
R.flushall >> mapM_ (\j -> R.set (U.fromString $ show $ fst j) (U.fromString $ snd j)) rw | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment