Documentation
¶
Overview ¶
Package sfcache provides a high-performance cache with S3-FIFO eviction and optional persistence.
Index ¶
- type MemoryCache
- type Option
- type TieredCache
- func (c *TieredCache[K, V]) Close() error
- func (c *TieredCache[K, V]) Delete(ctx context.Context, key K) error
- func (c *TieredCache[K, V]) Flush(ctx context.Context) (int, error)
- func (c *TieredCache[K, V]) Get(ctx context.Context, key K) (V, bool, error)
- func (c *TieredCache[K, V]) Len() int
- func (c *TieredCache[K, V]) Set(ctx context.Context, key K, value V, ttl ...time.Duration) error
- func (c *TieredCache[K, V]) SetAsync(ctx context.Context, key K, value V, ttl ...time.Duration) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MemoryCache ¶
type MemoryCache[K comparable, V any] struct { // contains filtered or unexported fields }
MemoryCache is a fast in-memory cache without persistence. All operations are context-free and never return errors.
func New ¶ added in v1.1.0
func New[K comparable, V any](opts ...Option) *MemoryCache[K, V]
New creates a new in-memory cache.
Example:
cache := sfcache.New[string, User](
sfcache.Size(10000),
sfcache.TTL(time.Hour),
)
defer cache.Close()
cache.Set("user:123", user) // uses default TTL
cache.Set("user:123", user, time.Hour) // explicit TTL
user, ok := cache.Get("user:123")
func (*MemoryCache[K, V]) Close ¶
func (*MemoryCache[K, V]) Close()
Close releases resources held by the cache. For MemoryCache this is a no-op, but provided for API consistency.
func (*MemoryCache[K, V]) Delete ¶
func (c *MemoryCache[K, V]) Delete(key K)
Delete removes a value from the cache.
func (*MemoryCache[K, V]) Flush ¶
func (c *MemoryCache[K, V]) Flush() int
Flush removes all entries from the cache. Returns the number of entries removed.
func (*MemoryCache[K, V]) Get ¶
func (c *MemoryCache[K, V]) Get(key K) (V, bool)
Get retrieves a value from the cache. Returns the value and true if found, or the zero value and false if not found.
func (*MemoryCache[K, V]) Len ¶
func (c *MemoryCache[K, V]) Len() int
Len returns the number of entries in the cache.
func (*MemoryCache[K, V]) Set ¶
func (c *MemoryCache[K, V]) Set(key K, value V, ttl ...time.Duration)
Set stores a value in the cache. If no TTL is provided, the default TTL is used. If no default TTL is configured, the entry never expires.
type Option ¶
type Option func(*config)
Option configures a MemoryCache or TieredCache.
type TieredCache ¶ added in v1.1.0
type TieredCache[K comparable, V any] struct { // Store provides direct access to the persistence layer. // Use this for persistence-specific operations: // cache.Store.Len(ctx) // cache.Store.Flush(ctx) // cache.Store.Cleanup(ctx, maxAge) Store persist.Store[K, V] // contains filtered or unexported fields }
TieredCache is a cache with an in-memory layer backed by persistent storage. The memory layer provides fast access, while the store provides durability. Core operations require context for I/O, while memory operations like Len() do not.
func NewTiered ¶ added in v1.1.0
func NewTiered[K comparable, V any](store persist.Store[K, V], opts ...Option) (*TieredCache[K, V], error)
NewTiered creates a cache with an in-memory layer backed by persistent storage.
Example:
store, _ := localfs.New[string, User]("myapp", "")
cache, err := sfcache.NewTiered[string, User](store,
sfcache.Size(10000),
sfcache.TTL(time.Hour),
)
if err != nil {
return err
}
defer cache.Close()
cache.Set(ctx, "user:123", user) // uses default TTL
cache.Set(ctx, "user:123", user, time.Hour) // explicit TTL
user, ok, err := cache.Get(ctx, "user:123")
storeCount, _ := cache.Store.Len(ctx)
func (*TieredCache[K, V]) Close ¶ added in v1.1.0
func (c *TieredCache[K, V]) Close() error
Close releases resources held by the cache.
func (*TieredCache[K, V]) Delete ¶ added in v1.1.0
func (c *TieredCache[K, V]) Delete(ctx context.Context, key K) error
Delete removes a value from the cache. The value is always removed from memory. Returns an error if persistence deletion fails.
func (*TieredCache[K, V]) Flush ¶ added in v1.1.0
func (c *TieredCache[K, V]) Flush(ctx context.Context) (int, error)
Flush removes all entries from the cache, including persistent storage. Returns the total number of entries removed from memory and persistence.
func (*TieredCache[K, V]) Get ¶ added in v1.1.0
func (c *TieredCache[K, V]) Get(ctx context.Context, key K) (V, bool, error)
Get retrieves a value from the cache. It first checks the memory cache, then falls back to persistence.
func (*TieredCache[K, V]) Len ¶ added in v1.1.0
func (c *TieredCache[K, V]) Len() int
Len returns the number of entries in the memory cache. For persistence entry count, use cache.Store.Len(ctx).
func (*TieredCache[K, V]) Set ¶ added in v1.1.0
Set stores a value in the cache. If no TTL is provided, the default TTL is used. The value is ALWAYS stored in memory, even if persistence fails. Returns an error if the key violates persistence constraints or if persistence fails.
func (*TieredCache[K, V]) SetAsync ¶ added in v1.1.0
func (c *TieredCache[K, V]) SetAsync(ctx context.Context, key K, value V, ttl ...time.Duration) error
SetAsync stores a value in the cache, handling persistence asynchronously. If no TTL is provided, the default TTL is used. Key validation and in-memory caching happen synchronously. Persistence errors are logged but not returned (fire-and-forget). Returns an error only for validation failures (e.g., invalid key format).
Directories
¶
| Path | Synopsis |
|---|---|
|
pkg
|
|
|
persist
module
|
|
|
persist/cloudrun
module
|
|
|
persist/datastore
module
|
|
|
persist/localfs
module
|
|
|
store/cloudrun
module
|
|
|
store/compress
module
|
|
|
store/datastore
module
|
|
|
store/localfs
module
|
|
|
store/null
module
|