You can change<p><pre><code> appV fn x = atomically $ readTVar x >>= writeTVar x . fn
</code></pre>
to<p><pre><code> appV fn x = atomically $ modifyTVar x fn
</code></pre>
The function getValue need not be in IO monad. You can simplify it using fromMaybe in Data.Maybe.<p>Sometimes you check "head l" and "tail l"; it's more idiomatic to use pattern matching. For example, you can change:<p><pre><code> setCommand handle cmd db = do
appV (conv k v) db
hPutStrLn handle $ "OK"
where k = (head cmd)
v = (unwords (tail cmd))
....
case (head cmd) of
("get") -> getCommand handle (unwords (tail cmd)) db
("set") -> setCommand handle (tail cmd) db
</code></pre>
to something like this:<p><pre><code> setCommand handle key val db = do
appV (conv key val) db
hPutStrLn handle "OK"
...
case cmd of
"get":key -> getCommand handle (unwords key) db
"set":key:val -> setCommand handle key (unwords val) db
</code></pre>
Also consider using hlint, it often gives good advice.
Nice example of using Software Transactional Memory. Which is starting to be available in things like gcc, but is much much nicer in Haskell for reasons involving purity and monads.<p>Particularly this function, which changes a variable in the database, transactionally.<p><pre><code> appV :: (DB -> DB) -> TVar DB -> IO ()
appV fn x = atomically $ readTVar x >>= writeTVar x . fn
</code></pre>
It would be easy to use this to add commands that eg, increment counters in the database, and the STM would ensure that it works correctly with multiple concurrent writers. No locking needed. A quick example, which could be improved by changing the DB type to support Int as well as String values:<p><pre><code> incrCommand :: Handle -> [String] -> (TVar DB) -> IO ()
incrCommand handle k db = do
appV incvar db
hPutStrLn handle $ "OK"
where
incvar k v = insert k $ show $ (fromMaybe 0 $ readMaybe v) + 1
</code></pre>
I don't know how well Haskell's STM performs compared with eg, database transactions or locking. It's been more than fast enough for my own needs. Anybody know?
<p><pre><code> conv k v db = insert k v db
</code></pre>
You could rewrite that as:<p><pre><code> conv = insert
</code></pre>
Probably some remnant of you figuring out how you should write appV, which is a proper helpful function, but probably should be called updateTVar or something :)
If you like this, check out <a href="http://www.happstack.com/docs/crashcourse/AcidState.html" rel="nofollow">http://www.happstack.com/docs/crashcourse/AcidState.html</a> . It allows for arbitrary Haskell data structures to safely and automatically be marshaled in and out of the data store.
How do I store something with a \n in it ;)<p>Edit: While I posted this as a joke, I do love the way that redis avoids any kind of injection attack via prepending the message length. Of course, I understand that this project is not at the stage where it is meant to be deployed in production/security become an issue; it's just interesting to see how the different communication methods have different consequences.