httpcache

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

Inject-able HTTP cache in Golang

Just inject the cache to the HTTP client, this will integrate with cache (Redis)

Quickstart

Make sure you have Go installed (download). Version 1.16 or higher is required.

Initialize your project by creating a folder and then running go mod init github.com/your/repo (learn more) inside the folder. Then install httpcache library with the go get command:

go get -u github.com/hinha/httpcache

Example

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/hinha/httpcache"
	"github.com/hinha/httpcache/cache"
)

var (
	url   = "https://google.com"
	token = "toke"
)

func main() {
	client := &http.Client{}
	// when webStatic is true can only be used static web ex: google.com
	// cause need cache-control
	webStatic := true
	_ = cache.NewRedisCache(client, &httpcache.RedisCacheOptions{
		Addr: "localhost:6379",
	}, time.Second*time.Duration(60))

	header := http.Header{}
	// header.Set("Authorization", token) // if need header

	for i := 0; i < 10; i++ {
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			log.Fatal((err))
		}
		req.Header = header

		res, err := client.Do(req)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("Status Code", res.StatusCode)
	}
}

Inspirations and Thanks

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrStorageNotFound will throw when some not found key error in storage
	ErrStorageNotFound = errors.New("key is missing")
	// ErrStorageInternal will throw when some internal error in storage occurred
	ErrStorageInternal = errors.New("internal error in storage")
)
View Source
var (
	HeaderAuthorization = "Authorization"
	HeaderCacheControl  = "Cache-Control"

	XFromHache   = "X-HTTPCache"
	XHacheOrigin = "X-HTTPCache-Origin"
)

Functions

This section is empty.

Types

type CacheHandler

type CacheHandler struct {
	// contains filtered or unexported fields
}

func NewCacheHandlerRoundtrip

func NewCacheHandlerRoundtrip(roundTripper http.RoundTripper, ICache CacheInterface, webStaticFile bool) *CacheHandler

NewCacheHandlerRoundtrip will create an implementations of cache http roundtripper

func (*CacheHandler) RoundTrip

func (c *CacheHandler) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip the implementation of http.RoundTripper

type CacheInterface

type CacheInterface interface {
	Set(key string, value CachedResponse) error
	Get(key string) (res CachedResponse, err error)
	Delete(key string) error
	Flush() error
}

CacheInterface implement method cache

type CachedResponse

type CachedResponse struct {
	Response      []byte    `json:"response"`      // The dumped response body
	RequestURI    string    `json:"requestUri"`    // The requestURI of the response
	RequestMethod string    `json:"requestMethod"` // The HTTP Method that call the request for this response
	CachedTime    time.Time `json:"cachedTime"`    // The timestamp when this response is Cached
}

CachedResponse represent the cacher struct item

func (CachedResponse) ToByte

func (c CachedResponse) ToByte() []byte

type RedisCacheOptions

type RedisCacheOptions struct {
	Addr     string
	Password string
	DB       int
}

RedisCacheOptions for storing data for Redis connections

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL