Replace cache_name_function with NameFilter class#762
Replace cache_name_function with NameFilter class#762mblaney merged 6 commits intosimplepie:masterfrom
Conversation
jtojnar
left a comment
There was a problem hiding this comment.
I am not sure the added boilerplate is worth it for an obscure function like this.
Hopefully, PHP will eventually add support for generics so we will be able to annotate the argument with callable(string): string natively. For now, the combination of static analysis and runtime crashes (which will occur earlier once we enable strict type checking).
Actually, I am not even sure if SimplePie needs to make this customizable.
|
I think I get your point. Unfortunately, we cannot know, if and how this function is used, so at least we could add stricter rules. On the other hand I can see a legit use case for this function. Especially in view of the fact that we give the user the possibility to use his own PSR-16 cache. PSR-16 provides some rules for the cache key (only specific characters, not more than 64 chars, and so on), which are fulfilled with Examples: Use longer cache key $simplepie->set_cache_namefilter(new class () implements \SimplePie\Cache\NameFilter {
public function filter(string $name): string {
return hash('sha3-512', $name);
}
});Add prefix to cache key $simplepie->set_cache_namefilter(new class () implements \SimplePie\Cache\NameFilter {
public function filter(string $name): string {
return 'simplepie_' . hash('md5', $name);
}
});Use raw key and handle everything in the PSR-16 cache $simplepie->set_cache_namefilter(new class () implements \SimplePie\Cache\NameFilter {
public function filter(string $name): string {
return $name;
}
});In my opinion this function offers the best flexibility for all use cases. |
|
Ping @mblaney |
|
looks good but there's a conflict now from the other merges |
|
Thank you. I fixed the conflict. |
* bump version to 1.8.0 * Update CHANGELOG.md * Fix version tags in deprecated messages * fix version in old deprecation messages * Fix typo see comment from @jtojnar in #752 * Add comment for DataCache interface see comment from @jtojnar in #752 * Update CHANGELOG.md for #760, #764 and #765 * Update CHANGELOG.md for #762, #767 and #763 * Update CHANGELOG.md for #768 and #770 * Update release date * Update CHANGELOG.md for #769 and #771 * Update CHANGELOG.md for #766
It is possible to change the name of a cached value by providing a callable string via
SimplePie\SimplePie::set_cache_name_function(). The default function ismd5(). This gives us two problems:This PR adds a new interface
SimplePie\Cache\NameFilterthat will be used internally. Implementations can be set via the new methodSimplePie\SimplePie::set_cache_namefilter(). For BC reasons there is also a new classSimplePie\Cache\CallableNameFilter, that handles all kinds of callables (not just strings).This PR also deprecates the
SimplePie\SimplePie::set_cache_name_function()method.