https://github.com/alexmerren/httpcache
A HTTP RoundTripper with a local SQLite cache
https://github.com/alexmerren/httpcache
caching go http roundtripper
Last synced: 2 months ago
JSON representation
A HTTP RoundTripper with a local SQLite cache
- Host: GitHub
- URL: https://github.com/alexmerren/httpcache
- Owner: alexmerren
- License: mit
- Created: 2023-08-28T13:13:53.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-10-22T18:57:14.000Z (6 months ago)
- Last Synced: 2026-01-15T05:15:03.048Z (3 months ago)
- Topics: caching, go, http, roundtripper
- Language: Go
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# httpcache
[](https://goreportcard.com/report/github.com/alexmerren/httpcache)

[](https://pkg.go.dev/github.com/alexmerren/httpcache)
httpcache is a local cache for HTTP requests and responses, wrapping `http.RoundTripper` from Go standard library.
## Features
httpcache has a few useful features:
- Store and retrieve HTTP responses for any type of request;
- Expire responses after a customisable time duration;
- Decide when to store responses based on status code and request method.
If you want to request a feature then please open a [GitHub Issue](https://www.github.com/alexmerren/httpcache/issues) today!
## Quick Start
This module can be installed using the command line:
```bash
go get -u github.com/alexmerren/httpcache
```
Here's an example of using the `httpcache` module to cache responses:
```go
func main() {
// Create a new SQLite database to store HTTP responses.
cache, _ := httpcache.NewSqliteCache("database.sqlite")
// Create a config with a behaviour of:
// - Storing responses with status code 200;
// - Storing responses from HTTP requests using method "GET";
// - Expiring responses after 7 days...
config := httpcache.NewConfigBuilder().
WithAllowedStatusCodes([]int{http.StatusOK}).
WithAllowedMethods([]string{http.MethodGet}).
WithExpiryTime(time.Duration(60*24*7) * time.Minute).
Build()
// ... or use the default config.
config = httpcache.DefaultConfig
// Create a transport with the SQLite cache and config.
cachedTransport, _ := httpcache.NewTransport(config, cache)
// Create a HTTP client with the cached roundtripper.
httpClient := http.Client{
Transport: cachedTransport,
}
// Do first request to populate local database.
httpClient.Get("https://www.google.com")
// Subsequent requests read from database with no outgoing HTTP request.
for _ = range 10 {
response, _ := httpClient.Get("https://www.google.com")
defer response.Body.Close()
responseBody, _ := io.ReadAll(response.Body)
fmt.Println(string(responseBody))
}
}
```
## ❓ Questions and Support
Any questions can be submitted via [GitHub Issues](https://www.github.com/alexmerren/httpcache/issues). Feel free to start contributing or asking any questions required!