cache

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 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 containing JSON data. 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.

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

func BasicHttpRequestReturnStruct

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

BasicHttpRequestReturnStruct makes a request with default parameters

func GetCacheAndStalenessReturnStruct added in v1.1.0

func GetCacheAndStalenessReturnStruct(cacheFilename string, cacheTTLOverride time.Duration, allowCacheUpdate bool, target interface{}) (bool, error)

GetCacheAndStalenessReturnStruct is the same as GetCacheAndStaleness but it returns the result as a specified struct. GetCacheAndStalenessReturnStruct returns the contents of the cache file as a struct and whether or not the cache is stale (this does not make an HTTP request)

func GetCacheFileAsString added in v1.4.1

func GetCacheFileAsString(filename string) (string, error)

GetCacheFileAsString reads the content of a file and returns it as a string.

func GetCacheFileAsStruct added in v1.2.0

func GetCacheFileAsStruct(filename string, target interface{}) error

GetCacheFileAsStruct reads the content of a file, unmarshals it as JSON into the target structure.

func HttpRequestReturnStruct

func HttpRequestReturnStruct(httpMethod string, url string, headers map[string][]string, cacheTTLOverride time.Duration, allowCacheUpdate bool, target interface{}) (int, 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.

func WriteStringToCacheFile added in v1.1.0

func WriteStringToCacheFile(filename string, value string) error

func WriteStructToCacheFile added in v1.1.0

func WriteStructToCacheFile(filename string, rawStruct interface{}) error

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 GetCacheAndStaleness added in v1.1.0

func GetCacheAndStaleness(cacheFilename string, cacheTTLOverride time.Duration, allowCacheUpdate bool) (Response, bool, error)

GetCacheAndStaleness returns the contents of the cache file and whether or not the cache is stale (this does not make an HTTP request)

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.

Jump to

Keyboard shortcuts

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