Giter Club home page Giter Club logo

krakend-httpcache's Introduction

Krakend HTTP Cache

A cached http client for the KrakenD framework tested on krakend-ce 1.4.1

Using it

This package exposes two simple factories capable to create a instances of the proxy.HTTPClientFactory and the proxy.BackendFactory interfaces, respectively, embedding an in-memory-cached http client using the package github.com/gregjones/httpcache. The client will cache the responses honoring the defined Cache HTTP header.

import 	(
	"context"
	"net/http"
	"github.com/luraproject/lura/config"
	"github.com/luraproject/lura/proxy"
	"github.com/devopsfaith/krakend-httpcache"
)

requestExecutorFactory := func(cfg *config.Backend) proxy.HTTPRequestExecutor {
	clientFactory := httpcache.NewHTTPClient(cfg)
	return func(ctx context.Context, req *http.Request) (*http.Response, error) {
		return clientFactory(ctx).Do(req.WithContext(ctx))
	}
}

You can create your own proxy.HTTPRequestExecutor and inject it into your BackendFactory

Lean changes

Modified added support to redis & redis-cluster as cache backends.

Backend extra configuration block example with default values:

"backend": [
{
    "extra_config": {
        "github.com/devopsfaith/krakend-httpcache": {
            "type": "redis",
            "redis": {
                "mode": "redis",
                "address": "localhost:6379",
                "dialTimeout": "100ms",
                "readTimeout": "100ms",
                "writeTimeout": "200ms",
                "maxRetries": 0,
                "idleTimeout": "5m",
                "idleCheckFrequency": "1m",
                "poolSize": 10,
                "poolTimeout": "10ms",
                "ttl": "1h",
            }
        },
    }
}

Config options:

  • type (optional, if missing memory will be used for an inmemory cache): memory | redis

redis block is required if type is set to redis:

  • mode (required): redis or rediscluster
  • address (required): Address to the redis node or one of the redis nodes when using a cluster. Format: ip:port
  • ttl: TTL to be used on all redis keys being set. Redis will expire the keys after this time. This value is independent from the Cache-Control header that the library will use to decide if the cached value is still valid or not.

Check the description of the following options in the go-redis package documentation: https://github.com/go-redis/redis

  • dialTimeout:
  • readTimeout:
  • writeTimeout:
  • maxRetries:
  • idleTimeout:
  • idleCheckFrequency:
  • poolSize:
  • poolTimeout:

How to build into krakend

We need to recompile kraken-cd in order to interpret new redis config schema

Edit krakend-ce go.mod at the end add

replace github.com/devopsfaith/krakend-httpcache v1.4.0 => github.com/leandroreox/krakend-httpcache v1.4.1

Build inside a go 1.16 container

docker run --rm -it -v "$(pwd):/app" -w /app golang:1.16.4 bash
make build

re-build krakend-ce, and run it !

krakend-httpcache's People

Watchers

 avatar  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.