cache

package module
v0.0.0-...-700a86b Latest Latest
Warning

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

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

README

GIMC: Go's In-Memory Cache

A Go package for in-memory caching. Aiming at thread-safety, simplicity & code readability.

  • simple, single-value cache
  • support for expiration
  • single method for getting & setting value
  • thread-safe
  • (optional) Thundering Herd-resistant

How to use

// first get the package using `go get github.com/azophy/gimc`
package main

import (
  "fmt"
  "time"

  "github.com/azophy/gimc"
)

var testCache = cache.Cache[int]{}

func main() {
  _, err := testCache.Fetch(10*time.Second, func() (int, error) { return 3, nil })
  res, err := testCache.Fetch(3*time.Second, func() (int, error) { return 5, nil })

  if err != nil {
    fmt.Println(err.Error())
  }

  fmt.Printf("value from cache is: %d\n", res)
}

Running test

$ go test cache*.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[ValType any] struct {
	// contains filtered or unexported fields
}

func (*Cache[ValType]) Fetch

func (c *Cache[ValType]) Fetch(expireDuration time.Duration, loader func() (ValType, error)) (ValType, error)

fetch from cache if it exists & not expired. else load it from loader()

func (*Cache[ValType]) Reset

func (c *Cache[ValType]) Reset()

reset expireTime & val into zero values

Jump to

Keyboard shortcuts

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