obcache-go

module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2025 License: MIT

README

obcache-go

High-performance, thread-safe caching library for Go with automatic function wrapping and TTL support.

Go Reference Go Report Card

Installation

go get github.com/vnykmshr/obcache-go

Quick Start

package main

import (
    "fmt"
    "time"
    "github.com/vnykmshr/obcache-go/pkg/obcache"
)

func expensiveFunction(id int) (string, error) {
    time.Sleep(100 * time.Millisecond) // Simulate expensive work
    return fmt.Sprintf("result-%d", id), nil
}

func main() {
    cache, _ := obcache.New(obcache.NewDefaultConfig())
    
    // Wrap function with caching
    cachedFunc := obcache.Wrap(cache, expensiveFunction)
    
    // First call: slow (cache miss)
    result1, _ := cachedFunc(123) 
    
    // Second call: fast (cache hit)
    result2, _ := cachedFunc(123)
    
    fmt.Println(result1, result2) // Same result, much faster
}
Basic Operations
cache, _ := obcache.New(obcache.NewDefaultConfig())

// Set with TTL
cache.Set("key", "value", time.Hour)

// Get value
if value, found := cache.Get("key"); found {
    fmt.Println("Found:", value)
}

// Delete
cache.Delete("key")

// Stats
stats := cache.Stats()
fmt.Printf("Hit rate: %.1f%%\n", stats.HitRate())

Configuration

Memory Cache
config := obcache.NewDefaultConfig().
    WithMaxEntries(1000).
    WithDefaultTTL(30 * time.Minute)

cache, _ := obcache.New(config)
Redis Backend
config := obcache.NewRedisConfig("localhost:6379").
    WithRedis(&obcache.RedisConfig{
        KeyPrefix: "myapp:",
    }).
    WithDefaultTTL(time.Hour)

cache, _ := obcache.New(config)
Compression
config := obcache.NewDefaultConfig().
    WithCompression(&compression.Config{
        Enabled:   true,
        Algorithm: compression.CompressorGzip,
        MinSize:   1000, // Only compress values > 1KB
    })

Features

  • Function wrapping - Automatically cache expensive function calls
  • TTL support - Time-based expiration
  • LRU eviction - Automatic cleanup of old entries
  • Thread safe - Concurrent access support
  • Redis backend - Distributed caching
  • Compression - Automatic value compression (gzip/deflate)
  • Statistics - Hit rates, miss counts, etc.
  • Hooks - Event callbacks for cache operations

Examples

See examples/ for complete examples:

License

MIT License - see LICENSE file.

Directories

Path Synopsis
advanced command
basic command
compression command
metrics command
redis-cache command
internal
pkg
obcache
Package obcache provides a high-performance, thread-safe, in-memory cache with TTL support, LRU eviction, function memoization, and advanced hook system for observability.
Package obcache provides a high-performance, thread-safe, in-memory cache with TTL support, LRU eviction, function memoization, and advanced hook system for observability.

Jump to

Keyboard shortcuts

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