cacher

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

Cacher

Common cacher library for Internal affairs golang projects

Usage example

This library can be used for store information in cache files. That information will be get from the server and the stored in a file.

import (
    "net/http"
    
    "https://github.com/relex/gotils/cacher"
)

req, _ := http.NewRequest("GET", "http://localhost:8080", nil)
req.Header.Add("PRIVATE-TOKEN", "myToken")
// body contains the current information which was stored in the file
body, err := cacher.GetFromURLOrDefaultCache(req, "myCacheFolder")
if err != nil {
    //YOUR STUFF
}
...

Documentation

Index

Constants

View Source
const (
	// Addr is the server address
	Addr string = "localhost:12345"
)

Variables

This section is empty.

Functions

func GetFromURLOrDefaultCache

func GetFromURLOrDefaultCache(req *http.Request, cacheDir string) (string, error)

GetFromURLOrDefaultCache downloads file into cacheDir and returns its content

If the URL is not available, attempt to read the previous response from cache

The function only returns remote error if both downloading from the URL and reading from existing cache fail, cache-related error is only logged, not reported.

func GetFromURLOrDefaultCacheWithCallback

func GetFromURLOrDefaultCacheWithCallback(req *http.Request, cacheDir string, onData func([]byte) error) error

GetFromURLOrDefaultCacheWithCallback downloads file into cacheDir and passes the content to the onData callback

If the URL is not available, attempt to read the previous response from cache

The onData function should process the data (e.g. parsing JSON) and return error on failure. It may be called a second time to process cache if the data from remote URL cannot be processed.

The function only returns remote error if both downloading from the URL and reading from existing cache fail, cache-related error is only logged, not reported.

func StartHTTPServer

func StartHTTPServer(testFilePath string) func()

StartHTTPServer starts a HTTP server in background.

Returns a function that closes the server and the listener completely

Types

This section is empty.

Jump to

Keyboard shortcuts

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