gorsy_cache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2019 License: MIT Imports: 6 Imported by: 0

README

                                                      __       
   ____ _____  ____________  __      _________ ______/ /_  ___ 
  / __ `/ __ \/ ___/ ___/ / / /_____/ ___/ __ `/ ___/ __ \/ _ \
 / /_/ / /_/ / /  (__  ) /_/ /_____/ /__/ /_/ / /__/ / / /  __/
 \__, /\____/_/  /____/\__, /      \___/\__,_/\___/_/ /_/\___/ 
/____/                /____/                                   

Gorsy is a concurrency-safe in-memory k/v cache store implemented by Golang that supports the lru, lfu, arc algorithm etc.

Example

package main

import (
	"fmt"
	"github.com/arianxx/gorsy-cache"
)

func loader(_ interface{}) (interface{}, error) {
	return "baka!", nil
}

func beforeEvict(key, value interface{}) {
	fmt.Printf("%v: %v was be removed\n", key, value)
}

func main() {
	builder, err := gorsy_cache.NewBuilder(gorsy_cache.LFU, 10)
	if err != nil {
		panic("build cache error: " + err.Error())
	}
	cache := builder.
		SetName("rocket").
		SetDefaultExpiration(gorsy_cache.DefaultExpiration).
		SetLoaderFunc(loader).
		SetBeforeEvictedFunc(beforeEvict).
		Build()
	cache.Set(1, 2)
	cache.Set(2, 3)
	fmt.Println(cache.Get(1))
	fmt.Println(cache.Get(2))
	fmt.Println(cache.GetOnlyPresent(3))
	cache.Remove(1)
	fmt.Println(cache.Get(1))
}

Documentation

Index

Constants

View Source
const (
	NoExpiration      = -1
	DefaultExpiration = 0

	NoPurge              = -1
	DefaultPurgeInterval = 0
)
View Source
const (
	ARC = "arc"
)
View Source
const (
	LFU = "lfu"
)
View Source
const (
	LRU = "lru"
)
View Source
const (
	SIMPLE = "simpleCache"
)

Variables

This section is empty.

Functions

func NewBuilder

func NewBuilder(name string, size int) (*cacheBuilder, error)

NewBuilder receive a constant cache name and a cache size, return a specific cache builder. A error will be returned if the specific cache is not registered or the cache implementation is invalid.

func StartPurge

func StartPurge(c *Cache, d time.Duration) error

func StopPurge

func StopPurge(c *Cache)

Types

type BeforeEvictedFunc

type BeforeEvictedFunc func(key, value interface{})

type Cache

type Cache interface {
	Init()
	Get(key interface{}) (interface{}, error)
	GetOnlyPresent(key interface{}) (interface{}, bool)
	Set(key, value interface{})
	SetWithExpire(key, value interface{}, duration time.Duration)
	Has(key interface{}) bool
	Remove(key interface{}) bool
	Keys() []interface{}
	CleanExpired() int
	Flush()
	Len() int
	// contains filtered or unexported methods
}

Cache represents a interface used by the user to r/w the cache store.

type KeyNotFoundError

type KeyNotFoundError struct {
	Name string
	Key  interface{}
	Err  error
}

func (*KeyNotFoundError) Error

func (e *KeyNotFoundError) Error() string

type LoaderFunc

type LoaderFunc func(key interface{}) (interface{}, error)

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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