Help us shape the future of Go! Take the 2026 Go Developer Survey

memkv

package
v0.32.41 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package memkv 进程内字符串 TTL KV(主包仅依赖标准库与 oekit/concurrency/async,后者为纯标准库实现的外部模块):单机内存模式的共享存储原语。

服务对象:Redis 消费方(anonymous/presign 等持窄接口的域)在 redis.host 为空 时的等价兜底——取件码映射、直传会话这类短 TTL 数据。语义对齐 Redis 最小子集: Get / Set / SetNX(原子)/ Del + TTL 过期;不做持久化、不跨进程。

防御对齐 lockout 内存兜底先例(2026-10-05 审计 P2):容量上限 + 周期清扫 + 满载守卫——仅靠惰性清理时,攻击者用海量唯一键(取件码枚举/负缓存标记)可把 表撑到无界。

消费方 ≥3 处后可评估上收 kit(当前 anonymous/presign 两个适配层复用本包)。

Index

Constants

View Source
const (
	// DefaultMax 单表容量上限。满载先清扫过期,仍满则整表重置(保命优先于
	// 保留数据:满载意味着已被海量唯一键刷过,重置的代价是缓存/标记失效,
	// 重回源路径兜底,不放大业务影响)。
	DefaultMax = 65536
	// DefaultGCPeriod 周期清扫间隔。
	DefaultGCPeriod = 10 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store 字符串 TTL KV。并发安全;清扫协程随首次写入惰性启动(只读不启)。

func New

func New() *Store

New 创建 Store(默认容量与清扫周期)。

func (*Store) Del

func (s *Store) Del(keys ...string) int

Del 删除若干键,返回实际删除数(同 Redis DEL)。

func (*Store) Get

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

Get 返回 (值, 是否命中)。过期条目惰性删除。

func (*Store) Len

func (s *Store) Len() int

Len 返回当前条目数(含未过期;测试用)。

func (*Store) Set

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

Set 写入(覆盖语义,同 Redis SET)。

func (*Store) SetNX

func (s *Store) SetNX(key, val string, ttl time.Duration) bool

SetNX 不存在才写入,返回是否写入成功(原子,同 Redis SET NX)。 已有过期条目视为不存在(写入前失效)。

func (*Store) WithClock

func (s *Store) WithClock(now func() time.Time) *Store

WithClock 注入时钟(测试用)。

Directories

Path Synopsis
Package redisshim 单机内存模式下 go-redis 命令面的等价实现(memkv.Store 的 redis 兼容垫片):redis.host 为空时 anonymous/presign 等域的 NewService 以此 兜底,取件码映射/直传会话等短 TTL 数据存进程内——不跨进程共享、重启即失, DB 仍是唯一真相源。
Package redisshim 单机内存模式下 go-redis 命令面的等价实现(memkv.Store 的 redis 兼容垫片):redis.host 为空时 anonymous/presign 等域的 NewService 以此 兜底,取件码映射/直传会话等短 TTL 数据存进程内——不跨进程共享、重启即失, DB 仍是唯一真相源。

Jump to

Keyboard shortcuts

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