Skip to content
[Not Safe For Production] gotcha: inmemory-cache in Go (Golang) with customizable algorithm
Branch: master
Clone or download
bxcodec feat(gotcha): add max-memory treshold (#20)
* add max-memory

* update test

* refactor the internal pacakge

* define minimun go version

* remove unused comment

* add condition to not using maxmemory to increase performance

* update readme for explanation

* Update README.md
Latest commit edea59d Apr 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cache feat(gotcha): add max-memory treshold (#20) Apr 18, 2019
internal feat(gotcha): add max-memory treshold (#20) Apr 18, 2019
.gitignore feat(gotcha): add max-memory treshold (#20) Apr 18, 2019
LICENSE chore(gotcha): add LICENSE (#15) Apr 14, 2019
Makefile
README.md feat(gotcha): add max-memory treshold (#20) Apr 18, 2019
go.mod
go.sum
gotcha.go feat(gotcha): add max-memory treshold (#20) Apr 18, 2019
gotcha_test.go feat(gotcha): add documentations and pubic (#12) Apr 13, 2019

README.md

gotcha

gotcha: inmemory-cache in Go (Golang) with customizable algorithm

GoDoc

Index

Support

You can file an Issue. See documentation in Godoc

Getting Started

Download

go get -u github.com/bxcodec/gotcha

Example

With Cache Client

package main

import (
	"fmt"
	"log"

	"github.com/bxcodec/gotcha"
)

func main() {
	cache := gotcha.New()
	err := cache.Set("name", "John Snow")
	if err != nil {
		log.Fatal(err)
	}
	val, err := cache.Get("name")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(val)
}

Without Cache Client

package main

import (
	"fmt"
	"log"

	"github.com/bxcodec/gotcha"
)

func main() {
	err := gotcha.Set("name", "John Snow")
	if err != nil {
		log.Fatal(err)
	}
	val, err := gotcha.Get("name")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(val)
}

With Custom Cache ALgorithm

You can also custom and change the algorithm, expiry-time and also maximum memory.

gotcha.NewOption().SetAlgorithm(cache.LRUAlgorithm).
	  SetExpiryTime(time.Minute * 10).
	  SetMaxSizeItem(100).
	  SetMaxMemory(cache.MB * 10)

Warn: Even gotcha support for MaxMemory, but the current version it's still using a simple json/encoding to count the byte size. So it will be slower if you set the MaxMemory.

Benchmark for LRU with/without MaxMemory

# With MaxMemory
20000000	      7878 ns/op	    1646 B/op	      20 allocs/op

# Without MaxMemory
200000000	       776 ns/op	     150 B/op	       6 allocs/op

If you seeking for fast performances and also your memory is high, ignore the MaxMemory options. I'm still looking for the better solutions for this problem. And if you have a better solutions, please kindly open and issue or submit a PR directly for the better results.

LRU

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/bxcodec/gotcha"
	"github.com/bxcodec/gotcha/cache"
)

func main() {
	cache := gotcha.New(
		gotcha.NewOption().SetAlgorithm(cache.LRUAlgorithm).
			SetExpiryTime(time.Minute * 10).SetMaxSizeItem(100),
	)
	err := cache.Set("Kue", "Nama")
	if err != nil {
		log.Fatal(err)
	}
	val, err := cache.Get("Kue")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(val)
}

LFU

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/bxcodec/gotcha"
	"github.com/bxcodec/gotcha/cache"
)

func main() {
	cache := gotcha.New(
		gotcha.NewOption().SetAlgorithm(cache.LFUAlgorithm).
			SetExpiryTime(time.Minute * 10).SetMaxSizeItem(100),
	)
	err := cache.Set("Kue", "Nama")
	if err != nil {
		log.Fatal(err)
	}
	val, err := cache.Get("Kue")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(val)
}

Contribution

  • You can submit an issue or create a Pull Request (PR)
You can’t perform that action at this time.