codemicro / fiber-cache

Simple and easy to use response caching middleware for the Fiber web framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fiber-cache

GitHub release (latest by date) Keep a Changelog v1.0.0 badge PkgGoDev Tests Gosec audit

fiber-cache is middleware that provides caching for specific routes in a Fiber application.

fiber-cache is now deprecated

Fiber v2.0.3+ now includes a bundled cache middleware, hence this package is no longer required. See https://github.com/gofiber/fiber/tree/master/middleware/cache for more info.

Examples

The most basic caching you can do is like this:

app.Get("/your/route", fcache.New(), yourHandler)

This will then cache the response and status code generated by that endpoint for the default TTL (Time-To-Live) specified. You don't have to do anything different in your handler than you normally would. If you have not changed the default TTL value, it is 5 * time.Minute.

You can change the default TTL for all endpoints by setting fcache.Config.DefaultTTL to a time.Duration. This will only have an effect on handlers registered after you change the value.

fcache.Config.DefaultTTL = time.Minute * 2

You can also set the TTL for each different handler, for example if you want to set one handler to a 20 second TTL while also maintaining the default TTL at 5 minutes.

app.Get("/your/route", fcache.NewWithTTL(fcache.AutoGenerateKey, time.Second*20), yourHandler)

Whenever you have the option to set a TTL, you can also set it to have no expiration. The handler function will be called once and the value stored until your program has exited. This can be done using fcache.NoExpiration.

app.Get("/your/route", fcache.NewWithTTL(fcache.AutoGenerateKey, fcache.NoExpiration), yourHandler)

Internally, caching is done using key-value pairs. Normally, the keys are automatically generated, but you can choose to set them manually if you want.

app.Get("/your/route", fcache.NewWithKey("yourKeyHere"), yourHandler)

The cache key for the current route is stored in c.Locals as cacheKey. This is especially useful if you're using automatically generated keys and want to access the underlying cache engine. This can be done through the fcache.Cache variable. Please click here for information about that.

app.Get("/your/route", fcache.New(), func(c *fiber.Ctx) error {
    cacheKey := c.Locals("cacheKey").(string) // -> cacheKey-0
    
    data, found := fcache.Cache.Get(cacheKey)

    // ...

    return c.Send("My cache key is: " + cacheKey) // -> My cache key is: cacheKey-0
})

Limitations

1: headers

Aside from the Content-Type header, fiber-cache will not cache headers. If you want a certain header to be added to every response on a specific endpoint, you'll have to add a custom middleware before the cache middleware.

app.Get("/", func(c *fiber.Ctx) error {
        c.Append("server", "potato")
        return c.Next()
    }, fcache.New(), yourHandler)

The Content-Type header is the only header that is automatically cached and sent.

2: scope

fiber-cache should not be registered as a global middleware (app.Use(fcache.New())) as it will cache every endpoint, including responses from POST and DELETE endpoints while also not running the handler endpoint.

Reference

https://pkg.go.dev/github.com/codemicro/fiber-cache/v2

Installation

You must have Go 1.14 or higher installed before attempting installation.

Installation is done using the go get command:

go get -u github.com/codemicro/fiber-cache/v2

You can then import the package as follows:

import (
    fcache "github.com/codemicro/fiber-cache/v2"
)

Benchmarks

Consider the following example:

app.Get("/", fcache.New(), func(c *fiber.Ctx) error {
    time.Sleep(time.Second * 5)
    return c.SendString("Hello world")
})

Bombardier output (after 1 initial request)

> bombardier -c 125 -n 50000 http://127.0.0.1:5000
Bombarding http://127.0.0.1:5000 with 50000 request(s) using 125 connection(s)
 50000 / 50000 [=====================================================================================] 100.00% 124517/s 0s
Done!
Statistics        Avg      Stdev        Max
  Reqs/sec    160806.70   22109.28  175973.08
  Latency      775.73us   274.91us    15.00ms
  HTTP codes:
    1xx - 0, 2xx - 50000, 3xx - 0, 4xx - 0, 5xx - 0
    others - 0
  Throughput:    28.90MB/s

Licence

fiber-cache is free and open source software covered by the Mozilla Public Licence v2.

Third party library licences

About

Simple and easy to use response caching middleware for the Fiber web framework

License:Mozilla Public License 2.0


Languages

Language:Go 100.0%