cache

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

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

Go to latest
Published: Feb 7, 2025 License: GPL-3.0 Imports: 2 Imported by: 0

README

cache

A very simple cache implementation in Go using generics. 0 external dependencies. Based on github.com/akyoto/cache.

func ExampleCache() {
	c := New[string, int]()

	c.Set("key", 123, 0)

	v, ex := c.Get("key")
	if v != 123 {
		panic("unexpected value")
	}

	c.Set("expired", 12345, time.Nanosecond)
	time.Sleep(2 * time.Nanosecond)
	_, ex = c.Get("expired")
	if ex {
		panic("expected expired item to not exist")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleCache

func ExampleCache()

Types

type Cache

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

func New

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

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]) Keys

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

func (*Cache[K, V]) Len

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

func (*Cache[K, V]) Set

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

type Item

type Item[V any] struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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