freecache

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package freecache provides a high-performance, zero-GC overhead implementation of httpcache.Cache using github.com/coocood/freecache as the underlying storage.

This backend is suitable for applications that need to cache millions of entries with minimal GC overhead and automatic memory management with LRU eviction.

Example usage:

cache := freecache.New(100 * 1024 * 1024) // 100MB cache
transport := httpcache.NewTransport(cache)
client := transport.Client()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is an implementation of httpcache.Cache that uses freecache for storage. It provides zero-GC overhead and automatic LRU eviction when cache is full.

func New

func New(size int) *Cache

New creates a new Cache with the specified size in bytes. The cache size will be set to 512KB at minimum.

For large cache sizes, you may want to call debug.SetGCPercent() with a lower value to reduce GC overhead.

Example:

import "runtime/debug"
cache := freecache.New(100 * 1024 * 1024) // 100MB
debug.SetGCPercent(20)

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all entries from the cache

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes the entry with the given key from the cache

func (*Cache) EntryCount

func (c *Cache) EntryCount() int64

EntryCount returns the number of entries currently in the cache

func (*Cache) EvacuateCount

func (c *Cache) EvacuateCount() int64

EvacuateCount returns the number of times entries were evicted due to cache being full

func (*Cache) ExpiredCount

func (c *Cache) ExpiredCount() int64

ExpiredCount returns the number of times entries expired

func (*Cache) Get

func (c *Cache) Get(key string) ([]byte, bool)

Get returns the cached response bytes and true if present, false if not found

func (*Cache) HitRate

func (c *Cache) HitRate() float64

HitRate returns the ratio of cache hits to total lookups

func (*Cache) ResetStatistics

func (c *Cache) ResetStatistics()

ResetStatistics resets all statistics counters (hit rate, evictions, etc.)

func (*Cache) Set

func (c *Cache) Set(key string, value []byte)

Set stores the response bytes in the cache with the given key. If the cache is full, it will evict the least recently used entry. The entry has no expiration time and will only be evicted when cache is full.

Jump to

Keyboard shortcuts

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