gotcha

package module
v1.0.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2019 License: MIT Imports: 3 Imported by: 4

README

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
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)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCache use for default cache client
	DefaultCache = New()
)

Functions

func ClearCache

func ClearCache() (err error)

ClearCache will Clear the cache using default option

func Delete

func Delete(key string) (err error)

Delete will delete an item from the cache using default option

func Get

func Get(key string) (value interface{}, err error)

Get will get an item from cache using default option

func GetKeys

func GetKeys() (keys []string, err error)

GetKeys will get all keys from the cache using default option

func New

func New(options ...*cache.Option) (c cache.Cache)

New will create a new cache client. If the options not set, the cache will use the default options

func NewOption

func NewOption() (op *cache.Option)

NewOption return an empty option

func Set

func Set(key string, value interface{}) (err error)

Set will set an item to cache using default option

Types

This section is empty.

Directories

Path Synopsis
examples
basic Module
lfu
lru

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL