-
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?
Changes from all commits
f6d972c
d4199fa
c41d36e
dc6471f
339460b
127f1e9
fa4b173
c59d808
2c75c18
a2a51bc
6879d13
fad4035
daff2a9
b4a002d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 If you need further assistance or specific updates, please let me know! Analysis chainScripts executedThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. @mktcode The rebase attempt with the
To resolve these conflicts, please follow these steps:
Please let me know if you need any further assistance! Analysis chainScripts executedThe 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { createHash } from 'crypto'; | ||
| import { Readable } from 'stream'; | ||
| import { set as setCache, get as getCache, clear as clearCache, isConfigured } from '../aws'; | ||
| import constants from '../constants.json'; | ||
| import { imageResolversCacheHitCount } from '../helpers/metrics'; | ||
| import { capture } from '@snapshot-labs/snapshot-sentry'; | ||
|
|
||
| export function sha256(str: string) { | ||
| return createHash('sha256') | ||
| .update(str) | ||
| .digest('hex'); | ||
| } | ||
|
|
||
| type ParamsType = { | ||
| type: string; | ||
| network: string; | ||
| address: string; | ||
| w: number; | ||
| h: number; | ||
| fallback?: string; | ||
| cb?: string; | ||
| }; | ||
|
|
||
| export default class Cache { | ||
| baseImageCacheKey: string; | ||
| resizedImageCacheKey: string; | ||
| isConfigured: boolean; | ||
|
|
||
| constructor({ type, network, address, w, h, fallback, cb }: ParamsType) { | ||
| const data = { type, network, address, w, h }; | ||
| if (fallback !== 'blockie') data['fallback'] = fallback; | ||
| if (cb) data['cb'] = cb; | ||
|
|
||
| const baseImageKey = this._buildKey({ ...data, w: constants.max, h: constants.max }); | ||
| const resizedImageKey = this._buildKey(data); | ||
|
|
||
| this.baseImageCacheKey = `${baseImageKey}/${baseImageKey}`; | ||
| this.resizedImageCacheKey = `${baseImageKey}/${resizedImageKey}`; | ||
| this.isConfigured = isConfigured; | ||
|
|
||
| if (!this.isConfigured) { | ||
| console.log('[cache:resolver] Cache is not configured'); | ||
| } | ||
| } | ||
|
|
||
| async getBaseImage(): Promise<Readable | boolean> { | ||
| return await this._getCache(this.baseImageCacheKey); | ||
| } | ||
|
|
||
| async getResizedImage(): Promise<Readable | boolean> { | ||
| return await this._getCache(this.resizedImageCacheKey); | ||
| } | ||
|
|
||
| async setBaseImage(value: Buffer) { | ||
| return await this._setCache(this.baseImageCacheKey, value); | ||
| } | ||
|
|
||
| async setResizedImage(value: Buffer) { | ||
| return await this._setCache(this.resizedImageCacheKey, value); | ||
| } | ||
|
|
||
| async clear(): Promise<boolean> { | ||
| if (!this.isConfigured) return false; | ||
|
|
||
| try { | ||
| const result = await clearCache(this.baseImageCacheKey); | ||
|
|
||
| console.log(`[cache:resolver] Cached cleared ${this.baseImageCacheKey}`); | ||
|
|
||
| return result; | ||
| } catch (e) { | ||
| console.log(`[cache:resolver] Failed to clear cache ${this.baseImageCacheKey}`); | ||
| capture(e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private async _getCache(key: string) { | ||
| if (!this.isConfigured) return false; | ||
|
|
||
| try { | ||
| console.log(`[cache:resolver] Getting cache ${key}`); | ||
| const cache = await getCache(key); | ||
|
|
||
| imageResolversCacheHitCount.inc({ status: cache ? 'HIT' : 'MISS' }, 1); | ||
|
|
||
| return cache; | ||
| } catch (e) { | ||
| capture(e); | ||
| console.log(`[cache:resolver] Failed to get cache ${key}`); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private async _setCache(key: string, value: Buffer) { | ||
| if (!this.isConfigured) return false; | ||
|
|
||
| try { | ||
| console.log(`[cache:resolver] Setting cache ${key}`); | ||
| return await setCache(key, value); | ||
| } catch (e) { | ||
| capture(e); | ||
| console.log(`[cache:resolver] Failed to set cache ${key}`); | ||
| } | ||
| } | ||
|
|
||
| private _buildKey(params: ParamsType): string { | ||
| return sha256(JSON.stringify(params)); | ||
| } | ||
| } |
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?