ABANDONED: Use https://github.com/kataras/iris instead. Extremely-easy cache service for HTTP/x . Supports both net/http and valyala/fasthttp
MIT License
httpcache is an easy, http cache service written in Go.
Compatible with net/http and valyala/fasthttp.
A web cache (or HTTP cache) is an information technology for the temporary storage (caching) of web documents, such as HTML pages and images, to reduce bandwidth usage, server load, and perceived lag. A web cache system stores copies of documents passing through it; subsequent requests may be satisfied from the cache if certain conditions are met.
Simple, you want faster web applications, use of the httpcache gives you more than 20 times performance advantage than other websites.
So, should I use cache for everyhing?
Ideas for cache: blogs, index pages, contact pages, about me websites...
httpcache
gives you the ability to cache certain handlers or the whole website,
it's up to you where you want to apply cache!
The only requirement is the Go Programming Language.
$ go get -u github.com/geekypanda/httpcache/...
Cache
& CacheFasthttp
functions, convert any type of Handler to cached Handler
.For distributed applications only:
ListenAndServe
function, starts the remote cache service on a specific network address.CacheRemote
& CacheRemoteFasthttp
functions, convert any type of Handlercached Handler
In short terms, any data with any content type is cached.
Some of them are...
application/json
text/html
text/plain
text/xml
text/javascript (JSONP)
application/octet-stream
application/pdf
image/jpeg
image/png
image/gif
image/bmp
image/svg+xml
image/x-icon
package main
import (
"net/http"
"time"
"github.com/geekypanda/httpcache"
)
func main() {
// The only thing that separates your handler to be cached is just
// ONE function wrapper
// httpcache.CacheFunc will cache your http.HandlerFunc
// httpcache.Cache will cache your http.Handler
//
// first argument is the handler witch serves the contents to the client
// second argument is how long this cache will be valid
// and must be refreshed after the x time passed and a new request comes
http.HandleFunc("/", httpcache.CacheFunc(mypageHandler, 20*time.Second))
// start the server, navigate to http://localhost:8080
http.ListenAndServe(":8080", nil)
}
func mypageHandler(w http.ResponseWriter, r *http.Request) {
// tap multiple times the browser's refresh button and you will
// see this println only once each of 20seconds
println("Handler executed. Cache refreshed.")
// set our content type and send the response to the client,
// it can be any type of response
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte("<h1> Hello!!! </h1>"))
}
Tip: To cache the whole website for a certain time, pass the
Cache
at themux
.http.ListenAndServe(":8080", http.Cache(http.DefaultServeMux, 5 *time.Minute))
Navigate to the _examples folder to see a small overview of cached templates.
I didn't see any usage example of fasthttp here, why?
The authors of httpcache project are:
Unless otherwise noted, the httpcache source files are distributed under the terms of the MIT License.
License can be found here.