cache

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: GPL-2.0 Imports: 7 Imported by: 0

README

go-cache

Rewrite https://github.com/patrickmn/go-cache

Usage is similar to go-cache

import "github.com/Akvicor/go-cache"

// New Any
New[string, any](5*time.Minute, 0)
NewAny[string, any](5*time.Minute, 0)
// New Number
NewNumber[string, any](5*time.Minute, 0)
  • Any: [K comparable, V any] Allows any type as a value
  • Number: [K comparable, V number] Allows numeric types as values
type number interface {
  ~int | ~int8 | ~int16 | ~int32 | ~int64 |
  ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
  ~uintptr | ~float32 | ~float64
}

Documentation

Index

Constants

View Source
const (
	// NoExpiration Item has no expiration date
	NoExpiration time.Duration = -1
	// DefaultExpiration Equivalent to passing in the same expiration duration as was given to New() or NewFrom()
	// when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Any

type Any[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any](defaultExpiration, cleanupInterval time.Duration) *Any[K, V]

func NewAny

func NewAny[K comparable, V any](defaultExpiration, cleanupInterval time.Duration) *Any[K, V]

func NewAnyFrom

func NewAnyFrom[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, items map[K]Item[V]) *Any[K, V]

func NewFrom

func NewFrom[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, items map[K]Item[V]) *Any[K, V]

func (Any) Add

func (c Any) Add(k K, v V, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (Any) Delete

func (c Any) Delete(k K)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (Any) DeleteAll added in v0.0.3

func (c Any) DeleteAll()

DeleteAll delete all

func (Any) DeleteBySlice added in v0.0.5

func (c Any) DeleteBySlice(ks []K)

DeleteBySlice an item from the cache. Does nothing if the key is not in the cache.

func (Any) DeleteExpired

func (c Any) DeleteExpired()

DeleteExpired delete all expired items

func (Any) Flush

func (c Any) Flush()

Delete all items from the cache.

func (Any) Get

func (c Any) Get(k K) (V, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Any) GetWithExpiration

func (c Any) GetWithExpiration(k K) (V, time.Time, bool)

GetWithExpiration returns an item and its expiration time from the cache. It returns the item or nil, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was found.

func (Any) GetWithHit

func (c Any) GetWithHit(k K) (V, int, bool)

func (Any) GetWithHitExpiration

func (c Any) GetWithHitExpiration(k K) (V, int, time.Time, bool)

func (Any) GetsByMap added in v0.0.4

func (c Any) GetsByMap(in map[K]V) (map[K]V, bool)

GetsByMap items from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Any) GetsBySlice added in v0.0.4

func (c Any) GetsBySlice(ks []K) ([]V, bool)

GetsBySlice items from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Any) ItemCount

func (c Any) ItemCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.

func (Any) Items

func (c Any) Items() map[K]Item[V]

Copies all unexpired items in the cache into a new map and returns it.

func (Any) Load

func (c Any) Load(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Any) LoadFile

func (c Any) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Any) OnEvicted

func (c Any) OnEvicted(f func(key K, value V, hit int))

func (Any) Replace

func (c Any) Replace(k K, x V, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (Any) Save

func (c Any) Save(w io.Writer) (err error)

Save Write the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Any) SaveFile

func (c Any) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Any) Set

func (c Any) Set(k K, v V, d time.Duration)

Set Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Any) SetDefault

func (c Any) SetDefault(k K, v V)

SetDefault Add an item to the cache, replacing any existing item, using the default expiration.

func (Any) SetJanitor

func (c Any) SetJanitor(j *janitor)

func (Any) SetsByMap added in v0.0.4

func (c Any) SetsByMap(data map[K]V, d time.Duration)

SetsByMap Add items to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Any) SetsBySlice added in v0.0.4

func (c Any) SetsBySlice(data []V, d time.Duration, handle func(V) (k K, v V))

SetsBySlice Add items to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Any) StopJanitor

func (c Any) StopJanitor()

func (Any) UpdateExpiration

func (c Any) UpdateExpiration(k K, d time.Duration) error

UpdateExpiration. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

type Item

type Item[V any] struct {
	Value      V
	Hit        int   // 命中次数
	Expiration int64 // 过期时间
}

func (Item[V]) Expired

func (item Item[V]) Expired() bool

Expired 判断是否过期

func (Item[V]) IsHit

func (item Item[V]) IsHit() bool

IsHit 判断是否命中过

type Number

type Number[K comparable, V number] struct {
	// contains filtered or unexported fields
}

func NewNumber

func NewNumber[K comparable, V number](defaultExpiration, cleanupInterval time.Duration) *Number[K, V]

func NewNumberFrom

func NewNumberFrom[K comparable, V number](defaultExpiration, cleanupInterval time.Duration, items map[K]Item[V]) *Number[K, V]

func (Number) Add

func (c Number) Add(k K, v V, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (*Number[K, V]) Decrement

func (c *Number[K, V]) Decrement(k K, n V) error

Decrement an item by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to decrement it by n. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementInt64.

func (Number) Delete

func (c Number) Delete(k K)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (Number) DeleteAll added in v0.0.3

func (c Number) DeleteAll()

DeleteAll delete all

func (Number) DeleteBySlice added in v0.0.5

func (c Number) DeleteBySlice(ks []K)

DeleteBySlice an item from the cache. Does nothing if the key is not in the cache.

func (Number) DeleteExpired

func (c Number) DeleteExpired()

DeleteExpired delete all expired items

func (Number) Flush

func (c Number) Flush()

Delete all items from the cache.

func (Number) Get

func (c Number) Get(k K) (V, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Number) GetWithExpiration

func (c Number) GetWithExpiration(k K) (V, time.Time, bool)

GetWithExpiration returns an item and its expiration time from the cache. It returns the item or nil, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was found.

func (Number) GetWithHit

func (c Number) GetWithHit(k K) (V, int, bool)

func (Number) GetWithHitExpiration

func (c Number) GetWithHitExpiration(k K) (V, int, time.Time, bool)

func (Number) GetsByMap added in v0.0.4

func (c Number) GetsByMap(in map[K]V) (map[K]V, bool)

GetsByMap items from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Number) GetsBySlice added in v0.0.4

func (c Number) GetsBySlice(ks []K) ([]V, bool)

GetsBySlice items from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (*Number[K, V]) Increment

func (c *Number[K, V]) Increment(k K, n V) error

Increment an item by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to increment it by n. To retrieve the incremented value, use one of the specialized methods, e.g. IncrementInt64.

func (Number) ItemCount

func (c Number) ItemCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.

func (Number) Items

func (c Number) Items() map[K]Item[V]

Copies all unexpired items in the cache into a new map and returns it.

func (Number) Load

func (c Number) Load(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Number) LoadFile

func (c Number) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Number) OnEvicted

func (c Number) OnEvicted(f func(key K, value V, hit int))

func (Number) Replace

func (c Number) Replace(k K, x V, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (Number) Save

func (c Number) Save(w io.Writer) (err error)

Save Write the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Number) SaveFile

func (c Number) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Number) Set

func (c Number) Set(k K, v V, d time.Duration)

Set Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Number) SetDefault

func (c Number) SetDefault(k K, v V)

SetDefault Add an item to the cache, replacing any existing item, using the default expiration.

func (Number) SetJanitor

func (c Number) SetJanitor(j *janitor)

func (*Number[K, V]) SetMax

func (c *Number[K, V]) SetMax(k K, v V, d time.Duration) error

SetMax Update Value to the maximum value. Create it if it does not exist. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (*Number[K, V]) SetMin

func (c *Number[K, V]) SetMin(k K, v V, d time.Duration) error

SetMin Update Value to the minimum value. Create it if it does not exist. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Number) SetsByMap added in v0.0.4

func (c Number) SetsByMap(data map[K]V, d time.Duration)

SetsByMap Add items to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Number) SetsBySlice added in v0.0.4

func (c Number) SetsBySlice(data []V, d time.Duration, handle func(V) (k K, v V))

SetsBySlice Add items to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Number) StopJanitor

func (c Number) StopJanitor()

func (Number) UpdateExpiration

func (c Number) UpdateExpiration(k K, d time.Duration) error

UpdateExpiration. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (*Number[K, V]) UpdateMax

func (c *Number[K, V]) UpdateMax(k K, v V) error

UpdateMax Update Value to the maximum value.

func (*Number[K, V]) UpdateMin

func (c *Number[K, V]) UpdateMin(k K, v V) error

UpdateMin Update Value to the minimum value.

Jump to

Keyboard shortcuts

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