-
Notifications
You must be signed in to change notification settings - Fork 0
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
Support closures as values for ::set()
#1
Conversation
This allows us to `Memoize::set()` with a `Closure` as a value, resolving it when retrieving from memoization. If the key being retrieved is not itself a Closure, we will resolve the closures recursively from the key downwards. Because `wp_cache_set()` should not store closures, when using the `WpCacheDriver`, any value that is set as a `Closure` will be stored in memory until the Closure is resolved.
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.
Oh interesting! You took it a slightly different way than I'd meant, which is fine! I originally meant that the function was called upon setting, so the driver doesn't matter. What you've done here is even more clever and it effectively makes a dynamic cached value, as the closure is called upon retrieval.
Honestly, I'm trying to decide if this is extremely clever or a caching anti-pattern. 🤔
This is based on some great recommendations from @defunctl and allows the library to be much more versatile - particularly in projects where Dependency Injection Containers are in use.
Implementing a better OOP structure
@JasonTheAdams - tbh, resolving on set would be waaaay simpler and I think I see where you are going with it being a potential anti-pattern. I could see how a result could become unpredictable later based on the moment of resolution. |
I went ahead and went the resolve-on-set route for simplification's sake. |
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.
Looks good! 💪
This allows us to
Memoize::set()
with aClosure
as a value, resolving it before setting in the cache.Documentation Updates:
README.md
: Updated to include a table of contents and examples using the newMemoizer
class instead of the deprecatedMemoize
class. [1] [2] [3] [4]Codebase Simplification:
src/Memoize/Config.php
: Removed theConfig
class in favor of dependency injection for setting drivers and namespaces.src/Memoize/Contracts/DriverInterface.php
: Renamed and updated the namespace for consistency.src/Memoize/Contracts/MemoizerInterface.php
: Renamed and updated the namespace for consistency. [1] [2]New Features:
src/Memoize/Drivers/MemoryDriver.php
: Modified to support closures as cache values, resolving them before storing. [1] [2]src/Memoize/Memoizer.php
: Introduced a newMemoizer
class implementingMemoizerInterface
, replacing the staticMemoize
class.Testing Enhancements:
tests/unit/ClosureTest.php
: Added new tests to ensure that closures are correctly resolved and stored in the cache.