gocache

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2025 License: MIT Imports: 2 Imported by: 0 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Get cache by key

func (*GoCache) GetMulti

func (g *GoCache) GetMulti(keys []string) map[string]any

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 any, ttl time.Duration) error

Set cache by key

func (GoCache) SetMulti

func (g GoCache) SetMulti(values map[string]any, 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