cache

package
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

mmap_common.go

Index

Constants

View Source
const (
	DefaultSize = 1 << 20 // 1MB
	THRESHOLD   = 0.8     // 80%使用率触发扩容
)

Variables

View Source
var (
	ErrInvalidFile   = fmt.Errorf("invalid cache file")
	ErrInvalidAccess = fmt.Errorf("invalid memory access")
	ErrChecksum      = fmt.Errorf("data checksum mismatch")
	ErrOutOfSpace    = fmt.Errorf("out of space")
)

Functions

This section is empty.

Types

type Cache added in v0.0.11

type Cache[E any] struct {
	// contains filtered or unexported fields
}

Cache 使用内存映射的跨进程安全缓存

func OpenCache added in v0.0.11

func OpenCache[E any](name string) (*Cache[E], error)

OpenCache 创建或打开内存映射缓存

func (*Cache[E]) Add added in v0.0.11

func (c *Cache[E]) Add(delta int) error

func (*Cache[E]) Close added in v0.0.11

func (c *Cache[E]) Close() error

Close 安全关闭

func (*Cache[E]) ToSlice added in v0.0.12

func (c *Cache[E]) ToSlice() ([]E, error)

ToSlice 安全转换为类型切片

func (*Cache[E]) WriteData added in v0.0.11

func (c *Cache[E]) WriteData(offset uint32, src []byte) error

WriteData 类型安全写入

type MemObject added in v0.0.11

type MemObject interface {
	Flush() error
	Unmap() error
	Bytes() []byte
}

MemObject 封装内存映射操作接口

type Pool

type Pool[E any] struct {
	// contains filtered or unexported fields
}

Pool 是一个泛型化的 sync.Pool 封装,用于缓存和复用对象。 对象在释放回池时会被重置为零值。

func (*Pool[E]) Acquire

func (p *Pool[E]) Acquire() *E

Acquire 从池中获取一个对象。如果池为空则创建新对象。 返回的对象会被重置为零值(通过 Release 时的重置保证)。

func (*Pool[E]) Release

func (p *Pool[E]) Release(obj *E)

Release 将对象放回池中。如果对象为 nil 则直接返回。 放回前会将对象重置为零值以保证下次获取时的状态。

Jump to

Keyboard shortcuts

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