gce

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

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

Go to latest
Published: Sep 10, 2025 License: CC0-1.0 Imports: 7 Imported by: 0

README

gce

formerly "gache".

generic, fast, sharded, lock-aware in-memory cache for Go.

[!IMPORTANT] gce requires go >=1.25 as it uses the new sync.WaitGroup api.

features

  • shared concurrency
  • optional ttl
  • optional lru eviction
  • atomic stats
  • background stale entry cleanup
  • simple

usage

package main

import (
	"fmt"
	"time"

	"github.com/nxtgo/gce"
)

func main() {
	// create cache with defaults
	c := gce.New[string, string]()

	// set values with TTL
	c.Set("foo", "bar", time.Minute)

	// get values
	if v, ok := c.Get("foo"); ok {
		fmt.Println("value:", v)
	}

	// stats
	fmt.Printf("%+v\n", c.Stats())

	// clean up
	c.Close()
}

license

this project is released under CC0 1.0 public domain with an additional IP waiver. do whatever you want with it. no rights reserved.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func New

func New[K comparable, V any](opts ...Option) *Cache[K, V]

func (*Cache[K, V]) Close

func (c *Cache[K, V]) Close()

func (*Cache[K, V]) Delete

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

func (*Cache[K, V]) Get

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

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() uint64

func (*Cache[K, V]) Purge

func (c *Cache[K, V]) Purge()

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, val V, ttl time.Duration)

func (*Cache[K, V]) Stats

func (c *Cache[K, V]) Stats() Stats

type Option

type Option func(*options)

func WithCleanupInterval

func WithCleanupInterval(d time.Duration) Option

func WithDefaultTTL

func WithDefaultTTL(d time.Duration) Option

func WithMaxEntries

func WithMaxEntries(max uint64) Option

func WithShardCount

func WithShardCount(n int) Option

type Stats

type Stats struct {
	Hits, Misses, Loads, Evictions, CurrentSize uint64
}

Jump to

Keyboard shortcuts

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