Git Product home page Git Product logo

cache's Introduction

Cache

Behaves like a browser storing local copies of retrieved pages, sending ETag values when available and respect 304 responses.

This project provides a memory cache provider and a redis cache provider. New providers can be added by implementing the Provider interface.

A custom http client can also be provided, it just needs to implement the HttpRequester interface, which requires nothing but a Do method.

Usage

Using the cache is as simple as:

cache.New(memoryprovider.New())
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
    panic(err)
}
resp, err := cache.Do(req)
if err != nil {
    panic(err)
}
defer resp.Body.Close()

data := io.ReadAll(resp.Body)

If the response has proper expiry headers, the cache will prevent extra http calls to be made.

If the response providers a proper ETag header, the cache will store the response and return it on subsequent requests and 304 responses will be properly dealt with.

Storing cached data

The cache can use any data store that implements the Provider interface.

type Provider interface {
	// Get returns the value for the given key. Should only return an error if the value could be checked for existence or if communication fails.
	// If the value is not found just return nil, nil.
	Get(ctx context.Context, key string) ([]byte, error)

	// Set sets the value for the given key. Should return an error if the value could not be set.
	Set(ctx context.Context, key string, value []byte, expiry time.Duration) error
}

Two providers are provided:

  • memoryprovider - stores data in memory
  • redisprovider - takes a redis connection and stores data in redis

Setting parameters to calls

By modifying the context, the behaviour of the cache can be modified.

  • WithIgnoreExpired - the cache will return expired parameters without trying to refresh them
  • WithIgnoreCache - ignores any return values from the cache. Http responses are still cached.
  • WithOnlyCached - returns only a cached value, if it exists. Returns an ErrCacheMiss error if the value is not cached.

Logging

The cache can make use of any struct that implements the Logger interface.

type Logger interface {
	Debug(msg string, params ...any)
	Info(msg string, params ...any)
	Error(msg string, params ...any)
}

The cache will try to extract the logger from the context using the LogExtractor parameter. If no logger is found, nothing will be logged.

The logger for golang.org/x/exp/slog package can be used as a drop-in for the interface.

Author

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.