persistent_cache

package module
v0.0.0-...-3e979e7 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2025 License: MIT Imports: 11 Imported by: 0

README

go-persistent-cache

go-persistent-cache is a Go library that provides a persistent caching mechanism using SQLite and Gob encoding. It allows you to memoize function results with a specified time-to-live (TTL), storing the results in a SQLite database for efficient retrieval. This can significantly improve the performance of your applications by avoiding redundant computations.

Features

  • Persistent caching using SQLite
  • Gob encoding for efficient serialization and deserialization
  • Memoization of functions with configurable TTL
  • Thread-safe singleton cache instance

Example Usage

package main

import (
    "fmt"
    "log/slog"
    "os"
    "time"
)

// multiply is a sample function to demonstrate memoization
func multiply(a, b int) int {
    time.Sleep(2 * time.Second)
    return a * b
}

func main() {
    // Configure slog to show debug messages
    logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
        Level: slog.LevelDebug,
    }))
    slog.SetDefault(logger)

    // Memoize the multiply function with a TTL of 5 seconds
    memoizeMultiply := Memoize2(5*time.Second, multiply)

    // Call the memoized function multiple times
    fmt.Println(memoizeMultiply(2, 3))
    fmt.Println(memoizeMultiply(2, 3))
    fmt.Println(memoizeMultiply(2, 3))
    fmt.Println(memoizeMultiply(2, 3))
}

Installation

To install the go-persistent-cache library, use the following command:

go get github.com/FlavioAmurrioCS/go-persistent-cache

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteFuncCache

func DeleteFuncCache(fn any)

func Memoize0

func Memoize0[R any](ttl time.Duration, fn func() R) func() R

func Memoize1

func Memoize1[A, R any](ttl time.Duration, fn func(A) R) func(A) R

func Memoize2

func Memoize2[A, B, R any](ttl time.Duration, fn func(A, B) R) func(A, B) R

func Memoize3

func Memoize3[A, B, C, R any](ttl time.Duration, fn func(A, B, C) R) func(A, B, C) R

func Memoize4

func Memoize4[A, B, C, D, R any](ttl time.Duration, fn func(A, B, C, D) R) func(A, B, C, D) R

func Memoize5

func Memoize5[A, B, C, D, E, R any](ttl time.Duration, fn func(A, B, C, D, E) R) func(A, B, C, D, E) R

func Memoize6

func Memoize6[A, B, C, D, E, F, R any](ttl time.Duration, fn func(A, B, C, D, E, F) R) func(A, B, C, D, E, F) R

func Memoize7

func Memoize7[A, B, C, D, E, F, G, R any](ttl time.Duration, fn func(A, B, C, D, E, F, G) R) func(A, B, C, D, E, F, G) R

func Memoize8

func Memoize8[A, B, C, D, E, F, G, H, R any](ttl time.Duration, fn func(A, B, C, D, E, F, G, H) R) func(A, B, C, D, E, F, G, H) R

func Memoize9

func Memoize9[A, B, C, D, E, F, G, H, I, R any](ttl time.Duration, fn func(A, B, C, D, E, F, G, H, I) R) func(A, B, C, D, E, F, G, H, I) R

func MemoizeN

func MemoizeN[R any](ttl time.Duration, fn func(args ...any) R) func(...any) R

Types

type Cache

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

Cache handles SQLite-based persistent caching with Gob encoding

Jump to

Keyboard shortcuts

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