cache

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

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 2 Imported by: 0

README

Generic Cache Implementations in Go

This repository provides thread-safe, generic implementations of various caching strategies, designed for high-performance concurrent access in Go. The initial offering includes an LRU (Least Recently Used) cache, with plans to expand to other strategies based on usage and demand.

Features

  • Generic Implementation: Works with any key and value types that are comparable in Go, thanks to Go's generics.
  • Concurrency Safe: Designed to be safe for concurrent use by multiple goroutines without the need for external synchronization.
  • Efficient Operations: Optimized for low memory overhead and fast operations, suitable for high-load environments.
  • Eviction Policies: Each cache implementation comes with its own eviction policy, starting with LRU.

Getting Started

Installation

To include the cache package in your Go project, use the following go get command:

go get -u github.com/edast/cache
Usage

Here's a quick example of how to use the LRUCache:

package main

import (
    "fmt"

    "github.com/edast/cache"
)

func main() {
    // Create a new LRU cache with a capacity for 2 items
    lru := cache.NewLRUCache[string, int](2)

    // Add items to the cache
    lru.Put("Alice", 1)
    lru.Put("Bob", 2)

    // Retrieve and print an item
    if val, found := lru.Get("Alice"); found {
        fmt.Printf("Alice's value: %d\n", val)
    }

    // Add another item, causing `Bob` item to be evicted
    lru.Put("Charlie", 3)

    // Try to retrieve the evicted item
    if _, found := lru.Get("Bob"); !found {
        fmt.Println("Bob was evicted")
    }
}

Benchmarks

Benchmark tests are included to evaluate the performance of cache operations. Run the benchmarks using the Go tool:

go test -bench=. github.com/edast/cache

Contributing

Contributions are welcome, whether they're for new cache strategies, optimizations, bug reports, or documentation improvements. Please feel free to submit issues or pull requests.

License

This project is licensed under the MIT License - see the LICENSE file 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
}

LRUCache implements a generic Least Recently Used (LRU) cache. It automatically evicts the least recently accessed items to maintain a fixed size. The cache is thread-safe, supporting concurrent access by multiple goroutines.

func NewLRUCache

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

NewLRUCache creates a new instance of an LRUCache with the given capacity. It initializes the internal data structures and prepares the cache for use.

func (*LRUCache[K, V]) Get

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

Get retrieves the value associated with the given key from the cache. If the key is found in the cache, Get returns the value and true. Otherwise, it returns the zero value for V and false.

func (*LRUCache[K, V]) Put

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

Put adds a key-value pair to the cache. If the key already exists, its value is updated. If adding a new key exceeds the cache's capacity, the least recently used item is evicted. Put is safe to call from multiple goroutines.

Jump to

Keyboard shortcuts

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