flashdb

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ShardCount uint8 = 128 // default number of shards

)

Variables

View Source
var (
	ErrNotFound     = errors.New("flashdb: key not found")            // not in cache
	ErrBadType      = errors.New("flashdb: value type mismatch")      // type assertion failed
	ErrIllegalField = errors.New("flashdb: illegal or missing field") // e.g. empty key
	ErrInvalidKey   = errors.New("flashdb: invalid/empty key")        // e.g. malformed key
)

Functions

This section is empty.

Types

type Options

type Options struct {
	Shards         uint8         // default 64
	MaxStorageSize uint64        // REQUIRED: per-store budget in bytes (split evenly per shard)
	DefaultTTL     time.Duration // default TTL
	JanitorEvery   time.Duration // default 1m (0 disables)
	LFUDepth       int           // default 4
	LFUWidth       uint64        // default 1<<16
	LFUAgingEvery  uint64        // default 200_000
}

Options for FlashDB. These options control the behavior and limits of the in-memory store.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store satisfies caching.Cache (see compile-time check below).

We shard the map; each shard maintains its own LRU and cost accounting. TinyLFU is shared to keep a global popularity signal (simple and effective).

func New

func New(opts Options) (*Store, error)

New constructs a Store with sane defaults. MaxCostBytes must be > 0.

func (*Store) Append

func (s *Store) Append(key string, appends map[string]string) error

Append appends strings to existing string fields in the record. Returns ErrNotFound if key missing/expired, ErrIllegalField if field absent, ErrBadType if value is not a string.

func (*Store) Close

func (s *Store) Close()

Close stops the janitor.

func (*Store) Decrement

func (s *Store) Decrement(key string, offsets map[string]int64) error

Decrement decreases numeric fields in the record by given offsets. Internally calls Increment with negative values.

func (*Store) Delete

func (s *Store) Delete(key string) (bool, error)

Delete removes a key if it exists. Returns true if the key existed and was removed. Returns false if the key was missing or invalid. Does not error if the key is missing.

func (*Store) Exists

func (s *Store) Exists(key string) (bool, error)

Exists checks if a key exists and is not expired. Does not update recency or frequency. Returns false for missing, expired, or invalid keys.

func (*Store) Get

func (s *Store) Get(key string) (any, error)

Get fetches a value by its (already-composed) key. Lazy-expires, bumps LRU recency, and records TinyLFU access. Returns ErrNotFound for missing or expired keys.

func (*Store) GetTTL

func (s *Store) GetTTL(key string) (int64, error)

GetTTL returns the remaining TTL (in seconds) for key. Returns -1 if the record has no TTL, and ErrNotFound if the key is missing or expired.

func (*Store) Increment

func (s *Store) Increment(key string, offsets map[string]int64) error

Increment increases numeric fields in the record by given offsets. Supported types: int, int32, int64, float64. Returns ErrNotFound if key missing/expired, ErrIllegalField if field absent, ErrBadType if value type is not numeric.

func (*Store) MultiDelete

func (s *Store) MultiDelete(keys []string) (map[string]bool, error)

MultiDelete deletes all provided keys. Returns per-key deletion success. A key is true if it existed and was removed; false if it was missing or invalid.

func (*Store) MultiGet

func (s *Store) MultiGet(keys []string) (map[string]any, error)

MultiGet returns a map of key -> value for all found (and not-expired) keys. Missing or expired keys are silently omitted.

func (*Store) MultiSet

func (s *Store) MultiSet(items map[string]map[string]any, ttl time.Duration) (map[string]bool, error)

MultiSet sets many keys with the same TTL. Returns per-key success. A key is true if it was stored (admitted and within budget); false if rejected (e.g., TinyLFU admission fail) or the key was invalid.

func (*Store) Set

func (s *Store) Set(key string, value map[string]any, ttl time.Duration) (bool, error)

Set stores/updates a value under key with an optional TTL. Returns true if stored, false if rejected by TinyLFU admission. Enforces per-shard byte budget using key+record size.

Notes:

  • ttl==0 uses s.opts.DefaultTTL (if > 0). Adjust unit mapping as per your option.
  • Uses your deep-size estimator; we add a small record overhead to approximate metadata.
  • All mutations happen under the shard write lock.

func (*Store) SetTTL

func (s *Store) SetTTL(key string, ttl time.Duration) error

SetTTL updates the TTL for key. ttl<=0 clears TTL. Returns ErrNotFound if the key is missing or expired.

Jump to

Keyboard shortcuts

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