lrucache

package module
v0.0.0-...-4efdbcd Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

LRU Cache

A thread-safe LRU cache implementation in Go.

Description

This package provides a thread-safe LRU (Least Recently Used) cache implementation in Go. The cache is implemented using a combination of a hash table and a min-heap, which allows for efficient insertion, retrieval, and eviction of items based on their usage frequency and expiration time.

The cache supports customizable options, such as log level and eviction callback, which allow for fine-grained control over the cache behavior. The cache also supports serialization and deserialization of values, which allows for storing and retrieving arbitrary data types.

Usage

Installation

To use this package in your project, add the following import statement to your Go code:

import "github.com/shammianand/lrucache"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheNotInitialized = errors.New("cache not initialized")
	ErrItemExpired         = errors.New("item expired")
	ErrItemNotFound        = errors.New("item not found")
)

Functions

This section is empty.

Types

type CacheItem

type CacheItem struct {
	Key       string
	Value     []byte
	ExpiresAt time.Time
}

type EvictCallback

type EvictCallback func(key string, value interface{})

EvictCallback is a function that is called when an item is evicted from the cache.

type LRU

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

func NewLRUWithTTL

func NewLRUWithTTL(size int, opts Options) (*LRU, error)

func (*LRU) Clear

func (l *LRU) Clear() error

func (*LRU) Delete

func (l *LRU) Delete(key string) error

func (*LRU) Get

func (l *LRU) Get(key string) (interface{}, error)

func (*LRU) Len

func (l *LRU) Len() int

func (*LRU) Set

func (l *LRU) Set(key string, value interface{}, ttl time.Duration) error

type Options

type Options struct {
	LogLevel      string // "debug", "info", "warn", "error"
	EvictCallback EvictCallback
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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