timoth-y/kicksware-api

View on GitHub
services/cdn/api/rest/cache.go

Summary

Maintainability
A
0 mins
Test Coverage
package rest

import (
    "fmt"
    "net/http"
    "os"
    "time"

    "github.com/victorspringer/http-cache"
    "github.com/victorspringer/http-cache/adapter/memory"
)

func (h *Handler) CacheController(next http.Handler) http.Handler {
    return h.cache.Middleware(next)
}

func newCacheClient() *cache.Client {
    memcached, err := memory.NewAdapter(
        memory.AdapterWithAlgorithm(memory.MFU),
        memory.AdapterWithCapacity(100000),
    )
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    cacheClient, err := cache.NewClient(
        cache.ClientWithAdapter(memcached),
        cache.ClientWithTTL(10 * time.Minute),
        cache.ClientWithRefreshKey("opn"),
    )
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    return cacheClient
}