Git Product home page Git Product logo

replay's Introduction

Replay

Configurable http caching middleware for Go servers.
Improve API throughput, reduce latency, and save on third-party resources.

Features

  • Eviction Policy: FIFO, LRU
  • Cache Size: Based on number of entries, or memory usage
  • Cache Filters: Filter on URL, method, or header fields
  • TTL + Max TTL: Cache expiration and renewal
  • Logger: Pass any logger package
  • Metrics: Track cache hits, misses, and evictions

Example

import (
	"log"
	"net/http"
	"os"
	"time"
	"github.com/Ztkent/replay"
	"github.com/go-chi/chi/v5"
)

func main() {
	r := chi.NewRouter()
	c := replay.NewCache(
		replay.WithMaxSize(100),
		replay.WithMaxMemory(100*1024*1024),
		replay.WithCacheFilters([]string{"URL", "Method"}),
		replay.WithCacheFailures(false),
		replay.WithEvictionPolicy("LRU"),
		replay.WithTTL(5*time.Minute),
		replay.WithMaxTTL(30*time.Minute),
		replay.WithLogger(log.New(os.Stdout, "replay: ", log.LstdFlags)),
	)

	// Apply the middleware to the entire router
	r.Use(c.Middleware)
	// Or apply it to a specific endpoint
	r.Get("/test-endpoint", c.MiddlewareFunc(testHandlerFunc()))
	http.ListenAndServe(os.Getenv("SERVER_PORT"), r)
}

Options

The cache configured with the following options:

  • WithMaxSize(maxSize int): Set the maximum number of entries in the cache.
  • WithMaxMemory(maxMemory uint64): Set the maximum memory usage of the cache.
  • WithEvictionPolicy(evictionPolicy string): Set the eviction policy for the cache [FIFO, LRU].
  • WithEvictionTimer(evictionTimer time.Duration): Set the time between cache eviction checks.
  • WithTTL(ttl time.Duration): Set the time a cache entry can live without being accessed.
  • WithMaxTTL(maxTtl time.Duration): Set the maximum time a cache entry can live, including renewals.
  • WithCacheFilters(cacheFilters []string): Set the cache filters to use for generating cache keys.
  • WithCacheFailures(cacheFailures bool): Set whether to cache failed requests.
  • WithLogger(l *log.Logger): Set the logger to use for cache logging.

Metrics

You can access cache metrics to monitor cache performance:

metrics := c.Metrics()

Fields Available:

  • Hits: Total number of cache hits.
  • Misses: Total number of cache misses.
  • Evictions: Total umber of cache evictions.
  • CurrentSize: Number of entries in the cache.
  • CurrentMemory: Current memory usage of the cache.

replay's People

Contributors

ztkent avatar

Watchers

 avatar

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.