lazymap

package module
v0.0.0-...-55fd9cb Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 8 Imported by: 31

README

lazymap

Golang thread-safe LazyMap implementation with additional features:

  1. Concurrency control
  2. Capacity control (if defined, it automatically cleans least recently used elements)
  3. Expiration (if defined, it automatically deletes expired elements)

The main purpose of LazyMap at webtor.io was to reduce requests between services by introducing intermediate caching-layer.

But what is LazyMap anyway?

Unlike ordinary Map, LazyMap evaluates value on demand if there is no value already exists for a specific key.

Example

package main

import (
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"github.com/webtor-io/lazymap"
)

type ResponseMap struct {
    *lazymap.LazyMap[string]
    cl *http.Client
}

func NewResponseMap(cl *http.Client) *ResponseMap {
	return &ResponseMap{
		cl: cl,
        LazyMap: lazymap.New[string](&lazymap.Config{
            Concurrency: 10,
            Expire:      600 * time.Second,
            ErrorExpire: 30 * time.Second,
            Capacity:    1000,
        }),
	}
}
func (s *ResponseMap) get(u string) (string, error) {
	res, err := s.cl.Get(u)
	if err != nil {
		return "", err
	}
	b := res.Body
	defer b.Close()
	r, err := ioutil.ReadAll(b)
	if err != nil {
		return "", err
	}
	return r, nil
}

func (s *ResponseMap) Get(u string) (string, error) {
	v, err := s.LazyMap.Get(u, func() (string, error) {
		return s.get(u)
	})
	if err != nil {
		return "", err
	}
	return v, nil
}

func main() {
	rm := NewResponseMap(http.DefaultClient)
	res, err := rm.Get("https://example.org")
	if err != nil {
		log.Fatal(err)
	}
	log.Println(res)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Concurrency    int
	Expire         time.Duration
	ErrorExpire    time.Duration
	StoreErrors    bool
	InitExpire     time.Duration
	Capacity       int
	CleanThreshold float64
	CleanRatio     float64
	EvictNotInited bool
}

type EvictedError

type EvictedError struct{}

func (*EvictedError) Error

func (s *EvictedError) Error() string

type ItemStatus

type ItemStatus int
const (
	None ItemStatus = iota
	Enqueued
	Running
	Done
	Failed
	Canceled
)

type LazyMap

type LazyMap[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](cfg *Config) *LazyMap[T]

func (*LazyMap[T]) Drop

func (s *LazyMap[T]) Drop(key string)

Drop removes whatever is stored under key, if anything.

func (*LazyMap[T]) Get

func (s *LazyMap[T]) Get(key string, f func() (T, error)) (T, error)

func (*LazyMap[T]) Keys

func (s *LazyMap[T]) Keys() []string

func (*LazyMap[T]) Len

func (s *LazyMap[T]) Len() int

func (*LazyMap[T]) Status

func (s *LazyMap[T]) Status(key string) (ItemStatus, bool)

func (*LazyMap[T]) Touch

func (s *LazyMap[T]) Touch(key string) bool

type PanicError

type PanicError struct {
	Value any
	Stack []byte
}

PanicError is a panic in f, returned as Get's error. Error leaves the stack out: callers hand errors to clients (rest-api's JSON body, torrent-store's gRPC status), and a stack there leaks source paths. Log Stack explicitly.

func (*PanicError) Error

func (e *PanicError) Error() string

Error renders Value on one line, cut to 200 runes: a panic value can be any size, and the message ends up in log lines and client responses. Control characters (\v, \f and NEL break lines too, ESC drives terminals) and U+2028/U+2029 become spaces.

func (*PanicError) Unwrap

func (e *PanicError) Unwrap() error

Unwrap gives errors.Is/As the panicked value when it is an error.

Jump to

Keyboard shortcuts

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