ephemeral

package
v0.3.7 Latest Latest
Warning

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

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

Documentation

Overview

Package ephemeral 提供短期 TTL KV:不版本化、不持久,纯内存 + 到点自动过期。

与 pkg/domain/storage 互补:storage 是版本化 KV + OCC(重,用于存档/配置), 本包是轻量过期缓存(用于匹配房间临时数据 / 短期 token 缓存 / 排行榜快照 / 验证码)。

ephemeral storage 语义:

  • Set(key, value, ttl):ttl 到点自动过期,Get 返回 (value, ok);
  • 底层 map + 单 goroutine 定时清扫(参考 pkg/token 的 gc 模式);
  • 并发安全(sync.RWMutex + channel-driven gc)。

零值不可用,用 New 构造。Store 并发安全,Stop 后清扫 goroutine 退出。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*config)

Option 配置 Store。

type Store

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

Store 短期 TTL KV 存储。

func New

func New(opts ...Option) *Store

New 创建 Store 并启动清扫 goroutine(每分钟扫一次过期条目)。

func (*Store) Delete

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

Delete 立即删除 key。返回是否原本存在(且未过期)。

func (*Store) Get

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

Get 读取 key,返回 (value, ok)。已过期返回 (nil, false) 并惰性删除。

func (*Store) Len

func (s *Store) Len() int

Len 当前条目数(含可能未扫到的过期条目)。

func (*Store) Set

func (s *Store) Set(key string, value any, ttl time.Duration)

Set 写入 key=value,ttl 后自动过期。ttl<=0 表示立即过期(不存)。

func (*Store) Stop

func (s *Store) Stop()

Stop 停止清扫 goroutine。幂等。

Jump to

Keyboard shortcuts

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