8000 Add NewFromConnection method for Redis by sky93 · Pull Request #1680 · gofiber/storage · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add NewFromConnection method for Redis #1680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 26, 2025
Merged

Conversation

sky93
Copy link
Contributor
@sky93 sky93 commented Apr 18, 2025

Introduced NewFromConnection to create Storage using an existing Redis client, enhancing flexibility. Added benchmarks to test Redis operations with this method and updated README.

Summary by CodeRabbit

  • Documentation

    • Updated Redis storage driver documentation to include creating storage instances from existing Redis connections with usage examples.
  • New Features

    • Added support for initializing Redis storage directly from an existing Redis client connection.
  • Tests

    • Added tests verifying Redis storage operations using existing connections.

sky93 added 2 commits April 18, 2025 22:03
Introduced NewFromConnection to create Storage using an existing Redis client, enhancing flexibility. Added benchmarks to test Redis operations with this method.
Updated the Redis README to include the NewFromConnection function, which allows creating a Storage object from an existing Redis client connection.
@sky93 sky93 requested a review from a team as a code owner April 18, 2025 18:41
@sky93 sky93 requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team April 18, 2025 18:41
Copy link
Contributor
coderabbitai bot commented Apr 18, 2025

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • .github/workflows/test-redis.yml is excluded by !**/*.yml

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

A new constructor function, NewFromConnection, was introduced in the Redis storage driver, allowing the creation of a Storage instance from an existing redis.UniversalClient. The documentation was updated to reflect this addition, including an example demonstrating usage. A test function was added to verify the correctness of this new constructor by performing set, get, and delete operations on the Redis store.

Changes

File(s) Change Summary
redis/README.md Updated documentation to describe the new NewFromConnection(conn redis.UniversalClient) *Storage constructor with usage example.
redis/redis.go Added the NewFromConnection function for initializing Storage with an existing Redis client.
redis/redis_test.go Added Test_Redis_NewFromConnection to verify functionality of creating a store from an existing Redis connection.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant RedisUniversalClient
    participant Storage

    Client->>RedisUniversalClient: Create or obtain existing client
    Client->>Storage: Call NewFromConnection(RedisUniversalClient)
    Storage-->>Client: Returns Storage instance using provided client
Loading

Suggested labels

📒 Documentation

Suggested reviewers

  • sixcolors

Poem

In the warren where the data flows,
A new connection method grows.
From client made, the storage springs,
Set and get and delete it brings.
Docs now show this clever way,
For Redis bunnies, hip-hooray! 🐇✨


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anyw 8000 here in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sixcolors
Copy link
Member
sixcolors commented Apr 18, 2025

I would recommend that if we accept this P.R. for redis we implement it as a standard for other adapters.

@grivera64
Copy link
Member

I would recommend that if we accept this P.R. for redis we implement it as a standard for other adapters.

I agree, it would be nice to have this work with any db connection.

Since most db connections have a variety of differing methods, the only common method that all storage packages expect to use is storage.Close().

If we do decide to implement it as a standard, I think it would be good enough to allow passing in any struct that implements Close() as an argument through making some interface called storage.Conn or something along those lines.

@gaby
Copy link
Member
gaby commented Apr 18, 2025

This is related to #967 questions is, what do we do with all the other config options on each Storage driver?

But yes, this needs to be added to all the drivers.

@sky93
Copy link
Contributor Author
sky93 commented Apr 19, 2025

I appreciate you taking a look at my proposed changes!

While I understand there may be discussion around standardizing this approach across all databases, I believe accepting this pull request now would still provide clear benefits:

Flexibility for Redis Users
Allowing initialization from existing connections addresses a real problem—many developers already have configured Redis clients prepared, and reusing them reduces unnecessary coding. It's a straightforward enhancement that leaves current code and interfaces untouched.

A Potential Model for Other Drivers
As noted, establishing consistent handling of pre-made connections throughout various adapters could occur step-by-step. Accepting this would generate a tested example for Redis serving as a model for databases like MySQL, Postgres, and beyond without postponing relief for Redis developers.

Backwards-Compatible and Low-Risk
This addition doesn’t break existing usage: developers can still rely on the standard config-based constructor if they wish. The new method simply provides an alternate path for those who already have a Redis client instance.

Looking forward to any final thoughts or suggestions on next steps :)

@DanialV
Copy link
DanialV commented Apr 19, 2025

This feature is essential, since most applications already use Redis connections for various purposes, eliminating the need to create a new one for fiber storage.

redis/README.md Outdated
10000
@@ -21,6 +21,7 @@ A Redis storage driver using [go-redis/redis](https://github.com/go-redis/redis)
### Signatures
```go
func New(config ...Config) Storage
func NewFromConnection(conn redis.UniversalClient) Storage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls add a section with example and short description with the purpose of this new method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an example. Does it look good?

sky93 added 2 commits April 26, 2025 16:52
This commit adds a section to the README showing how to create a Storage instance from an existing Redis client, facilitating client connection reuse.
Implemented tests to verify Redis key management functions including set, get, and delete operations. Ensuring data integrity and operation correctness in Redis storage.
Copy link
Contributor Author
@sky93 sky93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added requested changes. Hope it helps make the code better.

redis/README.md Outdated
@@ -21,6 +21,7 @@ A Redis storage driver using [go-redis/redis](https://github.com/go-redis/redis)
### Signatures
```go
func New(config ...Config) Storage
func NewFromConnection(conn redis.UniversalClient) Storage
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an example. Does it look good?

@gaby gaby requested a review from Copilot April 26, 2025 14:29
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new method, NewFromConnection, to create a Redis Storage instance from an existing Redis connection, enhancing flexibility. It also updates benchmark tests and documentation accordingly.

  • Added benchmarks for Set, Delete, and Get operations using an existing connection.
  • Introduced NewFromConnection in the redis package.
  • Updated the README to document the new functionality.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
redis/redis_test.go Added benchmark and functional tests for the new connection-based creation method.
redis/redis.go Implemented NewFromConnection to wrap an existing Redis client into a Storage instance.
redis/README.md Updated documentation to include usage of NewFromConnection, with a noted signature mismatch.

gaby and others added 2 commits April 26, 2025 11:39
Co-authored-b
8000
y: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Member
@gaby gaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM

@ReneWerner87 ReneWerner87 merged commit a8327f0 into gofiber:main Apr 26, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0