cache

package
v0.0.0-...-0660aa1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeFromHex

func DecodeFromHex[T any](encodedStr string) (value T, err error)

DecodeFromHex 使用 Gob 反序列化为原始类型

func EncodeToHex

func EncodeToHex(value any) (string, error)

Types

type Cache

type Cache[K comparable, V any] interface {
	// Set a value in the cache if the key does not already exist. If
	// timeout is given, use that timeout for the key;
	Set(key K, value V, ttl time.Duration) error

	// Get a given key from the cache.
	Get(key K) (value V, ok bool)

	// GetDefault a given key from the cache. If the key does not exist, return
	// default, which itself defaults to nil.
	GetDefault(key K, _default V) V

	// GetOrSet fetch a given key from the cache. If the key does not exist,
	// add the key and set it to the new value. If ttl is given, use that
	// timeout for the key.
	// The value result is the value for the existing key or the given new value.
	// The loaded result is true if the value was loaded, false if stored.
	// The err result reports error if setting a new val fails.
	GetOrSet(key K, newVal V, ttl time.Duration) (V, bool, error)

	// HasKey returns True if the key is in the cache and has not expired.
	HasKey(key K) bool

	// Keys returns a slice of all keys in the cache in order of usage.
	// note: Not all keys are exist because they may have expired and not yet been cleaned up.
	Keys() []K

	// Delete a key from the cache and return whether it succeeded, failing
	// silently.
	Delete(key K)

	// Clear *all* values from the cache at once.
	Clear()
}

type FileCache

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

FileCache represents the file-based shard cache system

func NewFileCache

func NewFileCache[K comparable, V any](numShards int, baseDir string, cleanupInterval time.Duration) (*FileCache[K, V], error)

NewFileCache initializes a new sharded file-based cache.

func (*FileCache[K, V]) Clear

func (c *FileCache[K, V]) Clear()

func (*FileCache[K, V]) Delete

func (c *FileCache[K, V]) Delete(key K)

func (*FileCache[K, V]) Get

func (c *FileCache[K, V]) Get(key K) (value V, ok bool)

Get retrieves a value from the file-based cache

func (*FileCache[K, V]) GetDefault

func (c *FileCache[K, V]) GetDefault(key K, _default V) V

func (*FileCache[K, V]) GetOrSet

func (c *FileCache[K, V]) GetOrSet(key K, newVal V, ttl time.Duration) (V, bool, error)

func (*FileCache[K, V]) HasKey

func (c *FileCache[K, V]) HasKey(key K) bool

func (*FileCache[K, V]) Keys

func (c *FileCache[K, V]) Keys() []K

func (*FileCache[K, V]) Set

func (c *FileCache[K, V]) Set(key K, value V, ttl time.Duration) error

func (*FileCache[K, V]) Stop

func (c *FileCache[K, V]) Stop()

type FileCacheShard

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

FileCacheShard represents a single shard of the cache

type FileNode

type FileNode[V any] struct {
	Value      V
	Expiration int64
}

func (*FileNode[V]) IsExpired

func (node *FileNode[V]) IsExpired() bool

IsExpired check whether it has expired.

type MemCache

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

MemCache is a simple lru cache.

func NewMemCache

func NewMemCache[K comparable, V any](numShards, capacity int, cleanupInterval time.Duration) *MemCache[K, V]

func (*MemCache[K, V]) Clear

func (c *MemCache[K, V]) Clear()

func (*MemCache[K, V]) Delete

func (c *MemCache[K, V]) Delete(key K)

Delete removes a specific key from the cache.

func (*MemCache[K, V]) Get

func (c *MemCache[K, V]) Get(key K) (value V, ok bool)

func (*MemCache[K, V]) GetDefault

func (c *MemCache[K, V]) GetDefault(key K, _default V) V

func (*MemCache[K, V]) GetOrSet

func (c *MemCache[K, V]) GetOrSet(key K, newVal V, ttl time.Duration) (V, bool, error)

func (*MemCache[K, V]) HasKey

func (c *MemCache[K, V]) HasKey(key K) bool

func (*MemCache[K, V]) Keys

func (c *MemCache[K, V]) Keys() []K

Keys returns a slice of all keys in the cache.

func (*MemCache[K, V]) Set

func (c *MemCache[K, V]) Set(key K, value V, ttl time.Duration) error

func (*MemCache[K, V]) Stop

func (c *MemCache[K, V]) Stop()

type MemCacheNode

type MemCacheNode[K comparable, V any] struct {
	Key        K
	Value      V
	Expiration int64
	// contains filtered or unexported fields
}

MemCacheNode is a doubly linked list

func (*MemCacheNode[K, V]) IsExpired

func (node *MemCacheNode[K, V]) IsExpired() bool

IsExpired check whether it has expired.

type MemCacheShard

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

Jump to

Keyboard shortcuts

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