Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
loadredis-faster.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
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