wai-middleware-cache-0.3.5: Caching middleware for WAI.

Safe HaskellSafe-Infered

Network.Wai.Middleware.Cache

Contents

Description

Transparent front cache middleware for Wai.

Instead caching internal data, this middleware caches entire responses. Of course, this creates additional costs. However, the simplification of the internal structure and concentration of caching in the immediate vicinity of the request is more than redeem them.

 cache (debugBackend True)
       ourFrivolousApplication

Synopsis

Middleware

type CacheBackendSource

Arguments

 = Application

Application

-> Request

Request

-> ResourceT IO (Maybe Response) 

Abstract cache backend. Result may be Nothing you need to respond with status 304 - Not Modified.

cacheSource

Arguments

:: CacheBackend

Cache backend.

-> Middleware 

Cache middleware. Use it with conjuction with CacheBackend and headerETag.

 -- Simplest backend. Suggests @304 - Not Modified@ with site root.
 rootBackend app req = do 
     case rawPathInfo req of
         "/" -> return Nothing
         _ -> do
             res <- app req 
             return $ Just res
 app = responseLBS ok200 [] "someresponse"
 
 cachedApp = cache rootBackend $ headerETag $ app

headerETag :: MiddlewareSource

Add "ETag" header to response if it not present. Value of header is MD5 hash of response body.

Request helpers

lookupETag :: Request -> Maybe ByteStringSource

Helper for extract If-None-Match header from Request. Use this with backends.