packages feed

hedis-pile 0.2.2 → 0.3.0

raw patch · 3 files changed

+38/−16 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Database.Redis.Pile: pile :: ByteString -> ByteString -> Maybe (ByteString, ByteString) -> IO ([(ByteString, ByteString)], Maybe Integer, [ByteString]) -> Redis (Maybe [(ByteString, ByteString)])
+ Database.Redis.Pile: pile :: ByteString -> ByteString -> Maybe (ByteString, ByteString) -> Maybe ByteString -> IO ([(ByteString, ByteString)], Maybe Integer, [ByteString]) -> Redis (Maybe [(ByteString, ByteString)])

Files

hedis-pile.cabal view
@@ -1,5 +1,5 @@ name:           hedis-pile
-version:        0.2.2
+version:        0.3.0
 cabal-version:  >= 1.8
 build-type:     Simple
 stability:      Experimental
src/Database/Redis/Pile.hs view
@@ -38,22 +38,28 @@ -- > conn <- connect defaultConnectInfo
 -- > runRedis conn $ do
 -- >    -- do it
--- >    r <- pile "myprefix" "mykey" (Just ("etag", "etag")) $  
+-- >    r <- pile "myprefix" "mykey" (Just ("etag", "etag")) Nothing $  
 -- >        return ([("etag", "etag"), ("val", "myval")], Nothing, [])
 -- >    liftIO $ print r
 -- >    -- Just [("etag", "etag"), ("val", "myval")]
 -- >    
 -- >    -- once again
--- >    r <- pile "myprefix" "mykey" (Just ("etag", "etag")) $  
+-- >    r <- pile "myprefix" "mykey" (Just ("etag", "etag")) Nothing $  
 -- >        return ([("etag", "etag"), ("val", "myval")], Nothing, [])
 -- >    liftIO $ print r
 -- >    -- Nothing
 -- >    
 -- >    -- and again without expect
--- >    r <- pile "myprefix" "mykey" Nothing $  
+-- >    r <- pile "myprefix" "mykey" Nothing Nothing $  
 -- >        return ([("etag", "etag"), ("val", "myval")], Nothing, [])
 -- >    liftIO $ print r
 -- >    -- Just [("etag", "etag"), ("val", "myval")]
+--
+--   Time complexity for cached data:
+--   
+--   * @O(1)@ If expect matches.
+--
+--   * @O(2)@ If payload field provided
 pile :: 
        B.ByteString
             -- ^ Prefix for key and tags.
@@ -61,25 +67,41 @@             -- ^ Key in cache. Key will be stored as @prefix:key@
     -> Maybe (B.ByteString, B.ByteString)
             -- ^ Optional expect field.
+    -> Maybe B.ByteString
+            -- ^ Optional payload field. This reduces time complexity of 
+            --   request data from the cache from @O(N)@ to @O(1)@.
+            --   Regardless of setting this field, all data from computation
+            --   will stored in cache.
     -> IO ([(B.ByteString, B.ByteString)], 
            Maybe Integer, 
            [B.ByteString])
             -- ^ Computation that returns data and 
             --   optional TTL and tags. All tags will be stored as @prefix:tag@.
     -> R.Redis (Maybe [(B.ByteString, B.ByteString)])
-pile p key (Just (ef, ev)) f = do
+pile p key (Just (ef, ev)) payload f = do
     e <- R.hget (p `B.append` ":" `B.append` key) ef
     case e of
         Right (Just ev') | ev' == ev -> return Nothing
-                         | otherwise -> pile p key Nothing f
-        _ -> pile p key Nothing f
-pile p key Nothing f = do
-    d <- R.hgetall withPrefix
+                         | otherwise -> pile p key Nothing payload f
+        _ -> pile p key Nothing payload f
+pile p key Nothing payload f = do
+    d <- fetchPayload payload
     case d of
-        Right [] -> runF
-        Right r -> return $ Just r
-        _ -> runF
+        Nothing -> runF
+        Just r -> return $ Just r
   where
+    fetchPayload Nothing = do
+        v <- R.hgetall withPrefix
+        return $ case v of 
+            Right [] -> Nothing
+            Right r -> Just r
+            _ -> Nothing
+    fetchPayload (Just plf) = do
+        v <- R.hget withPrefix plf
+        return $ case v of 
+            Right (Just v') -> Just [(plf, v')]
+            _ -> Nothing
+    
     withPrefix = p `B.append` ":" `B.append` key
     runF = do
         (r, ke, t) <- liftIO f
test/Database/Redis/Test/Pile.hs view
@@ -27,10 +27,10 @@     setup
     teardown
     $ runInRedis $ do
-        r <- RP.pile allPrefix "two" Nothing $ 
+        r <- RP.pile allPrefix "two" Nothing Nothing $ 
             return (allData, Nothing, [])
         liftIO $ Just allData @=? r
-        void $ RP.pile allPrefix "three" Nothing $ 
+        void $ RP.pile allPrefix "three" Nothing Nothing $ 
             return (allData, Just 15, ["one"])
         ex <- R.ttl $ allPrefix `B.append` ":three"
         liftIO $ Right 15 @=? ex
@@ -40,10 +40,10 @@     setup
     teardown
     $ runInRedis $ do
-        r <- RP.pile allPrefix "one" Nothing $ 
+        r <- RP.pile allPrefix "one" Nothing Nothing $ 
             return (allData, Nothing, [])
         liftIO $ Just allData @=? r
-        r' <- RP.pile allPrefix "one" (Just ("etag", "etag")) $ 
+        r' <- RP.pile allPrefix "one" (Just ("etag", "etag")) Nothing $ 
             return (allData, Nothing, [])
         liftIO $ Nothing @=? r'