Skip to content

📝 [v3 Proposal]: Improve Storage Interface #2839

Open
@efectn

Description

Feature Proposal Description

Idea One

Storage interface doesn't allow us to pass context to make operations cancellable. We should add new methods to allow context operations. Something like:

type Storage interface {
	Get(key string) ([]byte, error)
	Set(key string, val []byte, exp time.Duration) error
	Delete(key string) error
	Reset() error
	Close() error
        GetWithContext(ctx context.Context, key string) ([]byte, error)
	SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error
	DeleteWithContext(ctx context.Context, key string) error
	ResetWithContext(ctx context.Context) error
}

Idea Two

We can allow using generics to change key and value types.

type Storage[K ~comparable, V any] interface {
	// Get gets the value for the given key.
	// `nil, nil` is returned when the key does not exist
	Get(key K) (V, error)

	// Set stores the given value for the given key along
	// with an expiration value, 0 means no expiration.
	// Empty key or value will be ignored without an error.
	Set(key K, val V, exp time.Duration) error

	// Delete deletes the value for the given key.
	// It returns no error if the storage does not contain the key,
	Delete(key K) error

	// Reset resets the storage and delete all keys.
	Reset() error

	// Close closes the storage and will stop any running garbage
	// collectors and open connections.
	Close() error
}

Alignment with Express API

Express does not have storage interface.

HTTP RFC Standards Compliance

Not related to core.

API Stability

Not related to core.

Feature Examples

.

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have searched for existing issues that describe my proposal before opening this one.
  • I understand that a proposal that does not meet these guidelines may be closed without explanation.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status

      Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions