Skip to content

[LazyImage] Cache BlurHash, close #2 #1755

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 1 commit into from
Apr 19, 2024
Merged

[LazyImage] Cache BlurHash, close #2 #1755

merged 1 commit into from
Apr 19, 2024

Conversation

Kocal
Copy link
Member

@Kocal Kocal commented Apr 18, 2024

Q A
Bug fix? no
New feature? yes
Issues Fix #2
License MIT

Hi everyone, this PR is a proposal for #2, the second oldest issue on this repository 🤯

I've also though about decorates the BlurHashInterface if option cache is set, this way we don't modify the current BlurHash. Let me know what is preferred :)

@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Apr 18, 2024
@Kocal Kocal force-pushed the feat/GH-2 branch 4 times, most recently from 017dda6 to 74e49d9 Compare April 18, 2024 23:07

static::assertSame(
'L54ec*~q_3?bofoffQWB9F9FD%IU',
$cache->getItem(array_key_first($cache->getValues()))->get()
Copy link
Member Author

Choose a reason for hiding this comment

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

The generated cache key was different between my machine and the CI, so I'm using array_key_first($cache->getValues()) instead 🤷🏻

Copy link
Contributor

@norkunas norkunas Apr 19, 2024

Choose a reason for hiding this comment

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

Maybe would be enough to set framework.cache.prefix_seed to get stable namespaces/keys :)

Copy link
Member Author

Choose a reason for hiding this comment

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

To be more clear:

The different part is the result of hash('xxh3', '...') and is not related to the cache prefix seed 😕

And today we've learned, the namespace (generated through framework.cache.prefix) is not even used when adapter is an ArrayAdapter (https://github.com/symfony/cache/blob/7.0/DependencyInjection/CachePoolPass.php#L118-L119) :D

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah 😲

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, in fact it's super simple, since we compute the cache key with the filename, here __DIR__.'/../Fixtures/logo.png', paths are different between my machine and CI :)

But I we should keep this behaviour as is, I don't think we want to manipulate the filename (from public directory or not, from a URL or not, ...).

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

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

This is nice! I think I'd prefer as a decorator.

@Kocal
Copy link
Member Author

Kocal commented Apr 19, 2024

Rewritten as a decorator, tests have been adapted too

@Kocal Kocal requested a review from kbond April 19, 2024 06:58
@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Apr 19, 2024
@kbond
Copy link
Member

kbond commented Apr 19, 2024

Thanks Hugo, only 270 issues to go!

@kbond kbond merged commit 84e4e95 into symfony:2.x Apr 19, 2024
36 of 38 checks passed
@Kocal Kocal deleted the feat/GH-2 branch April 19, 2024 14:44
@Kocal
Copy link
Member Author

Kocal commented Apr 20, 2024

I just realized that the decorator CachedBlurHash is useless when you call data_uri_thumbnail (which seems to be the primary usage of this package):

  • CachedBlurHash#createDataUriThumbnail calls BlurHash#createDataUriThumbnail
  • BlurHash#createDataUriThumbnail calls BlurHash#encode, not CachedBlurHash#encode
  • The cache is only used when directly calling CachedBlurHash#encode

I'm working on it

kbond added a commit that referenced this pull request Apr 20, 2024
This PR was merged into the 2.x branch.

Discussion
----------

[LazyImage] Fix cache implementation

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Issues        | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT

<!--
Replace this notice by a description of your feature/bugfix.
This will help reviewers and should be a good start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - For new features, provide some code snippets to help understand usage.
 - Features and deprecations must be submitted against branch main.
 - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
 - Never break backward compatibility (see https://symfony.com/bc).
-->

Following #1755 (comment), `BlurHash#createDataUriThumbnail` could not use the cache because it call `$this->encode()`, which refers to `BlurHash#encode` and not `CachedBlurHash#encode`.

So I went back with my previous implementation by injecting (or not) a `CacheAdapter` implementation to `BlurHash`, and use it if necessary.

I've cheated a bit to adapt test, because I really want to ensure `$cache` is used or not.

Commits
-------

a619c6c [LazyImage] Fix cache implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LazyImage: cache blurhash
4 participants