cachex

package
v0.0.0-...-270bddf Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 2 Imported by: 0

README

Cachex

缓存处理库,支持多种缓存实现,包括内存缓存,Redis 缓存等

使用

import (
	"github.com/yu1ec/go-pkg/cachex"
	_ "github.com/yu1ec/go-pkg/cachex/driver/memory"  // 导入 memory 驱动
	// _ "github.com/yu1ec/go-pkg/cachex/driver/redis"  // 导入 redis 驱动(需要实现)
	// _ "github.com/yu1ec/go-pkg/cachex/driver/mysql"  // 导入 mysql 驱动(需要实现)
)

func main() {
	// 使用 memory 驱动的 gocache 实现
	memoryCache, err := cachex.New("memory", map[string]any{
		"implementation": "gocache",
		"CleanupInterval": 10 * time.Minute,
	})
	if err != nil {
		// 处理错误
	}

	// 使用 memoryCache...

	// 使用 redis 驱动(假设已实现)
	redisCache, err := cachex.New("redis", map[string]any{
		"address": "localhost:6379",
		"password": "",
		"db": 0,
	})
	if err != nil {
		// 处理错误
	}

	// 使用 redisCache...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get 从缓存中获取一个项目。返回该项或 nil,以及一个指示是否找到该键的布尔值。
	Get(k string) (any, bool)
	// Put 添加/替换现有的缓存设置,包括过期时间,如果过期时间是0,则使用默认过期时间,如果为-1则表示永不过期 单位/秒
	Put(k string, value any, expireSeconds int64)
	// Exists 检查给定的键是否存在于缓存中。
	Exists(k string) bool
	// Remember 如果缓存中不存在该键,则从 create 函数创建一个新值,并将其添加到缓存中。单位/秒
	Remember(k string, expireSeconds int64, create func() (any, error)) (any, error)
	// RememberForever 如果缓存中不存在该键,则从 create 函数创建一个新值,并将其添加到缓存中。
	RememberForever(key string, create func() (any, error)) (any, error)
	// Forget 删除给定的键。
	Forget(key string)
	// Flush 清空缓存
	Flush()
}

func New

func New(driverName string, config any) (Cache, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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