util

package
v0.18.3 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package util provides utility functions for type conversions and other common operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SafeIntToUint16

func SafeIntToUint16(v int) uint16

SafeIntToUint16 converts int to uint16 with overflow check.

func SafeIntToUint32

func SafeIntToUint32(v int) uint32

SafeIntToUint32 converts int to uint32 with overflow check.

func SafeIntToUint8

func SafeIntToUint8(v int) uint8

SafeIntToUint8 converts int to uint8 with overflow check.

func SafeUintToInt

func SafeUintToInt(v uint) int

SafeUintToInt converts uint to int with overflow check.

func StrToBool added in v0.5.0

func StrToBool(str string) (bool, error)

StrToBool converts a string to bool considering various truthy representations. Accepts: "true", "yes", "y", "on", "1", "t" as true Accepts: "false", "no", "n", "off", "0", "f" as false Returns error for empty strings or unrecognized values.

Types

type TTLCache added in v0.10.1

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

TTLCache is a simple in-memory cache with time-to-live expiration. Expired entries are removed lazily during Get and Set operations.

func NewTTLCache added in v0.10.1

func NewTTLCache[V any](ttl time.Duration) *TTLCache[V]

NewTTLCache creates a cache with the specified TTL for all entries.

func (*TTLCache[V]) Clear added in v0.10.1

func (c *TTLCache[V]) Clear()

Clear removes all entries from the cache.

func (*TTLCache[V]) Delete added in v0.10.1

func (c *TTLCache[V]) Delete(key string)

Delete removes an entry from the cache.

func (*TTLCache[V]) Get added in v0.10.1

func (c *TTLCache[V]) Get(key string) (V, bool)

Get retrieves a value if it exists and hasn't expired. Returns the value and true if found and valid, zero value and false otherwise.

func (*TTLCache[V]) Len added in v0.10.1

func (c *TTLCache[V]) Len() int

Len returns the number of entries in the cache (including expired ones).

func (*TTLCache[V]) Set added in v0.10.1

func (c *TTLCache[V]) Set(key string, value V)

Set stores a value with the cache's default TTL.

Jump to

Keyboard shortcuts

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