-
Notifications
You must be signed in to change notification settings - Fork 18
refactor: extract cache logic into dedicated class #171
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
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files📢 Thoughts on this report? Let us know! |
| testPathIgnorePatterns: ['/node_modules/', '<rootDir>/dist/', '<rootDir>/test/fixtures/'], | ||
| moduleFileExtensions: ['js', 'ts'] | ||
| moduleFileExtensions: ['js', 'ts'], | ||
| verbose: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did that just sneak into the commit, or is it meant to stay changed?
|
@wa0x6e @bonustrack What about this PR? I tried to resolve the conflicts but it's not all that obvious. Will take some time. But anything that tidies up the main API endpoint will be good I think, for exactly the reasons why this one is now so hard to merge. |
|
Been half a year since this PR opened. May be better to start anew again. We are now using redis as cache, in addition to aws. So maybe have a new cache folder, with aws and redis file inside, instead of having addressResolvers/cache.ts and resolvers/cache.ts |
|
@coderabbitai full review |
Actions performedFull review triggered. |
WalkthroughThis update introduces significant enhancements to the caching mechanism and overall code organization within the application. Key changes include the introduction of a dedicated Changes
Sequence Diagram(s)sequenceDiagram
participant C as Client
participant A as API
participant Cache
participant S3 as AWS S3
C->>A: Request image with parameters
A->>Cache: Check for cached image
Cache-->>A: Return cached image or false
A->>S3: Fetch image if not cached
S3-->>A: Return image data
A->>Cache: Store image in cache
Cache-->>A: Confirm storage
A-->>C: Return image response
Assessment against linked issues
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
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 as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
|
@coderabbitai full review |
Actions performedFull review triggered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (7)
- jest.config.ts (1 hunks)
- src/api.ts (4 hunks)
- src/aws.ts (2 hunks)
- src/helpers/metrics.ts (1 hunks)
- src/resolvers/cache.ts (1 hunks)
- src/utils.ts (4 hunks)
- test/integration/resolvers/cache.test.ts (1 hunks)
Additional comments not posted (30)
jest.config.ts (1)
19-19: Confirm the necessity of the verbose setting.The addition of the
verbose: trueproperty enhances the verbosity of test output, which is useful for debugging. However, ensure that this change is intentional and not a leftover from debugging.src/helpers/metrics.ts (1)
35-39: Addition of imageResolversCacheHitCount is beneficial.The new
imageResolversCacheHitCountconstant enhances monitoring of the image resolvers cache layer. Ensure that it is consistently used alongside existing metrics.src/aws.ts (6)
4-4: Explicit typing for the AWS S3 client.The
clientvariable is now explicitly typed asAWS.S3, improving type safety and clarity.
11-13: Refined configuration logic for the S3 client.The S3 client is now initialized only if
isConfiguredis true, ensuring that the client is only created when necessary.
15-15: Improved type definition for streamToBuffer.The
streamToBufferfunction now includes a return type ofPromise<Buffer>, enhancing its type definition.
24-24: Improved type definition for set function.The
setfunction's signature now specifies the types of its parameters, promoting better usage and error prevention.
35-35: Improved type definition for clear function.The
clearfunction now includes a return type ofPromise<boolean>, signaling its asynchronous nature and expected return value.
50-50: Improved type definition for get function.The
getfunction now specifies that it returns aPromise<Readable | boolean>, enhancing the clarity of its return behavior.src/utils.ts (3)
72-72: LGTM!The inclusion of
typein the return object ofparseQueryenhances the output and provides more context for the caller.
1-1: Verify the removal ofgetCacheKeyfunction.Ensure that the
getCacheKeyfunction is not used elsewhere in the codebase.Verification successful
The
getCacheKeyfunction has been successfully removed and is not used elsewhere in the codebase.
- No occurrences of
getCacheKeywere found in the repository.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify if the `getCacheKey` function is used elsewhere in the codebase. # Test: Search for the function usage. Expect: No occurrences of `getCacheKey`. rg --type js -w 'getCacheKey'Length of output: 178
Script:
#!/bin/bash # Description: Verify if the `getCacheKey` function is used elsewhere in the codebase. # Test: Search for the function usage in JavaScript and TypeScript files. Expect: No occurrences of `getCacheKey`. rg -t js -t ts -w 'getCacheKey'Length of output: 31
1-1: Verify the removal ofsha256function.Ensure that the
sha256function is not used elsewhere in the codebase.Verification successful
The
sha256function was removed fromsrc/utils.tsbut is still present insrc/resolvers/cache.ts.
src/resolvers/cache.ts: Thesha256function is defined and used.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify if the `sha256` function is used elsewhere in the codebase. # Test: Search for the function usage. Expect: No occurrences of `sha256`. rg --type js -w 'sha256'Length of output: 173
Script:
#!/bin/bash # Description: Verify if the `sha256` function is used elsewhere in the codebase. # Test: Search for the function usage. Expect: No occurrences of `sha256`. rg -w 'sha256'Length of output: 1251
Script:
#!/bin/bash # Description: Extract the definition of the `sha256` function from `src/resolvers/cache.ts` and compare it with the removed function from `src/utils.ts`. # Extract the `sha256` function definition from `src/resolvers/cache.ts` rg -A 10 'export function sha256' src/resolvers/cache.tsLength of output: 253
Script:
#!/bin/bash # Description: Verify if the `sha256` function was removed from `src/utils.ts`. # Search for the definition of the `sha256` function in `src/utils.ts` rg 'function sha256' src/utils.tsLength of output: 35
src/resolvers/cache.ts (9)
8-12: LGTM!The
sha256function correctly generates a SHA-256 hash from a string using thecryptomodule.
14-22: LGTM!The
ParamsTypetype definition correctly defines the parameters for theCacheclass constructor.
24-44: LGTM!The
Cacheclass constructor correctly initializes the cache keys and handles the configuration check appropriately.
46-48: LGTM!The
getBaseImagemethod correctly retrieves the base image from the cache and handles errors appropriately.
50-52: LGTM!The
getResizedImagemethod correctly retrieves the resized image from the cache and handles errors appropriately.
54-56: LGTM!The
setBaseImagemethod correctly sets the base image in the cache and handles errors appropriately.
58-60: LGTM!The
setResizedImagemethod correctly sets the resized image in the cache and handles errors appropriately.
62-75: LGTM!The
clearmethod correctly clears the cache and handles errors appropriately.
78-109: LGTM!The private methods
_getCache,_setCache, and_buildKeycorrectly handle internal cache operations and key generation, and handle errors appropriately.src/api.ts (3)
Line range hint
7-24:
LGTM!The
router.postroute handler correctly handles different methods and returns appropriate responses.
38-44: LGTM!The
router.getroute handler for clearing cache correctly uses theCacheclass to clear the cache and handles errors appropriately.
Line range hint
55-107:
LGTM!The
router.getroute handler for retrieving images correctly uses theCacheclass to retrieve images, handles parsed parameters appropriately, and manages errors.test/integration/resolvers/cache.test.ts (7)
1-10: LGTM! Imports and initial setup are correct.The necessary modules and
Cacheclass are imported correctly. The sample image buffer setup is appropriate for the tests.
11-16: LGTM! Configuration check and initialization are correct.The configuration check ensures that the tests run only if the cache is configured, which is a good practice.
17-40: LGTM!getBaseImagetests are comprehensive.The tests cover scenarios for both cached and non-cached images, ensuring the method works correctly in both cases.
43-66: LGTM!getResizedImagetests are comprehensive.The tests cover scenarios for both cached and non-cached images, ensuring the method works correctly in both cases.
69-80: LGTM!setBaseImagetest is correct.The test verifies that the image is saved in the cache, ensuring the method works as expected.
82-93: LGTM!setResizedImagetest is correct.The test verifies that the image is saved in the cache, ensuring the method works as expected.
95-127: LGTM!cleartests are comprehensive.The tests cover scenarios for both existing and non-existing caches, ensuring the method works correctly in both cases.
|
@coderabbitai configuration |
CodeRabbit configurationlanguage: en-US
tone_instructions: ''
early_access: true
enable_free_tier: true
reviews:
profile: chill
request_changes_workflow: false
high_level_summary: true
high_level_summary_placeholder: '@coderabbitai summary'
auto_title_placeholder: '@coderabbitai'
review_status: true
poem: false
collapse_walkthrough: false
sequence_diagrams: true
path_filters: []
path_instructions: []
abort_on_close: true
auto_review:
enabled: true
auto_incremental_review: true
ignore_title_keywords: []
labels: []
drafts: false
base_branches: []
tools:
shellcheck:
enabled: true
ruff:
enabled: true
markdownlint:
enabled: true
github-checks:
enabled: true
timeout_ms: 90000
languagetool:
enabled: true
enabled_only: false
level: default
enabled_rules: []
disabled_rules: []
enabled_categories: []
disabled_categories: []
biome:
enabled: true
hadolint:
enabled: true
swiftlint:
enabled: true
phpstan:
enabled: true
level: default
golangci-lint:
enabled: true
yamllint:
enabled: true
gitleaks:
enabled: true
checkov:
enabled: true
detekt:
enabled: true
eslint:
enabled: true
ast-grep:
packages: []
rule_dirs: []
util_dirs: []
essential_rules: true
chat:
auto_reply: true
knowledge_base:
opt_out: false
learnings:
scope: auto
issues:
scope: auto
jira:
project_keys: []
linear:
team_keys: []
|
|
Tip For best results, initiate chat on the files or code changes. @mktcode: The merge resulted in numerous conflicts across multiple files, including If you need further assistance with specific conflicts, please let me know! Analysis chainScripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Fetch the latest changes from the base branch
git fetch origin main
# Check out the PR branch
git checkout -b pr-branch origin/pr/171
# Merge the latest changes from the base branch into the PR branch
git merge origin/main
Length of output: 264 Script: #!/bin/bash
# List all remote branches to identify the correct default branch and PR branch references
git branch -r
Length of output: 62 Script: #!/bin/bash
# Fetch the PR branch using the correct reference
git fetch origin pull/171/head:pr-branch
# Check out the PR branch
git checkout pr-branch
# Merge the latest changes from the master branch into the PR branch
git merge origin/master
Length of output: 263 Script: #!/bin/bash
# Fetch the PR branch using the correct reference
git fetch origin pull/171/head:pr-branch
# Check out the PR branch
git checkout pr-branch
# Merge the latest changes from the master branch into the PR branch with the --allow-unrelated-histories flag
git merge origin/master --allow-unrelated-histories
Length of output: 3538 Learnings added
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai can you resolve conflicts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mktcode It appears that there are no merge conflicts in the current branch. The working tree is clean, and no conflict markers were found. If you need to synchronize this branch with the latest changes from the master branch, you can update it by merging or rebasing with master.
If you need further assistance or specific updates, please let me know!
Analysis chain
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Fetch the conflicting files and the nature of conflicts
git fetch origin
git merge-base HEAD origin/main
git diff --name-only --diff-filter=U
Length of output: 134
Script:
#!/bin/bash
# List all remote branches to identify the correct main branch name
git branch -r
Length of output: 62
Script:
#!/bin/bash
# Fetch the conflicting files and the nature of conflicts using the master branch
git fetch origin
git merge-base HEAD origin/master
git diff --name-only --diff-filter=U
Length of output: 93
Script:
#!/bin/bash
# Check the status of the repository
git status
# Search for conflict markers in the files
git grep -n '<<<<<<< HEAD'
Length of output: 104
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai I'm afraid you're mistaken. GitHub is displaying conflicts rather than a merge button. Your conclusion is incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mktcode The rebase attempt with the master branch has revealed conflicts in the following files:
.gitignorepackage.jsonsrc/api.tssrc/index.tstsconfig.jsonyarn.lock
To resolve these conflicts, please follow these steps:
- Open each of the conflicting files and manually resolve the conflicts. Look for conflict markers (
<<<<<<<,=======,>>>>>>>) and decide which changes to keep. - Once you have resolved the conflicts, mark the files as resolved by running:
git add <conflicted_file>
- Continue the rebase process by running:
git rebase --continue
- If you encounter further conflicts, repeat the steps above. If you wish to abort the rebase process at any point, you can run:
git rebase --abort
Please let me know if you need any further assistance!
Analysis chain
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Fetch the conflicting files and the nature of conflicts using the master branch
git fetch origin
git merge-base HEAD origin/master
git diff --name-only --diff-filter=U
# Check the status of the repository
git status
# Search for conflict markers in the files
git grep -n '<<<<<<< HEAD'
Length of output: 198
Script:
#!/bin/bash
# Attempt to rebase the current branch with master to identify any conflicts
git fetch origin
git rebase origin/master
Length of output: 894
🧿 Current issues / What's wrong ?
The image resolver cache is tightly coupled inside express controller, making the controller fat and hard to follow. This can also leads to bug (such as cache not clearing properly), as there is no single module for cache handling
💊 Fixes / Solution
Extract the cache logic from express controller inside a dedicated
Cacheclass.This allow adding logging and metrics to the cache module, without complicating the express controller.
This is step 1 of a refactoring process, which should also resolve #119 after all refactoring PR will be done
🚧 Changes
Cacheclass🛠️ Tests
This is an internal refactoring, and does not change the end-user usage/behavior.
By adding other params such as size and cb will SET a new cache, then subsequent call to the same url should only trigger a single GET cache
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
Tests