asyncdb

package
v1.4.4-alpha1202-loadi... Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DatabaseCacheItemNormal databaseCacheItemStatus = iota // save to database before  expired
	DatabaseCacheItemSave                                  // expired, save to database and delete(default) or update(Update-status)
	DatabaseCacheItemUpdate                                // someone peek the instruction when save, update the instruction after save
	DatabaseCacheItemNotFound
)

Variables

This section is empty.

Functions

func GetDatabaseCacheStatus

func GetDatabaseCacheStatus[K comparable, T any](c *DataBaseCacheWithKey[K, T], key K) databaseCacheItemStatus

func NewConfig

func NewConfig(opts ...Option) *config

Types

type Cache

type Cache[T MemoryItem, D DBItem] struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache[T MemoryItem, D DBItem](
	ttl time.Duration,

	marshal MarshalFunc[T, D],

	fetch FetchFunc[D],

	delete DeleteFunc[D],

	save SaveFunc[D],
	load LoadFunc[T, D],
	opt ...Option,
) *Cache[T, D]

func (*Cache[T, D]) Close

func (c *Cache[T, D]) Close(wgs ...*sync.WaitGroup)

func (*Cache[T, U]) Count

func (c *Cache[T, U]) Count() int

func (*Cache[T, D]) Delete

func (c *Cache[T, D]) Delete(id int64)

func (*Cache[T, U]) Get

func (c *Cache[T, U]) Get(id int64) (T, bool)

func (*Cache[T, D]) GetAll

func (c *Cache[T, D]) GetAll() map[int64]T

func (*Cache[T, U]) Set

func (c *Cache[T, U]) Set(item T)

type CacheItem

type CacheItem[T MemoryItem, D DBItem] struct {
	MemoryItem T
	DBItem     D
}

type DBItem

type DBItem interface {
	GetIdInt64() int64
}

type DataBaseCacheWithKey

type DataBaseCacheWithKey[K comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewDatabaseCacheWithKey

func NewDatabaseCacheWithKey[K comparable, T any](
	ttl time.Duration,
	save SaveDatabase[K, T],
	load LoadFromDatabase[K, T],
) *DataBaseCacheWithKey[K, T]

param: cache : ttl/lru cache save: save to database, function should be blocking, return true is success load: load from database, function should be blocking, return data and error

func (*DataBaseCacheWithKey[K, T]) Close

func (c *DataBaseCacheWithKey[K, T]) Close()

func (*DataBaseCacheWithKey[K, T]) Count

func (c *DataBaseCacheWithKey[K, T]) Count() int

func (*DataBaseCacheWithKey[K, T]) Delete

func (c *DataBaseCacheWithKey[K, T]) Delete(key K)

func (*DataBaseCacheWithKey[K, T]) DisableSave

func (c *DataBaseCacheWithKey[K, T]) DisableSave()

DisableSave disables saving to database

func (*DataBaseCacheWithKey[K, T]) EnableSave

func (c *DataBaseCacheWithKey[K, T]) EnableSave()

EnableSave enables saving to database

func (*DataBaseCacheWithKey[K, T]) ForEach

func (c *DataBaseCacheWithKey[K, T]) ForEach(f func(K, T) bool)

func (*DataBaseCacheWithKey[K, T]) Get

func (c *DataBaseCacheWithKey[K, T]) Get(key K) (T, bool)

func (*DataBaseCacheWithKey[K, T]) GetAll

func (c *DataBaseCacheWithKey[K, T]) GetAll() map[K]T

func (*DataBaseCacheWithKey[K, T]) GetPure

func (c *DataBaseCacheWithKey[K, T]) GetPure(key K) (T, bool)

func (*DataBaseCacheWithKey[K, T]) IsClose

func (c *DataBaseCacheWithKey[K, T]) IsClose() bool

func (*DataBaseCacheWithKey[K, T]) IsSaveDisabled

func (c *DataBaseCacheWithKey[K, T]) IsSaveDisabled() bool

IsSaveDisabled returns whether saving to database is disabled

func (*DataBaseCacheWithKey[K, T]) Set

func (c *DataBaseCacheWithKey[K, T]) Set(key K, memValue T)

type DeleteFunc

type DeleteFunc[D DBItem] func([]D)

type Fetch

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

func NewFetch

func NewFetch[T any](
	fetchFromDB func(context.Context, int) <-chan T,
	opt ...Option,
) *Fetch[T]

func NewFetchWithConfig

func NewFetchWithConfig[T any](
	fetchFromDB func(context.Context, int) <-chan T,
	cfg *config,
) *Fetch[T]

func (*Fetch[T]) Close

func (f *Fetch[T]) Close()

func (*Fetch[T]) DeleteRest

func (f *Fetch[T]) DeleteRest(delete func([]T), wg ...*sync.WaitGroup)

Close stops the background goroutine and closes the buffer channel.

func (*Fetch[T]) Fetch

func (f *Fetch[T]) Fetch() (T, error)

type FetchFunc

type FetchFunc[D DBItem] func(context.Context, int) <-chan D

type LoadFromDatabase

type LoadFromDatabase[K comparable, T any] func(K) (T, error)

load data from database by key attention: this function should be blocking

type LoadFunc

type LoadFunc[T MemoryItem, D DBItem] func(int64) (T, D, error)

type MarshalFunc

type MarshalFunc[T MemoryItem, D DBItem] func(T, D)

type MemoryItem

type MemoryItem interface {
	GetId() int64
	SetId(int64)
}

type Option

type Option func(*config)

func WithContext

func WithContext(ctx context.Context) Option

func WithEnableSave

func WithEnableSave(enables ...bool) Option

func WithFetchSize

func WithFetchSize(size int) Option

func WithName

func WithName(name string) Option

func WithSaveSize

func WithSaveSize(size int) Option

func WithSaveTimeout

func WithSaveTimeout(timeout time.Duration) Option

type Save

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

Save provides a way to collect items and save them in batches using a background goroutine. It buffers items and periodically passes them to a save function for processing.

func NewSave

func NewSave[T any](
	saveToDB func([]T),
	opt ...Option,
) *Save[T]

NewSave creates a new Saver with the specified buffer size and save function. It starts a background goroutine to process items from the buffer.

func NewSaveWithConfig

func NewSaveWithConfig[T any](
	saveToDB func([]T),
	cfg *config,
) *Save[T]

func (*Save[T]) Close

func (s *Save[T]) Close()

Close stops the background goroutine and waits for it to finish. It also processes any remaining items in the buffer before returning.

func (*Save[T]) Save

func (s *Save[T]) Save(item T)

Save adds an item to the buffer for saving. It will be processed by the background goroutine.

type SaveDatabase

type SaveDatabase[K comparable, T any] func(K, T, utils.EvictionReason) bool

save to database attention: this function should be blocking

type SaveFunc

type SaveFunc[D DBItem] func([]D)

Jump to

Keyboard shortcuts

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