cachex

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

Cachex

一个简单的缓存库。

读取

数据查询时会先命中第一层缓存,命中失败会从二级缓存等中读取至缓存中

写入

数据会先写入持久化存储再自动存储至第一层缓存中

支持的缓存类型

内存存储

Lru算法

Lru是一种内存数据淘汰策略,最近最少被使用的数据最先被淘汰。使用cachex.NewMemLruCacheStroage创建Lru内存缓存存储。

Lfu算法

Lfu是一种内存数据淘汰策略,使用频率最低的数据最先被淘汰。使用cachex.NewMemLfuCacheStroage创建Lfu内存缓存存储。

Fifo算法

Fifo是一种内存数据淘汰策略,最先加入缓存的数据最先被淘汰。使用cachex.NewMemFifoCacheStroage创建Fifo内存缓存存储。

持久化存储

Redis

使用Redis进行存储,使用cachex.NewRedisCacheStroage创建Redis缓存存储。

LevelDB

使用LevelDB进行存储,使用cachex.NewLevelDBCacheStroage创建Redis缓存存储。

使用方法

example

package main

import (
	"fmt"

	"github.com/lfhy/cachex"
)

// 内存缓存使用lru
// 远程缓存使用redis
func main() {
	// 初始化缓存配置
	lruCache := cachex.NewMemLruCacheStroage(30000)
	redisCache := cachex.NewRedisCacheStroage(&cachex.RedisConfig{
		Addr:     "192.168.188.230:6379",
		Password: "password",
		DB:       4,
	}, 0)
	// 建立缓存
	cache := cachex.NewCache(lruCache, redisCache)
	// 设置缓存
	cache.Set("hello", "world")
	// 读取缓存
	value, ok := cache.Get("hello")
	if ok {
		fmt.Printf("第一次从lru读取: %v\n", value)
	} else {
		fmt.Println("读取失败")
	}
	// Lru将缓存删除
	lruCache.Delete("hello")
	// 重新读取缓存
	value, ok = cache.Get("hello")
	if ok {
		fmt.Printf("第二次从redis读: %v\n", value)
	} else {
		fmt.Println("读取失败")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal added in v0.0.8

func Marshal(val interface{}) ([]byte, bool)

func MarshalString added in v0.0.8

func MarshalString(val interface{}) (string, bool)

func Unmarshal added in v0.0.8

func Unmarshal(buf []byte, val interface{}) bool

func UnmarshalString added in v0.0.8

func UnmarshalString(buf string, val interface{}) bool

Types

type Cache

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

func NewCache

func NewCache(defaultcache CacheStroage[any], remotecache ...CacheStroage[any]) *Cache[any]

默认创any类型的存储

func NewCacheWithType

func NewCacheWithType[T any](defaultcache CacheStroage[T], remotecache ...CacheStroage[T]) *Cache[T]

带有类型的存储

func (*Cache[T]) Close added in v0.0.6

func (c *Cache[T]) Close()

关闭缓存

func (*Cache[T]) Create added in v0.0.4

func (c *Cache[T]) Create(key string, value T)

设置缓存别名

func (*Cache[T]) Delete

func (c *Cache[T]) Delete(key string)

删除缓存

func (*Cache[T]) Free

func (c *Cache[T]) Free()

清空缓存

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, bool)

获取缓存

func (*Cache[T]) GetOrSet

func (c *Cache[T]) GetOrSet(key string, value T) (oldValue T, isLoad bool)

获取失败时则设置缓存为指定值

func (*Cache[T]) GetOrSetFunc

func (c *Cache[T]) GetOrSetFunc(key string, setFn func() (value T, canSet bool)) (oldValue T, isLoad bool)

获取失败时则通过指定函数设置为指定值

func (*Cache[T]) Save added in v0.0.4

func (c *Cache[T]) Save(key string, value T)

设置缓存别名

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, value T)

设置缓存

func (*Cache[T]) SetFunc added in v0.0.4

func (c *Cache[T]) SetFunc(key string, fn func() (value T, canSet bool))

通过函数取值 取值后设置缓存

func (*Cache[T]) Swap

func (c *Cache[T]) Swap(key string, value T) (oldValue T, isLoad bool)

获取缓存并设置为另一个值

type CacheStroage

type CacheStroage[T any] interface {
	// 获取缓存
	Get(key string) (T, bool)
	// 设置缓存
	Set(key string, value T)
	// 删除缓存
	Delete(key string)
	// 清空缓存
	Free()
	// 关闭
	Close()
	// 遍历
	Range(fn func(key string, value T) (Continue bool))
}

type CachexGorm added in v0.0.8

type CachexGorm[T any] struct {
	// contains filtered or unexported fields
}

func NewGormCachex added in v0.0.8

func NewGormCachex[T any](db *gorm.DB, tableName ...string) *CachexGorm[T]

func NewSqliteCache added in v0.0.8

func NewSqliteCache[T any](dbPath string, tableName ...string) (*CachexGorm[T], error)

func (*CachexGorm[T]) Close added in v0.0.8

func (c *CachexGorm[T]) Close()

func (*CachexGorm[T]) Delete added in v0.0.8

func (c *CachexGorm[T]) Delete(key string)

func (*CachexGorm[T]) Free added in v0.0.8

func (c *CachexGorm[T]) Free()

func (*CachexGorm[T]) Get added in v0.0.8

func (c *CachexGorm[T]) Get(key string) (T, bool)

func (*CachexGorm[T]) GetDBInterface added in v0.0.8

func (c *CachexGorm[T]) GetDBInterface() *gorm.DB

func (*CachexGorm[T]) NewModel added in v0.0.8

func (c *CachexGorm[T]) NewModel() CachexGormModel

func (*CachexGorm[T]) Range added in v0.0.8

func (c *CachexGorm[T]) Range(f func(key string, value T) bool)

func (*CachexGorm[T]) Set added in v0.0.8

func (c *CachexGorm[T]) Set(key string, value T)

type CachexGormModel added in v0.0.8

type CachexGormModel struct {
	ID    int64  `gorm:"id"`
	Key   string `gorm:"key"`
	Value string `gorm:"value"`
	// contains filtered or unexported fields
}

func (CachexGormModel) TableName added in v0.0.8

func (c CachexGormModel) TableName() string

type Data added in v0.0.4

type Data[T any] struct {
	// contains filtered or unexported fields
}

type Eviction added in v0.0.3

type Eviction[T any] struct {
	Key   string
	Value T
}

type FifoCache added in v0.0.4

type FifoCache[T any] struct {
	// contains filtered or unexported fields
}

func NewMemFifoCacheStroage added in v0.0.4

func NewMemFifoCacheStroage(cacheSize ...int) *FifoCache[any]

Fifo最近最先加入缓存的数据最先被淘汰

func NewMemFifoCacheStroageWithType added in v0.0.4

func NewMemFifoCacheStroageWithType[T any](cacheSize ...int) *FifoCache[T]

Fifo最近最先加入缓存的数据最先被淘汰

func (FifoCache) Close added in v0.0.6

func (FifoCache) Close()

func (*FifoCache[T]) Delete added in v0.0.4

func (c *FifoCache[T]) Delete(key string)

删除缓存

func (*FifoCache[T]) Free added in v0.0.4

func (c *FifoCache[T]) Free()

清空缓存

func (*FifoCache[T]) Get added in v0.0.4

func (c *FifoCache[T]) Get(key string) (T, bool)

获取缓存

func (*FifoCache[T]) Range added in v0.0.8

func (c *FifoCache[T]) Range(f func(key string, value T) bool)

遍历缓存

func (*FifoCache[T]) Set added in v0.0.4

func (c *FifoCache[T]) Set(key string, value T)

设置缓存

type JsonByteData

type JsonByteData[T any] struct {
	Data T `json:"Data"`
}

type LeveldbCache added in v0.0.3

type LeveldbCache[T any] struct {
	// contains filtered or unexported fields
}

func NewLevelDBCacheStroage added in v0.0.3

func NewLevelDBCacheStroage(dbpath string) (*LeveldbCache[any], error)

Leveldb最近最少被使用的数据最先被淘汰

func NewLevelDBCacheStroageWithType added in v0.0.3

func NewLevelDBCacheStroageWithType[T any](dbpath string) (*LeveldbCache[T], error)

Leveldb最近最少被使用的数据最先被淘汰

func (*LeveldbCache[T]) Close added in v0.0.6

func (c *LeveldbCache[T]) Close()

获取DB实例对象

func (*LeveldbCache[T]) Delete added in v0.0.3

func (c *LeveldbCache[T]) Delete(key string)

删除缓存

func (*LeveldbCache[T]) Free added in v0.0.3

func (c *LeveldbCache[T]) Free()

清空缓存

func (*LeveldbCache[T]) Get added in v0.0.3

func (c *LeveldbCache[T]) Get(key string) (T, bool)

获取缓存

func (*LeveldbCache[T]) GetDBInterface added in v0.0.5

func (c *LeveldbCache[T]) GetDBInterface() *leveldb.DB

获取DB实例对象

func (*LeveldbCache[T]) Range added in v0.0.8

func (c *LeveldbCache[T]) Range(f func(key string, value T) bool)

遍历数据

func (*LeveldbCache[T]) Set added in v0.0.3

func (c *LeveldbCache[T]) Set(key string, value T)

设置缓存

func (*LeveldbCache[T]) Unmarshal added in v0.0.8

func (c *LeveldbCache[T]) Unmarshal(data []byte) (T, bool)

反序化数据

type LfuCache added in v0.0.3

type LfuCache[T any] struct {
	UpperBound int
	LowerBound int

	EvictionChannel chan<- Eviction[T]
	// contains filtered or unexported fields
}

func NewMemLfuCacheStroage added in v0.0.3

func NewMemLfuCacheStroage() *LfuCache[any]

Lfu使用频率最低的数据最先被淘汰

func NewMemLfuCacheStroageWithType added in v0.0.3

func NewMemLfuCacheStroageWithType[T any]() *LfuCache[T]

Lfu使用频率最低的数据最先被淘汰

func (*LfuCache[T]) Close added in v0.0.6

func (c *LfuCache[T]) Close()

关闭缓存

func (*LfuCache[T]) Delete added in v0.0.3

func (c *LfuCache[T]) Delete(key string)

删除缓存

func (*LfuCache[T]) Free added in v0.0.3

func (c *LfuCache[T]) Free()

清空缓存

func (*LfuCache[T]) Get added in v0.0.3

func (c *LfuCache[T]) Get(key string) (T, bool)

获取缓存

func (*LfuCache[T]) Range added in v0.0.8

func (c *LfuCache[T]) Range(f func(key string, value T) bool)

遍历数据

func (*LfuCache[T]) Set added in v0.0.3

func (c *LfuCache[T]) Set(key string, value T)

设置缓存

type LruCache

type LruCache[T any] struct {
	// contains filtered or unexported fields
}

func NewMemLruCacheStroage

func NewMemLruCacheStroage(cacheSize ...int) *LruCache[any]

Lru最近最少被使用的数据最先被淘汰

func NewMemLruCacheStroageWithType

func NewMemLruCacheStroageWithType[T any](cacheSize ...int) *LruCache[T]

Lru最近最少被使用的数据最先被淘汰

func (LruCache) Close added in v0.0.6

func (LruCache) Close()

func (*LruCache[T]) Delete

func (c *LruCache[T]) Delete(key string)

删除缓存

func (*LruCache[T]) Free

func (c *LruCache[T]) Free()

清空缓存

func (*LruCache[T]) Get

func (c *LruCache[T]) Get(key string) (T, bool)

获取缓存

func (*LruCache[T]) Range added in v0.0.8

func (c *LruCache[T]) Range(f func(key string, value T) bool)

遍历数据

func (*LruCache[T]) Set

func (c *LruCache[T]) Set(key string, value T)

设置缓存

type RedisCache

type RedisCache[T any] struct {
	// contains filtered or unexported fields
}

func NewRedisCacheStroage

func NewRedisCacheStroage(config *RedisConfig, timeout ...time.Duration) *RedisCache[any]

Redis存储 传入连接参数和缓存超时时间

func NewRedisCacheStroageWithType

func NewRedisCacheStroageWithType[T any](config *RedisConfig, timeout ...time.Duration) *RedisCache[T]

Redis存储 传入连接参数和缓存超时时间

func (*RedisCache[T]) Close added in v0.0.6

func (c *RedisCache[T]) Close()

func (*RedisCache[T]) Delete

func (c *RedisCache[T]) Delete(key string)

删除缓存

func (*RedisCache[T]) Free

func (c *RedisCache[T]) Free()

清空缓存

func (*RedisCache[T]) Get

func (c *RedisCache[T]) Get(key string) (T, bool)

获取缓存

func (*RedisCache[T]) GetDBInterface added in v0.0.5

func (c *RedisCache[T]) GetDBInterface() *redis.Client

获取DB实例对象

func (*RedisCache[T]) Range added in v0.0.8

func (c *RedisCache[T]) Range(f func(key string, value T) bool)

遍历数据

func (*RedisCache[T]) Set

func (c *RedisCache[T]) Set(key string, value T)

设置缓存

type RedisConfig

type RedisConfig = redis.Options

type SliceMap added in v0.0.4

type SliceMap[T any] struct {
	// contains filtered or unexported fields
}

func (SliceMap) Close added in v0.0.6

func (SliceMap) Close()

func (*SliceMap[T]) Delete added in v0.0.4

func (m *SliceMap[T]) Delete(key string)

func (*SliceMap[T]) Get added in v0.0.4

func (m *SliceMap[T]) Get(key string) (T, bool)

func (*SliceMap[T]) Len added in v0.0.4

func (m *SliceMap[T]) Len() int

func (*SliceMap[T]) Pop added in v0.0.4

func (m *SliceMap[T]) Pop() (T, bool)

获取第一个元素 并删除

func (*SliceMap[T]) Range added in v0.0.8

func (m *SliceMap[T]) Range(fn func(key string, value T) (Continue bool))

func (*SliceMap[T]) Set added in v0.0.4

func (m *SliceMap[T]) Set(key string, value T)

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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