You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apart from using all the caching mechanism that exist for Timber or WordPress, I’ve used a lot of memoization functions in custom Timber\Post methods recently. The gist is like this:
publicfunctionrelated_posts() {
return memoize( function() {
// Make some costly calculations or perform database queries.// …return$posts;
}, 'some_cache_key' );
}
With that I could do something like this in Twig without losing performance:
Now, the memoize() function could take many forms. And in a WordPress environment, there’s a lot of existing caching mechanisms like the WP Object Cache or Transients to work with.
WP Cache Remember is a set of caching functions that achieve this. The spatie/once package is another memoization function that has a little backtrace magic under the hood, because you don’t have to provide a cache key yourself.
Now I wonder: Should we have recommended way for memoization in Timber? Should we use an existing solution and add documentation about it? Should we build our own?
In Timber, we already have quite a few places where we use memoization, but always with a custom setup:
I wonder whether we could use a memoization function in these places, which would remove the amount of properties that we have and thus, maybe a little bit of complexity.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Apart from using all the caching mechanism that exist for Timber or WordPress, I’ve used a lot of memoization functions in custom
Timber\Post
methods recently. The gist is like this:With that I could do something like this in Twig without losing performance:
Now, the
memoize()
function could take many forms. And in a WordPress environment, there’s a lot of existing caching mechanisms like the WP Object Cache or Transients to work with.WP Cache Remember is a set of caching functions that achieve this. The spatie/once package is another memoization function that has a little backtrace magic under the hood, because you don’t have to provide a cache key yourself.
Now I wonder: Should we have recommended way for memoization in Timber? Should we use an existing solution and add documentation about it? Should we build our own?
In Timber, we already have quite a few places where we use memoization, but always with a custom setup:
Timber::context()
caches the global context variables.Timber\Post::css_class()
Timber\Post::type()
Timber\Post::link()
I wonder whether we could use a memoization function in these places, which would remove the amount of properties that we have and thus, maybe a little bit of complexity.
Beta Was this translation helpful? Give feedback.
All reactions