timecache

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FirstSeenCache

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

FirstSeenCache 是一个时间缓存,仅在消息首次添加时标记其过期时间。

func (*FirstSeenCache) Add

func (tc *FirstSeenCache) Add(s string) bool

Add 将消息添加到缓存中 参数: - s: 消息字符串 返回值: - bool: 是否成功添加消息

func (*FirstSeenCache) Done

func (tc *FirstSeenCache) Done()

Done 停止后台清理协程

func (*FirstSeenCache) Has

func (tc *FirstSeenCache) Has(s string) bool

Has 检查消息是否存在于缓存中 参数: - s: 消息字符串 返回值: - bool: 消息是否存在

type LastSeenCache

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

LastSeenCache 是一个时间缓存,在添加或检查消息存在时,延长其过期时间。

func (*LastSeenCache) Add

func (tc *LastSeenCache) Add(s string) bool

Add 将消息添加到缓存中,并延长其过期时间。 参数: - s: 消息字符串 返回值: - bool: 是否成功添加消息(如果消息已存在,返回 false)

func (*LastSeenCache) Done

func (tc *LastSeenCache) Done()

Done 停止后台清理协程

func (*LastSeenCache) Has

func (tc *LastSeenCache) Has(s string) bool

Has 检查消息是否存在于缓存中,并延长其过期时间。 参数: - s: 消息字符串 返回值: - bool: 消息是否存在

type Strategy

type Strategy uint8

Strategy 表示 TimeCache 过期策略

const (
	// Strategy_FirstSeen 表示从添加条目时开始计算过期时间
	Strategy_FirstSeen Strategy = iota
	// Strategy_LastSeen 表示从上次添加或检查条目时开始计算过期时间
	Strategy_LastSeen
)

type TimeCache

type TimeCache interface {
	// Add 将 id 添加到缓存中,如果该 id 不存在
	// 如果 id 是新添加的,返回 true
	// 根据实现策略,可能会或不会更新现有条目的过期时间
	Add(string) bool
	// Has 检查缓存中是否存在 id
	// 根据实现策略,可能会或不会更新现有条目的过期时间
	Has(string) bool
	// Done 表示用户不再使用此缓存,可以停止后台线程并释放资源
	Done()
}

TimeCache 是一个最近看到的消息(通过 id)的缓存接口

func NewTimeCache

func NewTimeCache(ttl time.Duration) TimeCache

NewTimeCache 创建一个默认的 ("first seen") 缓存实现 参数: - ttl: 条目的存活时间 返回值: - TimeCache: 一个实现了 TimeCache 接口的缓存实例

func NewTimeCacheWithStrategy

func NewTimeCacheWithStrategy(strategy Strategy, ttl time.Duration) TimeCache

NewTimeCacheWithStrategy 根据指定的策略创建一个 TimeCache 实例 参数: - strategy: 使用的过期策略 - ttl: 条目的存活时间 返回值: - TimeCache: 一个实现了 TimeCache 接口的缓存实例

Jump to

Keyboard shortcuts

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