GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - bxcodec/httpcache: Get a working HTTP Cache in Go (Golang) with only 3 lines of code!!!!

This repository was archived by the owner on Mar 8, 2024. It is now read-only.
BranchesTags
Open more actions menu

Latest commit

 

History

32 Commits
32 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
cache
cache
 
 
helper/cacheheader
helper/cacheheader
 
 
misc/makefile
misc/makefile
 
 
mocks
mocks
 
 
.gitignore
.gitignore
 
 
.golangci.yaml
.golangci.yaml
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
example_custom_storage_test.go
example_custom_storage_test.go
 
 
example_storages_test.go
example_storages_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
httpcache.go
httpcache.go
 
 
roundtriper.go
roundtriper.go
 
 
roundtripper_test.go
roundtripper_test.go
 
 

Repository files navigation

[DEPRECATED] httpcache, inject-able HTTP cache in Golang

Hi all, this library is no longer maintained. I don't see any usecase when to use this. Caching logic is a critical and complex problem. There are a lot of caching solutions that you can try, including this httpcache. But, as systems grow, httpcache is really not relevant/important. Thanks for the support and contributions so far. You can find me at https://imantumorang.com if you later need a similar library that we can collaborate on together.

Howdy there!!!

Usually when we want to integrate with cache (let's say Redis), we usually have to do many changes in our code. What if, we just inject the cache to the HTTP client. So we don't have to create many changes in every line of our code to support the cache features? With only less than 10 line of code, you can got a complete implementations of HTTP Cache based on RFC 7234

Build Status License GoDoc Go.Dev

This package is used for caching your http request results from the server. Example how to use can be seen below.

Index

  • Support
  • Getting Started
  • Example
  • Contribution

Support

You can file an Issue. See documentation in Godoc or in go.dev

Getting Started

Download

go get -u github.com/bxcodec/httpcache

Example with Inmemory Storage


Example how to use more details can be seen in the example file: ./example_inmemory_storage_test.go

Short example:

// Inject the HTTP Client with httpcache
client := &http.Client{}
_, err := httpcache.NewWithInmemoryCache(client, true, time.Second*60)
if err != nil {
  log.Fatal(err)
}

// And your HTTP Client already supported for HTTP Cache
// To verify you can run a request in a loop

for i:=0; i< 10; i++ {
  startTime := time.Now()
  req, err := http.NewRequest("GET", "https://imantumorang.com", nil)
  if err != nil {
    log.Fatal((err))
  }

  res, err := client.Do(req)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Printf("Response time: %vms\n", time.Since(startTime).Microseconds())
  fmt.Println("Status Code", res.StatusCode)
}
// See the response time, it will different on each request and will go smaller.

Example with Custom Storage

You also can use your own custom storage, what you need to do is implement the cache.ICacheInteractor interface. Example how to use more details can be seen in the example file: ./example_custom_storage_test.go

Example:

client := &http.Client{}
_, err := httpcache.NewWithCustomStorageCache(client,true, mystorage.NewCustomInMemStorage())
if err != nil {
	log.Fatal(err)
}

About RFC 7234 Compliance

You can disable/enable the RFC Compliance as you want. If RFC 7234 is too complex for you, you can just disable it by set the RFCCompliance parameter to false

_, err := httpcache.NewWithInmemoryCache(client, false, time.Second*60)
// or
_, err := httpcache.NewWithCustomStorageCache(client,false, mystorage.NewCustomInMemStorage())

The downside of disabling the RFC Compliance, All the response/request will be cached automatically. Do with caution.

TODOs

  • See the issues

Inspirations and Thanks

  • pquerna/cachecontrol for the Cache-Header Extraction
  • bxcodec/gothca for in-memory cache. *Notes: if you find another library that has a better way for inmemm cache, please raise an issue or submit a PR

Contribution


To contrib to this project, you can open a PR or an issue.

About

Get a working HTTP Cache in Go (Golang) with only 3 lines of code!!!!

Topics

cachegogolanggolang-libraryhttp-cachehttp-clienthttpcachehttpcachedclientrfc-7234

Resources

Readme
MIT license
Activity

Stars

58 stars

Watchers

2 watching

Forks

14 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.