cache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 11 Imported by: 0

README

cache

I got tired of rewriting HTTP response caching logic for all my microservices so I hacked together this library that saves HTTP responses as local .txt files. cache is smart enough to return the contents of the cache file if it's only been a bit since the last call to that endpoint (cache filenames are generated as a hash of the HTTP method, URL, and headers). If the cache is expired, it remakes the call, saves the response to a file, and returns the contents either as a string or a struct. See usage examples below.

Installation

go get github.com/jessemillar/cache

Usage

import "github.com/jessemillar/cache"

See cache_test.go for usage examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Response

type Response struct {
	StatusCode int    `json:"status"`
	Body       string `json:"body"`
}

func BasicHttpRequest

func BasicHttpRequest(httpMethod string, url string) (Response, error)

BasicHttpRequest makes a request with default parameters

func BasicHttpRequestReturnStruct

func BasicHttpRequestReturnStruct(httpMethod string, url string, target interface{}) (Response, error)

BasicHttpRequestReturnStruct makes a request with default parameters

func HttpRequest

func HttpRequest(httpMethod string, url string, headers map[string][]string, cacheTTLOverride time.Duration, allowCacheUpdate bool) (Response, error)

HttpRequest sends an HTTP request to the specified URL and returns the HTTP response. The response is cached for a duration specified by cacheTTL. If cacheTTLOverride is zero, the default cache TTL value is used.

func HttpRequestReturnStruct

func HttpRequestReturnStruct(httpMethod string, url string, headers map[string][]string, cacheTTLOverride time.Duration, allowCacheUpdate bool, target interface{}) (Response, error)

HttpRequestReturnStruct is the same as HttpRequest but it returns the result as a specified struct. HttpRequest sends an HTTP request to the specified URL and returns the HTTP response. The response is cached for a duration specified by cacheTTL. If cacheTTLOverride is zero, the default cache TTL value is used.

Jump to

Keyboard shortcuts

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