Documentation
¶
Index ¶
- Variables
- func IsZeroValue(v any) bool
- func Pointer[T any](v T) *T
- func SafeCall(fn func()) (err error)
- type CacheItem
- type CacheItemOptions
- type CacheOptions
- type CacheOptionsFunc
- type Logger
- type MemoryCache
- func (c *MemoryCache[T]) Clear()
- func (c *MemoryCache[T]) Close()
- func (c *MemoryCache[T]) Delete(key string)
- func (c *MemoryCache[T]) Get(key string) (_ *T, ok bool)
- func (c *MemoryCache[T]) Has(key string) (ok bool)
- func (c *MemoryCache[T]) IsEmpty() bool
- func (c *MemoryCache[T]) Iterator() iter.Seq2[string, T]
- func (c *MemoryCache[T]) Keys() []string
- func (c *MemoryCache[T]) Set(key string, value T, optionFunc ...CacheOptionsFunc) (err error)
- func (c *MemoryCache[T]) Size() int
- func (c *MemoryCache[T]) Take(key string) (_ *T, ok bool)
- func (c *MemoryCache[T]) Update(key string, updater func(value T) T, optionFunc ...CacheOptionsFunc) (ok bool)
- func (c *MemoryCache[T]) Upsert(key string, updater func(value *T) *T, optionFunc ...CacheOptionsFunc) (err error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrCacheFull = fmt.Errorf("cache is full") ErrCacheClosed = fmt.Errorf("cache is closed") )
Functions ¶
func IsZeroValue ¶
Types ¶
type CacheItemOptions ¶
type CacheOptions ¶
type CacheOptionsFunc ¶
type CacheOptionsFunc func(options *CacheOptions)
func WithCleanupInterval ¶
func WithCleanupInterval(interval time.Duration) CacheOptionsFunc
Set cleanup interval, only working with new function
func WithExpiration ¶
func WithExpiration(expiration time.Duration) CacheOptionsFunc
Set expiration time, which can be used globally or set separately during Set
func WithLogger ¶
func WithLogger(log Logger) CacheOptionsFunc
Set logger, only working with new function
func WithMaxSize ¶
func WithMaxSize(maxSize int) CacheOptionsFunc
Set cache max size, only working with new function
type MemoryCache ¶
type MemoryCache[T any] struct { // contains filtered or unexported fields }
func NewMemoryCache ¶
func NewMemoryCache[T any](optFuncs ...CacheOptionsFunc) *MemoryCache[T]
NewMemoryCache returns a new MemoryCache. Support options:
- WithLogger
- WithMaxSize
- WithExpiration
- WithCleanupInterval
func (*MemoryCache[T]) Delete ¶
func (c *MemoryCache[T]) Delete(key string)
Delete the value for the given key
func (*MemoryCache[T]) Get ¶
func (c *MemoryCache[T]) Get(key string) (_ *T, ok bool)
Get returns the value for the given key, or nil if not found
func (*MemoryCache[T]) Has ¶
func (c *MemoryCache[T]) Has(key string) (ok bool)
Has returns true if the cache contains the given key
func (*MemoryCache[T]) IsEmpty ¶
func (c *MemoryCache[T]) IsEmpty() bool
IsEmpty returns true if the cache is empty
func (*MemoryCache[T]) Iterator ¶
func (c *MemoryCache[T]) Iterator() iter.Seq2[string, T]
for go range loop
func (*MemoryCache[T]) Keys ¶
func (c *MemoryCache[T]) Keys() []string
Keys returns all the keys in the cache.
func (*MemoryCache[T]) Set ¶
func (c *MemoryCache[T]) Set(key string, value T, optionFunc ...CacheOptionsFunc) (err error)
Set the value for the given key
func (*MemoryCache[T]) Size ¶
func (c *MemoryCache[T]) Size() int
Size returns the number of items in the cache
func (*MemoryCache[T]) Take ¶
func (c *MemoryCache[T]) Take(key string) (_ *T, ok bool)
Take returns the value for the given key, and remove it from cache if it exists
func (*MemoryCache[T]) Update ¶
func (c *MemoryCache[T]) Update(key string, updater func(value T) T, optionFunc ...CacheOptionsFunc) (ok bool)
Update the value for the given key, do nothing if the key does not exist
func (*MemoryCache[T]) Upsert ¶
func (c *MemoryCache[T]) Upsert(key string, updater func(value *T) *T, optionFunc ...CacheOptionsFunc) (err error)
Upsert the value for the given key, create a new value if the key does not exist. Do nothing is updater returns nil