Gin-cache - Gin cache middleware with golang

Related tags

Caching gin-cache
Overview

Build Status Go Report Card codecov

Gin cache middleware

Install

go get -u github.com/pygzfei/gin-cache

Quick start

Use memory cache

package main

import (
	"github.com/gin-gonic/gin"
	"time"
)

func main() {
	cache := NewMemoryCache(
		time.Minute * 30, // 30 Minutes Cache will be invalid
	)
	r := gin.Default()

	r.GET("/ping", cache.Handler(
		Caching{
			Cacheable: []Cacheable{
				// #id# is your query or post data, if query `/?id=1`, kye in cache will be `anson:userid:1`
				{CacheName: "anson", Key: `id:#id#`},
			},
		},
		func(c *gin.Context) {
			c.JSON(200, gin.H{
				"message": "pong", // response data will be cache
			})
		},
	))

	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

Trigger Cache evict

// post data: {"id": 1}
// accurately delete key: `anson:userid:1`
r.POST("/ping", cache.Handler(
    Caching{
        Evict: []CacheEvict{
            // #id# in your post data field is id, e.q `{"id": 1}`
            {CacheName: []string{"anson"}, Key: "id:#id#"},
        },
    },
    func(c *gin.Context) {
        // ...
    },
))

// And you can use wildcard '*'
// When this data in your cache ["anson:id:1", "anson:id:2", "anson:id:3"]
// The key start with `anson:id` will be delete in your cache 
r.POST("/ping", cache.Handler(
    Caching{
        Evict: []CacheEvict{
            // #id# in your post data field is id, e.q `{"id": 1}`
            {CacheName: []string{"anson"}, Key: "id:*"},
        },
    },
    func(c *gin.Context) {
        // ...
    },
))

Use Redis

cache := NewRedisCache(time.Second*30, &redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})
	
You might also like...
A File Cache With Golang

A File Cache With Golang

Gocodecache - An in-memory cache library for code value master in Golang

gocodecache An in-memory cache library for code master in Golang. Installation g

LevelDB style LRU cache for Go, support non GC object.

Go语言QQ群: 102319854, 1055927514 凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa LRU Cache Install go get github.com/chai2010/c

groupcache is a caching and cache-filling library, intended as a replacement for memcached in many cases.

groupcache Summary groupcache is a distributed caching and cache-filling library, intended as a replacement for a pool of memcached nodes in many case

☔️ A complete Go cache library that brings you multiple ways of managing your caches
☔️ A complete Go cache library that brings you multiple ways of managing your caches

Gocache Guess what is Gocache? a Go cache library. This is an extendable cache library that brings you a lot of features for caching data. Overview He

Ristretto is a fast, concurrent cache library built with a focus on performance and correctness.
fastcache - fast thread-safe inmemory cache for big number of entries in Go

Fast thread-safe inmemory cache for big number of entries in Go. Minimizes GC overhead

Efficient cache for gigabytes of data written in Go.

BigCache Fast, concurrent, evicting in-memory cache written to keep big number of entries without impact on performance. BigCache keeps entries on hea

An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.

go-cache go-cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine. Its major

Releases(v1.1.3)
Owner
Anson
Anson
Package cache is a middleware that provides the cache management for Flamego.

cache Package cache is a middleware that provides the cache management for Flamego. Installation The minimum requirement of Go is 1.16. go get github.

Flamego 11 Nov 9, 2022
Cache library for golang. It supports expirable Cache, LFU, LRU and ARC.

GCache Cache library for golang. It supports expirable Cache, LFU, LRU and ARC. Features Supports expirable Cache, LFU, LRU and ARC. Goroutine safe. S

Jun Kimura 2.2k Nov 18, 2022
A mem cache base on other populator cache, add following feacture

memcache a mem cache base on other populator cache, add following feacture add lazy load(using expired data, and load it asynchronous) add singlefligh

zhq 1 Oct 28, 2021
Cache - A simple cache implementation

Cache A simple cache implementation LRU Cache An in memory cache implementation

Stanislav Petrashov 1 Jan 25, 2022
An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC

GCache Cache library for golang. It supports expirable Cache, LFU, LRU and ARC. Features Supports expirable Cache, LFU, LRU and ARC. Goroutine safe. S

Jun Kimura 318 May 31, 2021
gdcache is a pure non-intrusive distributed cache library implemented by golang

gdcache is a pure non-intrusive distributed cache library implemented by golang, you can use it to implement your own distributed cache

Jovan 10 Sep 26, 2022
Cache list, count with filter param golang, using struct, hashkey

Dumbcache Cache list, count with filter param golang, using struct, hashkey Structure we hash your request object to md5 hashing and add a prefix coun

Te Nguyen 4 Nov 1, 2021
Fast key-value cache written on pure golang

GoCache Simple in-memory key-value cache with default or specific expiration time. Install go get github.com/DylanMrr/GoCache Features Key-value stor

Dmitriy Eginov 1 Nov 16, 2021
Light weight thread safe cache for golang

go-cache Light weight thread safe LRU cache Getting started import( "fmt" "github.com/tak1827/go-cache/lru" ) func main() { size := 2 cache := l

tak 1 Dec 12, 2021
Continuous Benchmark for cache libraries written in golang.

Simple performance comparison of cache libraries written in golang. Reports Continuous Bencmark Result (click here) Default parameters 256 shards * 32

bench+ 9 Oct 26, 2022