wai-middleware-cache (empty) → 0.1.0
raw patch · 4 files changed
+108/−0 lines, 4 filesdep +basedep +bytestringdep +conduitsetup-changed
Dependencies added: base, bytestring, conduit, http-types, wai
Files
- LICENSE +25/−0
- Setup.hs +2/−0
- src/Network/Wai/Middleware/Cache.hs +49/−0
- wai-middleware-cache.cabal +32/−0
+ LICENSE view
@@ -0,0 +1,25 @@+The following license covers this documentation, and the source code, except+where otherwise indicated.++Copyright 2010, Michael Snoyman. All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+ list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple +main = defaultMain
+ src/Network/Wai/Middleware/Cache.hs view
@@ -0,0 +1,49 @@+ +-- | Transparent front cache middleware for 'Network.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 + +module Network.Wai.Middleware.Cache ( + -- * Backend + CacheBackend, + -- * Middleware + cache +) where + +import Data.Maybe (fromMaybe) +import Data.ByteString.Lazy (empty) +import Data.Conduit (ResourceT) + +import Network.Wai (Application, Middleware, Request(..), Response(..), + responseLBS) +import Network.HTTP.Types (status304) + +-- | Abstract cache backend. Result may be 'Nothing' you need to respond +-- wirh status @304 - Not Modified@. +type CacheBackend = + Application -- ^ Application + -> Request -- ^ Request + -> ResourceT IO (Maybe Response) + +-- | Cache middleware. Use it with conjuction with 'CacheBackend'. +-- +-- > -- 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 +cache :: + CacheBackend -- ^ Cache backend. + -> Middleware +cache cacheBackend app req = do + res <- cacheBackend app req + return $ fromMaybe (responseLBS status304 [] empty) res +
+ wai-middleware-cache.cabal view
@@ -0,0 +1,32 @@+name: wai-middleware-cache +version: 0.1.0 +cabal-version: >= 1.8 +build-type: Simple +stability: Experimental +author: Alexander Dorofeev <aka.spin@gmail.com> +maintainer: Alexander Dorofeev <aka.spin@gmail.com> +synopsis: Caching middleware for WAI. +homepage: https://github.com/akaspin/wai-middleware-cache +bug-reports: https://github.com/akaspin/wai-middleware-cache/issues +description: + This package provides cache middleware and abstract type for + creating cache backends. +license: BSD3 +license-file: LICENSE +category: Web + +source-repository head + type: git + location: git://github.com/akaspin/wai-middleware-cache.git + +library + hs-source-dirs: src + build-depends: + base >= 4 && < 5, + wai >= 1.1, + bytestring >= 0.9 && < 0.10, + conduit >= 0.2 && < 0.3, + http-types >= 0.6 && < 0.7 + ghc-options: -Wall + exposed-modules: Network.Wai.Middleware.Cache +