lrucache

package module
v0.0.0-...-9dfad47 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 2 Imported by: 0

README

LRU Cache

Thread-safe LRU (Least Recently Used) cache implementation in Go with generics.

Installation

go get github.com/elcruzo/lru-cache

Usage

package main

import (
    "fmt"
    lru "github.com/elcruzo/lru-cache"
)

func main() {
    cache := lru.New[string, int](100)

    cache.Put("a", 1)
    cache.Put("b", 2)

    if val, ok := cache.Get("a"); ok {
        fmt.Println(val) // 1
    }

    cache.Delete("b")
    cache.Clear()
}

API

Method Description
New[K, V](capacity) Create cache with capacity
Get(key) Get value, returns (value, exists)
Put(key, value) Add or update entry
Delete(key) Remove entry
Len() Current size
Cap() Maximum capacity
Clear() Remove all entries
Keys() Get all keys (most recent first)

Features

  • Generic types (Go 1.18+)
  • Thread-safe with RWMutex
  • O(1) get/put operations
  • Automatic eviction of least recently used items

License

MIT License - see LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRUCache

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

func New

func New[K comparable, V any](capacity int) *LRUCache[K, V]

func (*LRUCache[K, V]) Cap

func (c *LRUCache[K, V]) Cap() int

func (*LRUCache[K, V]) Clear

func (c *LRUCache[K, V]) Clear()

func (*LRUCache[K, V]) Delete

func (c *LRUCache[K, V]) Delete(key K) bool

func (*LRUCache[K, V]) Get

func (c *LRUCache[K, V]) Get(key K) (V, bool)

func (*LRUCache[K, V]) Keys

func (c *LRUCache[K, V]) Keys() []K

func (*LRUCache[K, V]) Len

func (c *LRUCache[K, V]) Len() int

func (*LRUCache[K, V]) Put

func (c *LRUCache[K, V]) Put(key K, value V)

Jump to

Keyboard shortcuts

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