PHP-SugarNewsWikiDocsBugsForumFilesSource LoginRegister
HomeForumsDevelopmentFunction caching
Function caching
Development
Sean Middleditch Jul 27th, 2008 at 2:25pm
I added the ability to set a function as non-caching back into the codebase. I found that people tend to forget to put the nocache block around functions that can't be cached in most cases, and that just causes everyone headaches.

When calling $sugar->registerFunction(), the third parameter can be set to false to disable caching (if unspecified, it is true).

Note that this will only work for top-level function calls. If you call a function inside of an expression, it will be cached as normal. Changed this behavior is not trivial and I'm personally not interested in changing it, since I've found little need for it. Use nocache when you just have to make sure an expression is not cached.

Also note that the function parameters will be pre-computed and cached as well, unless the call is inside of a nocache block. I haven't myself once run into an instance where I needed the parameters cached.

Generally, the only two times I've actually found that I even needed no-cache functions were for template functions that either spit out a login bar (either an account and logout link if a user is logged in, or a login and signup link for guests) and for shopping cart output. In both cases the application design didn't require that the template pass a user or cart object to the function (they were globals, or properties of an application object global, or the session, etc.).

As a side note, I've changed how function lookup works. If you call registerFunction() with a second parameter, it now looks for a function called sugar_function_foo instead of just foo.

I've also added plugin support, so Sugar will look for plugin files called sugar_function_foo.php in the $sugar->pluginDir directory if necessary. I found that having to have a huge functions.inc.php and a ton of register calls got to be a drag.

If you want to set a function as non-cachable, you'll need to use $sugar->registerFunction().

Reply to Topic
or Login