lrucache

package module
v0.0.0-...-f751159 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: MIT Imports: 5 Imported by: 0

README

LRUCache

A thread-safe LRU (Least Recently Used) cache implementation in Go.

Features

  • Thread-safe LRU cache implementation
  • Configurable cache capacity
  • Basic operations: Get/Set/Remove
  • Automatic eviction of least recently used items

Installation

go get github.com/hacker4257/lrucache

Usage

package main

import (
    "fmt"
    "github.com/hacker4257/lrucache"
)

func main() {
    // Create a new cache with capacity of 1000
    cache := lrucache.NewLruCache(1000)
    
    // Set a cache entry
    cache.Set("key1", "value1")
    
    // Get a cache entry
    value, exists := cache.Get("key1")
    if exists {
        fmt.Printf("Found value: %v\n", value)
    }
}

API Documentation

NewLruCache(capacity int) *Cache

Creates a new LRU cache instance with the specified capacity.

Get(key string) (any, error)

Retrieves the value for a given key. Returns false if the key doesn't exist.

Set(key string, value any)

Sets a key-value pair in the cache.

Performance

  • Time Complexity: O(1) for Get/Set/Remove operations
  • Space Complexity: O(capacity) where capacity is the cache size

Contributing

Issues and Pull Requests are welcome!

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestLruCache

func TestLruCache(t *testing.T)

Types

type LruCache

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

LruCache represents a least-recently-used (LRU) cache.

func NewLruCache

func NewLruCache(capacity int) *LruCache

NewLruCache creates a new LruCache with the specified capacity.

func (*LruCache) Get

func (c *LruCache) Get(key string) (any, error)

Retrieves a cached item by key and updates its position in the cache.

func (*LruCache) Set

func (c *LruCache) Set(key string, value any)

Adds or updates a cached item with the given key and value.

type Node

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

Jump to

Keyboard shortcuts

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