gocache

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2022 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Package gocache is a memory cache driver implement. base on the package: github.com/patrickmn/go-cache

Usage:

import "github.com/gookit/cache"

cache.Register(gocache.NewGoCache(0, cache.FiveMinutes))
// use
// cache.Set("key", "value")
Example
package main

import (
	"fmt"
	"time"

	"github.com/gookit/cache"
	"github.com/gookit/cache/gocache"
)

func main() {
	c := gocache.New()
	key := "name"

	// set
	c.Set(key, "cache value", cache.Seconds2)
	fmt.Println(c.Has(key))

	// get
	val := c.Get(key)
	fmt.Println(val)

	time.Sleep(2 * time.Second)

	// get expired
	val2 := c.Get(key)
	fmt.Println(val2)

	// del
	c.Del(key)
	fmt.Println(c.Has(key))

}
Output:

true
cache value
<nil>
false

Index

Examples

Constants

View Source
const Name = "gocache"

Name driver name

Variables

This section is empty.

Functions

This section is empty.

Types

type GoCache

type GoCache struct {
	// contains filtered or unexported fields
}

GoCache struct

Example (In_cachePkg)
package main

import (
	"fmt"
	"time"

	"github.com/gookit/cache"
	"github.com/gookit/cache/gocache"
)

func main() {
	c1 := gocache.NewGoCache(cache.OneDay, cache.FiveMinutes)
	cache.Register(gocache.Name, c1)
	defer cache.UnregisterAll()

	key := "name1"

	// set
	cache.Set(key, "cache value", cache.Seconds2)
	fmt.Println(cache.Has(key))

	// get
	val := cache.Get(key)
	fmt.Println(val)

	time.Sleep(2 * time.Second)

	// get expired
	val2 := cache.Get(key)
	fmt.Println(val2)

	// del
	cache.Del(key)
	fmt.Println(cache.Has(key))

}
Output:

true
cache value
<nil>
false

func New

func New() *GoCache

New create instance

func NewGoCache

func NewGoCache(defaultExpiration, cleanupInterval time.Duration) *GoCache

NewGoCache create instance with settings

func NewSimple added in v0.2.0

func NewSimple() *GoCache

NewSimple create new simple instance

func (*GoCache) Clear

func (g *GoCache) Clear() error

Clear all caches

func (*GoCache) Close

func (g *GoCache) Close() error

Close connection

func (*GoCache) Db added in v0.2.0

func (g *GoCache) Db() *goc.Cache

Db get the goc.Cache

func (GoCache) Del

func (g GoCache) Del(key string) error

Del cache by key

func (*GoCache) DelMulti

func (g *GoCache) DelMulti(keys []string) error

DelMulti db by keys

func (*GoCache) Get

func (g *GoCache) Get(key string) interface{}

Get cache by key

func (*GoCache) GetMulti

func (g *GoCache) GetMulti(keys []string) map[string]interface{}

GetMulti cache by keys

func (*GoCache) Has

func (g *GoCache) Has(key string) bool

Has cache key

func (*GoCache) Set

func (g *GoCache) Set(key string, val interface{}, ttl time.Duration) error

Set cache by key

func (GoCache) SetMulti

func (g GoCache) SetMulti(values map[string]interface{}, ttl time.Duration) error

SetMulti cache by keys

Jump to

Keyboard shortcuts

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