Documentation
¶
Index ¶
- Constants
- Variables
- func NewCheckAndSetFailedError(key string, err error) error
- func NewCloseFailedError(err error) error
- func NewDeleteFailedError(key string, err error) error
- func NewGetFailedError(key string, err error) error
- func NewInvalidConfigError(field string) error
- func NewInvalidValueTypeError(key string, value any) error
- func NewLockFailedError(key string, err error) error
- func NewNilConfigError() error
- func NewSetFailedError(key string, err error) error
- func NewValueConversionError(key string, value any, err error) error
- func NewValueExpiredError(key string) error
- type Backend
- func (m *Backend) CheckAndSet(ctx context.Context, key, oldValue, newValue string, expiration time.Duration) (bool, error)
- func (m *Backend) Cleanup()
- func (m *Backend) Close() error
- func (m *Backend) Delete(ctx context.Context, key string) error
- func (m *Backend) Get(ctx context.Context, key string) (string, error)
- func (m *Backend) Set(ctx context.Context, key string, value string, expiration time.Duration) error
Constants ¶
View Source
const ( // DefaultCleanupInterval is the default interval for cleaning up expired entries DefaultCleanupInterval = 10 * time.Minute )
Variables ¶
View Source
var ( // Configuration errors ErrInvalidConfig = errors.New("memory backend requires memory.Config") ErrNilConfig = errors.New("memory backend config cannot be nil") // Operation errors ErrSetFailed = errors.New("failed to set key") ErrGetFailed = errors.New("failed to get key") ErrDeleteFailed = errors.New("failed to delete key") ErrCloseFailed = errors.New("failed to close memory backend") // CheckAndSet specific errors ErrCheckAndSetFailed = errors.New("failed to perform check-and-set operation") ErrInvalidValueType = errors.New("invalid value type for check-and-set") ErrValueExpired = errors.New("value has expired") // Internal errors ErrLockFailed = errors.New("failed to acquire lock") ErrValueConversion = errors.New("failed to convert value to string") )
Functions ¶
func NewCheckAndSetFailedError ¶ added in v0.0.6
CheckAndSet error functions
func NewCloseFailedError ¶ added in v0.0.6
func NewDeleteFailedError ¶ added in v0.0.6
func NewGetFailedError ¶ added in v0.0.6
Operation error functions
func NewInvalidConfigError ¶ added in v0.0.6
Configuration error functions
func NewInvalidValueTypeError ¶ added in v0.0.6
func NewLockFailedError ¶ added in v0.0.6
Internal error functions
func NewNilConfigError ¶ added in v0.0.6
func NewNilConfigError() error
func NewSetFailedError ¶ added in v0.0.6
func NewValueConversionError ¶ added in v0.0.6
func NewValueExpiredError ¶ added in v0.0.6
Types ¶
type Backend ¶ added in v0.0.5
type Backend struct {
// contains filtered or unexported fields
}
func New ¶
func New() *Backend
New initializes a new in-memory storage instance with default (10 minutes) cleanup.
func NewWithCleanup ¶ added in v0.0.5
NewWithCleanup initializes a new in-memory storage instance with custom cleanup interval. Set interval to 0 to disable automatic cleanup.
func (*Backend) CheckAndSet ¶ added in v0.0.5
func (m *Backend) CheckAndSet(ctx context.Context, key, oldValue, newValue string, expiration time.Duration) (bool, error)
CheckAndSet atomically sets key to newValue only if current value matches oldValue. This operation provides compare-and-swap (CAS) semantics for implementing optimistic locking.
Parameters:
- ctx: Context for cancellation and timeouts
- key: The storage key to operate on
- oldValue: Expected current value. Use empty string "" for "set if not exists" semantics
- newValue: New value to set if the current value matches oldValue
- expiration: Time-to-live for the key. Use 0 for no expiration
Returns:
- bool: true if the CAS succeeded (value was set), false if the compare failed and no write occurred
- error: Any storage-related error (not including a compare mismatch)
Behavior:
- If oldValue is "", the operation succeeds only if the key does not exist (or is expired)
- If oldValue matches the current value, the key is updated to newValue
- Expired keys are treated as non-existent for comparison purposes
- All values are stored and compared as strings
Caller contract:
- A (false, nil) return indicates the compare condition did not match (e.g., another writer won the race or the key already exists when using "set if not exists"). This is not an error. Callers may safely reload state and retry with backoff according to their contention policy.
- A non-nil error indicates a storage/backend failure and should not be retried blindly.
func (*Backend) Cleanup ¶ added in v0.0.5
func (m *Backend) Cleanup()
Cleanup triggers an immediate cleanup of expired entries This method is exported for manual cleanup when needed
Click to show internal directories.
Click to hide internal directories.