A lot of functions in Drupal use static variables for performance caching.
Sometimes it is necessary to reset this cache during a request. The traditional way to do this was using reset parameters in the function with the static variable. The new and controversial way in D7 is to work with drupal_static(), which is basically a global registry, except that it doesn't pretend to be OOP.
Unfortunately, the function that is causing the reason for the reset (such as, changing values in the database), and the function with the static variable, might know nothing about each other. In this case, neither reset parameters nor drupal_static() do the job. And upon that, drupal_static was said to have performance problems.
-------
As an alternative to drupal_static() and reset parameters, I would like to propose a listener system, where functions can subscribe their static variables to named global events.
Benefits:
- You can request a reset without knowing about the listeners.
- Listeners can subscribe their events without knowing when they have to be reset.
- Lazy evaluation: Reset does not trigger the expensive calculations that we want to avoid.
- Extra function call overhead only on first run and after a reset. No overhead in the average repeated function call. Except for the string compare to 'first run', which can hopefully be done in a different way.
Example code is attached. It should run out of the box, if you rename it and copy it into a web-accessible location.
---------
Posted before in #581626-13: Use a consistent/clean pattern for using $reset or drupal_static(), and an older (not-working) version in #629452-6: Roll back drupal_static().
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | drupal_reset_listeners.d7.patch | 1.31 KB | donquixote |
| reset_listeners.php_.v2.txt | 1.65 KB | donquixote |
Comments
Comment #1
sunsubscribing
Very interesting.
Comment #2
effulgentsia commentedsubscribing
Comment #3
donquixote commentedI can make this a simple patch, that does nothing but add the function to bootstrap.inc.
Comment #4
donquixote commentedComment #5
David_Rothstein commentedSubscribing, but looks like this will have to be for Drupal 8 at this point.
I like the basic idea here a lot - unlike drupal_static(), which is basically just an ugly wrapper around normal PHP static variables :) The approach here would basically make the static variable system a lot more "Drupalish" since it doesn't require the function which is clearing the caches to know every other function that might be depending on it.
Comment #6
donquixote commentedCan't we just provide the function, and let contrib play with it? Then don't have to start at zero in D8. I hate waiting.
Otherwise I could make this thing a contrib module, but that would mean a dependency and an extra download for any module that uses it, for a very little piece of code.
Comment #7
klausitrailing white spaces
Comment #8
moshe weitzman commentedSymfony has a listener pattern which would work well here.
Comment #9
sunThis issue appears to be obsolete; #1577902: [META] Remove all usages of drupal_static() & drupal_static_reset()