standards

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: BSD-3-Clause Imports: 1 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Standards

The standards package provides a set of interfaces and implementations for caching and logging functionalities, designed to be flexible and extensible. It includes interfaces for cache operations, a logger interface, and implementations for both cache and logging functionalities.

Static Badge

GoDoc GitHub stars Go Report Card codecov

Versions

Stable Version GitHub Release GitHub Release

Installation

To install the standards package, use the following Go command:

go get -u github.com/gouef/standards

Caching

The standards package includes different types of cache interfaces and implementations.

Cache Interface

The Cache interface defines methods for working with cache items. It provides functionality for retrieving, saving, deleting, and clearing cache items.

Methods:
  • GetItem(key string) CacheItem: Retrieves a cache item by key or nil if not exists.
  • GetItems(keys ...string) []CacheItem: Retrieves multiple cache items by keys.
  • HasItem(key string) bool: Checks if a cache item exists.
  • Clear() error: Clears all items in the cache.
  • DeleteItem(key string) error: Deletes a specific cache item.
  • DeleteItems(keys ...string) error: Deletes multiple cache items.
  • Save(item CacheItem) error: Immediately persists a cache item.
  • SaveDeferred(item CacheItem) error: Marks a cache item to be saved later.
  • Commit() error: Persists any deferred cache items.
CacheItem Interface

The CacheItem interface represents individual cache items with methods for interacting with them.

Methods:
  • GetKey() string: Returns the key of the cache item.
  • Get() any: Retrieves the value of the cache item.
  • IsHit() bool: Confirms if the cache lookup was a hit.
  • Set(value any) (CacheItem, error): Sets the value of the cache item.
  • ExpiresAt(expiration time.Time) (CacheItem, error): Sets the absolute expiration time for the cache item.
  • ExpiresAfter(t time.Duration): Sets the relative expiration time for the cache item.
CacheSimple Interface

The CacheSimple interface provides simplified cache operations with key-value pairs.

Methods:
  • Get(key string, defaultValue any) any: Retrieves a value from the cache.
  • GetMultiply(keys []string, defaultValue any) []any: Retrieves multiple values from the cache.
  • Has(key string) bool: Checks if a key exists in the cache.
  • Clear() error: Clears all cache items.
  • Delete(key string) error: Deletes a specific cache item.
  • DeleteMultiply(keys ...string) error: Deletes multiple cache items.
  • Set(key string, item any) error: Adds or updates a cache item.
  • SetMultiply(values []any, ttl time.Duration) error: Adds or updates multiple cache items with a TTL.

Logging

The standards package provides a logging system with multiple log levels, making it easy to implement custom loggers or use the provided default LoggerNull.

Logger Interface

The Logger interface defines methods for logging at various levels.

Methods:
  • Emergency(message string, context []any) error: Logs an emergency level message.
  • Alert(message string, context []any) error: Logs an alert level message.
  • Critical(message string, context []any) error: Logs a critical level message.
  • Error(message string, context []any) error: Logs an error message.
  • Warning(message string, context []any) error: Logs a warning message.
  • Notice(message string, context []any) error: Logs a notice level message.
  • Info(message string, context []any) error: Logs an info level message.
  • Debug(message string, context []any) error: Logs a debug level message.
  • Log(level LogLevel, message string, context []any) error: Logs a message with an arbitrary log level.
LogLevel Enum

The LogLevel type defines the following log levels:

  • EMERGENCY: System is unusable.
  • ALERT: Immediate action required.
  • CRITICAL: Critical conditions.
  • ERROR: Errors that do not require immediate action.
  • WARNING: Exceptional occurrences, not errors.
  • NOTICE: Normal but significant events.
  • INFO: Informative events.
  • DEBUG: Detailed debug information.
LoggerNull Implementation

The LoggerNull type implements the Logger interface and provides a no-op (no operation) logger. It does not perform any logging, making it useful for scenarios where logging is not required.

Example usage:
package main

import (
	"github.com/gouef/standards"
	"fmt"
)

func main() {
	// Create a new LoggerNull instance
	logger := &standards.LoggerNull{}

	// Example of using the logger methods
	logger.Info("Informational message", nil)
	logger.Warning("Warning message", nil)
	logger.Error("Error message", nil)
}

LoggerAware Interface

The LoggerAware interface provides a SetLogger method to inject a logger into an object that requires logging functionality.

Method:
  • SetLogger(logger Logger): Injects a logger instance into the object.

Contributing

Read Contributing

Contributors

JanGalek actions-user

Documentation

Index

Constants

View Source
const KeepTTL = -1

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// GetItem Returns a CacheItem representing the specified key.
	GetItem(key string) CacheItem
	// GetItems Returns a list of cache items.
	GetItems(keys ...string) []CacheItem
	// HasItem Confirms if the cache contains specified cache item.
	HasItem(key string) bool
	// Clear Deletes all items in the pool
	Clear() error
	// DeleteItem Removes the item from the pool.
	DeleteItem(key string) error
	// DeleteItems Removes multiple items from the pool.
	DeleteItems(keys ...string) error
	// Save Persists a cache item immediately.
	Save(item CacheItem) error
	// SaveDeferred Sets a cache item to be persisted later.
	SaveDeferred(item CacheItem) error
	// Commit Persists any deferred cache items.
	Commit() error
}

type CacheItem

type CacheItem interface {
	// GetKey Returns the key for the current cache item.
	GetKey() string
	// Get Retrieves the value of the item from the cache.
	Get() any
	// IsHit Confirms if the cache item lookup resulted in a cache hit.
	IsHit() bool
	// Set Sets the value represented by this cache item
	Set(value any, ttl time.Duration) (CacheItem, error)
	// ExpiresAt Sets the absolute expiration time for this cache item.
	ExpiresAt(expiration time.Time) (CacheItem, error)
	// ExpiresAfter Sets the relative expiration time for this cache item.
	ExpiresAfter(t time.Duration)
}

type CacheSimple

type CacheSimple interface {
	// Get Returns a value from the cache.
	Get(key string, defaultValue any) any
	// GetMultiply Returns a list of cache items.
	GetMultiply(keys []string, defaultValue any) []any
	// Has Determines whether an item is present in the cache.
	Has(key string) bool
	// Clear Deletes all cache's keys.
	Clear() error
	// Delete Remove an item from the cache.
	Delete(key string) error
	// DeleteMultiply Removes multiple items in a single operation.
	DeleteMultiply(keys ...string) error
	// Set Persists a cache item.
	Set(key string, item any, ttl time.Duration) error
	// SetMultiply Persists a cache items.
	SetMultiply(values map[string]any, ttl time.Duration) error
}

type LogLevel

type LogLevel string
var (
	EMERGENCY LogLevel = "emergency"
	ALERT     LogLevel = "alert"
	CRITICAL  LogLevel = "critical"
	ERROR     LogLevel = "error"
	WARNING   LogLevel = "warning"
	NOTICE    LogLevel = "notice"
	INFO      LogLevel = "info"
	DEBUG     LogLevel = "debug"
)

type Logger

type Logger interface {
	// Emergency System is unusable
	Emergency(message string, context []any) error
	// Alert Action must be taken immediately.
	Alert(message string, context []any) error
	// Critical Critical conditions.
	Critical(message string, context []any) error
	// Error Runtime errors that do not require immediate action but should typically
	Error(message string, context []any) error
	// Warning Exceptional occurrences that are not errors.
	Warning(message string, context []any) error
	// Notice Normal but significant events.
	Notice(message string, context []any) error
	// Info Interesting events
	Info(message string, context []any) error
	// Debug Detailed debug information.
	Debug(message string, context []any) error
	// Log Logs with an arbitrary level.
	Log(level LogLevel, message string, context []any) error
}

type LoggerAware

type LoggerAware interface {
	SetLogger(logger Logger)
}

type LoggerNull

type LoggerNull struct {
}

func (*LoggerNull) Alert

func (l *LoggerNull) Alert(message string, context []any) error

Alert Action must be taken immediately.

func (*LoggerNull) Critical

func (l *LoggerNull) Critical(message string, context []any) error

Critical Critical conditions.

func (*LoggerNull) Debug

func (l *LoggerNull) Debug(message string, context []any) error

Debug Detailed debug information.

func (*LoggerNull) Emergency

func (l *LoggerNull) Emergency(message string, context []any) error

Emergency System is unusable

func (*LoggerNull) Error

func (l *LoggerNull) Error(message string, context []any) error

Error Runtime errors that do not require immediate action but should typically

func (*LoggerNull) Info

func (l *LoggerNull) Info(message string, context []any) error

Info Interesting events

func (*LoggerNull) Log

func (l *LoggerNull) Log(level LogLevel, message string, context []any) error

Log Logs with an arbitrary level.

func (*LoggerNull) Notice

func (l *LoggerNull) Notice(message string, context []any) error

Notice Normal but significant events.

func (*LoggerNull) Warning

func (l *LoggerNull) Warning(message string, context []any) error

Warning Exceptional occurrences that are not errors.

Jump to

Keyboard shortcuts

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