Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package module
v0.0.0-...-c8f64e8 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: GPL-3.0 Imports: 5 Imported by: 4

README

Go Memory Cache

No periodically scheduler is running without your actions. It times the cleanup to the next expiry.

Usage
import(
	"github.com/ProxeusApp/memcache"
	"fmt"
	"time"
)

func main(){
	c := cache.New(3 * time.Second)
	//or extend expiry on get 'true' | 'false'
	c = cache.NewExtendExpiryOnGet(3 * time.Second, true)

	c.Put("myKey", "my Value")
	c.PutWithOtherExpiry("myKey", "my Value", 6*time.Second)
	c.Put(123, 456)
	c.Put(1.456, 7.89)
	c.OnExpired = func(key interface{}, val interface{}){
		fmt.Println("on expired", key, val)
	}
	time.Sleep(10*time.Second)

	var myVal string
	err := c.Get("myKey", &myVal)
	if err != nil {
	    panic(err)
	}
	fmt.Println(myVal)

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotExist = errors.New("cache not exist")
)

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Expiry time.Duration

	OnExpired func(key interface{}, val interface{})
	// contains filtered or unexported fields
}

func New

func New(expiry time.Duration) *Cache

func NewExtendExpiryOnGet

func NewExtendExpiryOnGet(expiry time.Duration, extendExpiry bool) *Cache

func (*Cache) Clean

func (s *Cache) Clean()

func (*Cache) Close

func (s *Cache) Close()

func (*Cache) Get

func (s *Cache) Get(key interface{}, ref interface{}) error

func (*Cache) GetAndExtendExpiry

func (s *Cache) GetAndExtendExpiry(key interface{}, ref interface{}, extendExpiry bool) error

func (*Cache) Put

func (s *Cache) Put(key interface{}, val interface{})

func (*Cache) PutWithOtherExpiry

func (s *Cache) PutWithOtherExpiry(key interface{}, val interface{}, expiry time.Duration)

func (*Cache) Remove

func (s *Cache) Remove(key interface{}) bool

Jump to

Keyboard shortcuts

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