utils

package
v1.78.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicWriteFile

func AtomicWriteFile(filePath string, data []byte, perm os.FileMode) error

AtomicWriteFile writes data to a file atomically using a temporary file This prevents corruption if the process is interrupted during writing

func CheckHTTPResponse

func CheckHTTPResponse(resp *http.Response, url string) error

CheckHTTPResponse checks if HTTP response indicates an error

func EnsureDir

func EnsureDir(dir string, perm os.FileMode) error

EnsureDir ensures that a directory exists, creating it if necessary

func MarshalJSONIndentString

func MarshalJSONIndentString(data interface{}, indent string) (string, error)

MarshalJSONIndentString marshals a Go object to an indented JSON string

func MarshalJSONString

func MarshalJSONString(data interface{}) (string, error)

MarshalJSONString marshals a Go object to a JSON string

func NewDefaultHTTPClient

func NewDefaultHTTPClient() *http.Client

NewDefaultHTTPClient creates a new HTTP client with default configuration

func NewHTTPClient

func NewHTTPClient(config HTTPClientConfig) *http.Client

NewHTTPClient creates a new HTTP client with the given configuration

func ReadJSONFile

func ReadJSONFile(filePath string, target interface{}) error

ReadJSONFile reads a JSON file into a Go object

func SafeClose

func SafeClose(file *os.File, fileName string)

SafeClose safely closes a file with error logging

func SafeCloseResponse

func SafeCloseResponse(resp *http.Response)

SafeCloseResponse safely closes HTTP response body with error logging

func WriteJSONFile

func WriteJSONFile(filePath string, data interface{}, options JSONWriteOptions) error

WriteJSONFile writes a Go object to a JSON file with the given options

func WriteJSONFileDefault

func WriteJSONFileDefault(filePath string, data interface{}) error

WriteJSONFileDefault writes a Go object to a JSON file with default options

Types

type HTTPClientConfig

type HTTPClientConfig struct {
	Timeout time.Duration
}

HTTPClientConfig holds configuration for HTTP client creation

func DefaultHTTPClientConfig

func DefaultHTTPClientConfig() HTTPClientConfig

DefaultHTTPClientConfig returns default HTTP client configuration

type HTTPError

type HTTPError struct {
	StatusCode int
	Message    string
	URL        string
}

HTTPError represents an HTTP error with status code and message

func (HTTPError) Error

func (e HTTPError) Error() string

type JSONWriteOptions

type JSONWriteOptions struct {
	Indent   string
	FileMode os.FileMode
	Atomic   bool
}

JSONWriteOptions holds options for JSON writing

func DefaultJSONWriteOptions

func DefaultJSONWriteOptions() JSONWriteOptions

DefaultJSONWriteOptions returns default options for JSON writing

type TTLCache

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

TTLCache is a thread-safe cache with TTL (Time To Live) support

func NewTTLCache

func NewTTLCache(ttl time.Duration) *TTLCache

NewTTLCache creates a new TTL cache with the specified default TTL

func (*TTLCache) CleanupExpired

func (c *TTLCache) CleanupExpired() int

CleanupExpired removes all expired items from the cache

func (*TTLCache) Clear

func (c *TTLCache) Clear()

Clear removes all items from the cache

func (*TTLCache) Delete

func (c *TTLCache) Delete(key string)

Delete removes a value from the cache

func (*TTLCache) Get

func (c *TTLCache) Get(key string) (interface{}, bool)

Get retrieves a value from the cache Returns the value and true if found and not expired, nil and false otherwise

func (*TTLCache) Set

func (c *TTLCache) Set(key string, value interface{})

Set stores a value in the cache with the default TTL

func (*TTLCache) SetWithTTL

func (c *TTLCache) SetWithTTL(key string, value interface{}, ttl time.Duration)

SetWithTTL stores a value in the cache with a custom TTL

func (*TTLCache) Size

func (c *TTLCache) Size() int

Size returns the number of items in the cache (including expired items)

func (*TTLCache) StartCleanupGoroutine

func (c *TTLCache) StartCleanupGoroutine(interval time.Duration)

StartCleanupGoroutine starts a background goroutine that periodically cleans up expired items

type TTLCacheItem

type TTLCacheItem struct {
	Value     interface{}
	ExpiresAt time.Time
}

TTLCacheItem represents a cache item with expiration

func (TTLCacheItem) IsExpired

func (item TTLCacheItem) IsExpired() bool

IsExpired checks if the cache item has expired

Jump to

Keyboard shortcuts

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