#81461: Clean up the cache API (stop overloading function arguments, remove procedural wrappers) introduced a lovely new cache API.

this issue will track converting core to use it. as a general rule, simple conversions live here, but anything more complicated should be tracked in another issue.

Once the conversion is done, it should be possible to commit #1272706: Remove backwards compatibility layer for cache API.

Here's a list of places to make patches against. Note that modules may contain multiple files:

includes/form.inc -- Testing
includes/menu.inc -- partial conversion
modules/block/ -- Errors
modules/poll/ -- Errors
modules/update/
modules/locale/
modules/image/
modules/field/
modules/statistics/
modules/system/
modules/menu/
modules/book/

CommentFileSizeAuthor
#64 cache_conversion-1272686-64.patch47.11 KBAnonymous (not verified)
#63 cache_conversion-1272686-63.patch47.01 KBPaul Simard
#58 cache_conversion-1272686-57.patch46.77 KBbfroehle
#56 cache_conversion-54-remaining.txt3.78 KBbfroehle
#54 172686-New-Cache-API-54.patch23.8 KBPaul Simard
#52 172686_Rollup_New_Cache_API.patch23.81 KBPaul Simard
#51 module-image.cache-172686-51.patch2.39 KBPaul Simard
#49 module-field.cache-172686-49.patch3.13 KBPaul Simard
#47 module-image.cache-172686-47.patch2.38 KBPaul Simard
#46 module-field.cache-172686-46.patch3.13 KBPaul Simard
#45 module-system.cache-1726886-45.patch435 bytesPaul Simard
#44 module-menu.cache-172686-44.patch761 bytesPaul Simard
#41 path.inc_.cache-172686-41.patch906 bytesPaul Simard
#35 module-simpletest.cache-172686-35.patch1.07 KBPaul Simard
#34 module-poll.cache-172686-34.patch417 bytesPaul Simard
#34 module-user.cache-172686-34.patch1.21 KBPaul Simard
#34 module-filter.cache-172686-34.patch3.09 KBPaul Simard
#34 module-taxonomy.cache-172686-34.patch2.25 KBPaul Simard
#33 module-forum.cache-172686-33.patch591 bytesPaul Simard
#33 module-node.cache-172686-33.patch2.21 KBPaul Simard
#33 module-path.cache-172686-33.patch1007 bytesPaul Simard
#33 module-simpletest.cache-172686-33.patch1.58 KBPaul Simard
#32 module-block.cache-172686-32.patch2.43 KBPaul Simard
#29 module-comment.cache-172686-29.patch2.14 KBPaul Simard
#27 module-block.cache-172686-27.patch2.44 KBPaul Simard
#26 registry.inc_.cache-172686-26.patch832 bytesPaul Simard
#25 path.inc_.cache-172686-25.patch906 bytesPaul Simard
#24 module.inc_.cache-172686-24.patch3.95 KBPaul Simard
#22 form.inc_.cache-127686-22.patch2.23 KBPaul Simard
#21 menu.inc_.cache-127686-21.patch3.65 KBPaul Simard
#20 gettext.inc_.cache-1272686-20.patch464 bytesPaul Simard
#18 module.inc_.cache-172686-17.patch3.94 KBPaul Simard
#14 menu-form-gettext-module-inc.cache-1272686-9.patch3.09 KBPaul Simard
#9 menu-form-gettext-module-inc.cache-1272686-9.patch3.51 KBPaul Simard
#6 poll.cache-1272686-6.patch417 bytesPaul Simard
#4 poll.cache_-1272686-4.patch843 bytesPaul Simard
#3 aggregator.cache_-1272686-3.patch426 bytesPaul Simard
common.cache_.patch3.4 KBAnonymous (not verified)
theme.cache_.patch1.12 KBAnonymous (not verified)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

Issue tags: +Novice

adding novice task tag.

catch’s picture

Status: Needs review » Reviewed & tested by the community

Those two first patches are good to go. Thanks beejeebus!

catch’s picture

Issue summary: View changes

Updated issue summary.

catch’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

aggregator module patch -- 1st contribution.

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

FileSize
843 bytes

poll module patch

Possible error: duplicate of previous patch included, as well. oops!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, poll.cache_-1272686-4.patch, failed testing.

Paul Simard’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
417 bytes

poll module patch

Status: Reviewed & tested by the community » Needs work

The last submitted patch, poll.cache-1272686-6.patch, failed testing.

catch’s picture

Call to cache_clear_all() with no arguments need to stay the same for now, this is why poll is failing. That was bad grepping on my part, sorry!

Paul Simard’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
3.51 KB

Patches to following include files:
menu.inc
form.inc
gettext.inc
module.inc

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

Assigned: Unassigned » Paul Simard

Assumed assignment

Status: Reviewed & tested by the community » Needs work

The last submitted patch, menu-form-gettext-module-inc.cache-1272686-9.patch, failed testing.

catch’s picture

The conversions are reversed.

-  cache_clear_all('locale:', 'cache', TRUE);
+  cache('locale:')->delete('cache');

This should be:

-  cache_clear_all('locale:', 'cache', TRUE);
+ cache()->deletePrefix('locale:');
catch’s picture

-  cache_clear_all('bootstrap_modules', 'cache_bootstrap');
-  cache_clear_all('system_list', 'cache_bootstrap');
+  cache()->deleteMultiple(array('bootstrap_modules', 'system_list', 'cache_bootstrap'));

This is nearly there, could be:

cache('bootstrap')->deleteMultiple(array('system_list', 'bootstrap_modules'));
Paul Simard’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
3.09 KB

Fixes in [#12] & [#13] emplaced, I hope.

Status: Reviewed & tested by the community » Needs work
Issue tags: -Novice

The last submitted patch, menu-form-gettext-module-inc.cache-1272686-9.patch, failed testing.

Paul Simard’s picture

Status: Needs work » Needs review
Issue tags: +Novice
bfroehle’s picture

Status: Needs review » Needs work

As catch pointed out in #12, your argument ordering is incorrect.

Here is a rough conversion guide:

cache_get:
cache_get('foo', 'cache');
cache()->get('foo');
cache_get('foo', 'cache_bar');
cache('bar')->get('foo');
cache_set:
cache_set('foo', $data);
cache()->set('foo', $data);
cache_set('foo', $data, 'cache_bar');
cache('bar')->set('foo', $data);
cache_clear_all:
cache_clear_all('foo', 'cache');
cache()->delete('foo');
cache_clear_all('foo', 'cache', TRUE);
cache()->deletePrefix('foo');
cache_clear_all('foo', 'cache_bar');
cache('bar')->delete('foo');
cache_clear_all('foo', 'cache_bar', TRUE);
cache('bar')->deletePrefix('foo');
cache_clear_all('foo', 'cache_bar');
cache_clear_all('baz', 'cache_bar');
cache('bar')->deleteMultiple(array('foo', 'baz'));
Paul Simard’s picture

Status: Needs work » Needs review
FileSize
3.94 KB

module.inc stand-alone patch file.

I'm splitting the combined file submitted in #9 into individual patch files, 1 per file.

Paul Simard’s picture

Thanks for the assist. Spent some time this afternoon reviewing the changes in includes/cache.inc and concluded the same.

Paul Simard’s picture

gettext.inc stand-alone cache patch.

Paul Simard’s picture

menu.inc patch. Partial conversion completed.

Still need to convert cache_clear_all() occurrances where immediately followed by drupal_register_shutdown_function('cache_clear_all'...)

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

form.inc stand-alone cache patch.

Anonymous’s picture

Status: Needs review » Needs work

i see instances of this:

+  cache('bootstrap')->deleteMultiple('bootstrap_modules', 'system_list');

they need to be this instead:

+  cache('bootstrap')->deleteMultiple(array('bootstrap_modules', 'system_list'));
Anonymous’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

revised module.inc stand-alone cache patch.
Includes correction from beejeebus in previous comment.

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

path.inc stand-alone cache patch.

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

registry.inc stand-alone cache patch

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

module/block cache patch

Paul Simard’s picture

Status: Needs work » Needs review

patches submitted.

Paul Simard’s picture

modules/comment cache patch.

Anonymous’s picture

#26 has this:

+  cache()->flush()();

which is not valid php...

Paul Simard’s picture

Gag. Must've been that spot on my glasses. Will fix.

Paul Simard’s picture

revised module-block cache patch.

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

cache patches for:

module forum
module node
module simpletest
module path

Paul Simard’s picture

cache patches for:
module poll
module user
module filter
module taxonomy

Paul Simard’s picture

revised module simpletest cache patch

Paul Simard’s picture

Issue summary: View changes

Updated issue summary.

Paul Simard’s picture

Status: Needs review » Needs work

End of day progress for 9/8-9/9

Summary:
Successful patches: 15 (testing passed)
Failed patches: 2

module poll 3 failures
module block 4 failures

Will be back to finish up remaining modules this weekend.

Note:
Files excluded from conversions generally ended in .css, .info, .js extensions.
Files specifically checked for conversions generally ended in .inc, .php, .install, .module extensions.

bfroehle’s picture

Also a bunch of the patches incorrectly use cache()->deletePrefix(...) instead of cache()->delete(...). The deletePrefix method should be used only when the previous call was of the form cache_clear_all(..., ..., TRUE) (note the TRUE in the 3rd position).

Paul Simard’s picture

Ok. Will follow-up and re-craft. That brings up a question:

Rough conversion guide for cache_clear_all():

cache_clear_all('foo', 'cache');

        cache()->delete('foo');

cache_clear_all('foo', 'cache', TRUE);

        cache()->deletePrefix('foo');

cache_clear_all('foo', 'cache_bar');

        cache('bar')->delete('foo');

cache_clear_all('foo', 'cache_bar', TRUE);

       cache('bar')->deletePrefix('foo');

cache_clear_all('foo', 'cache_bar');
cache_clear_all('baz', 'cache_bar');

       cache('bar')->deleteMultiple(array('foo', 'baz'));

When in the form
cache_clear_all('*', cache_bar, TRUE); or cache_clear_all('*', cache, TRUE);
is the solution

       cache('bar')->deletePrefix('*');
       // or
       cache()->deletePrefix('*');

or

       cache('bar')->delete();
       // or
       cache()->delete();

or

       cache('bar')->flush();
       // or
       cache()->flush();

If more than one of those is correct, depending on the context of the code, could you clarify which case calls for which solution?

Thanks
Paul

Anonymous’s picture

re #38, when $cid == '*', and $wildcard == TRUE:

cache_clear_all('*', $bin, TRUE);

convert to this:

cache($bin)->flush();
Paul Simard’s picture

Got it. Thanks.

Paul Simard’s picture

path.inc revised cache patch

Paul Simard’s picture

Status: Needs work » Needs review
Paul Simard’s picture

Status: Needs review » Needs work
Paul Simard’s picture

Status: Needs work » Needs review
FileSize
761 bytes

module menu cache patch

Paul Simard’s picture

module system cache patch

Paul Simard’s picture

module field cache patch for testing

Paul Simard’s picture

module image cache patch

Status: Needs review » Needs work

The last submitted patch, module-image.cache-172686-47.patch, failed testing.

Paul Simard’s picture

repaired field module cache patch

Paul Simard’s picture

Status: Needs work » Needs review
Paul Simard’s picture

revised image module cache patch

Paul Simard’s picture

patch roll-up & testing (cache patches)

catch’s picture

Status: Needs review » Needs work

This is looking good, spotted a few cases where deletePrefix() should be delete() or vice versa, but otherwise very close.

+++ b/includes/form.incundefined
@@ -835,8 +835,8 @@ function drupal_process_form($form_id, &$form, &$form_state) {
+        cache('form')->deletePrefix('form_' . $form_state['values']['form_build_id']);
+        cache('form')->deletePrefix('form_state_' . $form_state['values']['form_build_id']);

These two can just be straight cache('form')->delete(....). No need for prefix since there was no TRUE argument to cache_clear_all().

+++ b/includes/gettext.incundefined
@@ -53,7 +53,7 @@ function _locale_import_po($file, $langcode, $mode) {
-  cache_clear_all('locale:', 'cache', TRUE);

This one however does need to be ->deletePrefix()

+++ b/modules/simpletest/simpletest.moduleundefined
@@ -421,7 +421,7 @@ function simpletest_clean_environment() {
-  cache_clear_all('simpletest', 'cache');

This can just be ->delete()

18 days to next Drupal core point release.

Paul Simard’s picture

Status: Needs work » Needs review
FileSize
23.8 KB

Final roll-up patch I think.

bfroehle’s picture

I manually reviewed the patch in #54 and did not notice any errors in the conversion.

Are we going for one big patch here or multiple little ones? In particular, there appear to still be many unchanged occurrences of cache_get, cache_set, and cache_clear_all.

bfroehle’s picture

I have attached a list of occurrences of cache_get, cache_set, and cache_clear_all after applying the patch in #54 to a clean 8.x checkout:

Paul Simard’s picture

I'll have to check with catch on that. Maybe he'll see this and give a holler. Will work on these in a couple of hours.

As far as the patch is concerned. I started with lots of little ones, and was told to roll them all up. So I did.

Thanks for the feedback.

bfroehle’s picture

Assigned: Paul Simard » Unassigned
FileSize
46.77 KB

I went ahead and fixed up some (but not all) of the remaining occurrences of the old API. For easier review, the attached patch has two big chunks, the first of which is #54 and the second which is new.

Still to be fixed:

./includes/common.inc:  if (!empty($cid) && $cache = cache_get($cid, $bin)) {
./includes/common.inc:  cache_set($cid, $data, $bin, $expire);
./includes/common.inc:    cache_clear_all('*', $table, TRUE);
./modules/simpletest/tests/cache.test:      cache_set($id, $this->default_value, $bin);
./modules/system/system.module:    cache_clear_all(NULL, $table);

I'm not sure what the conversion process is for $bin as a variable ... i.e. will passing $bin = 'cache_page' always still work or do we need to change the prior code to make sure that $bin is (eventually) just 'page'?

Paul Simard’s picture

Assigned: Unassigned » Paul Simard
Status: Needs review » Needs work
Paul Simard’s picture

Ok, you combined your changes into my patch. Which is fine. The only hitch is I'm still new at this and paranoid about screwing things up. The new patch is double in size from the one I've been working.

As I see it, I should:

  1. Download cache_conversion-1272686-57.patch
  2. git reset --hard
  3. git apply -v cache_conversion-1272686-57.patch

Is that right? BTW, I'm on IRC in #drupal-contribute if you need to contact directly. Query is fine.

Paul

Paul Simard’s picture

Also, from your last comment...

From ./includes/common.inc: cache_clear_all('*', $table, TRUE);

function drupal_flush_all_caches() {
  // ...
  // previous code omitted
  // ...
  // Ordered so clearing the page cache will always be the last action.
  $core = array('cache', 'cache_path', 'cache_filter', 'cache_bootstrap', 'cache_page');
  $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
  foreach ($cache_tables as $table) {
    cache_clear_all('*', $table, TRUE);  --old code
    cache($table)->flush();               // New code
  }
  // remainder of function follows
}

works for now.
The new code should execute like this:

{
  cache('cache')->flush();
  cache('path')->flush();
  cache('filter')->flush();
  cache('bootstrap')->flush();
  cache('page')->flush();
}

in succession using the new API. As Drupal does not require a cache to exist, this may result in added cache rebuilds, but the function is drupal_flush_all_caches().

It does pose an additional question, which is the module_invoke_all('flush_caches') call. As I read it, this adds to the array $cache_tables the function 'flush_caches' points to where it exists in the list of modules contained in $core.

Overall, I think it's safe here to use the cache($table)->flush().

Paul Simard’s picture

Regarding...

./includes/common.inc:  if (!empty($cid) && $cache = cache_get($cid, $bin)) {

$bin will resolve to either 'cache', or the value contained in $elements['#cache']['bin'] which is passed to the function as shown below:

  // ... previous stuff ...
  $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';

  if (!empty($cid) && $cache = cache_get($cid, $bin)) {
  // ... more stuff ...

The question is whether the argument passed to $elements will always be good within the function. If $elements['#cache']['bin'] doesn't have a value, the value resolves to 'cache', which is all good. The issue is that $elements['#cache']['bin'] always needs to contain a valid cache bin if the value is present. Sounds like something to be tested for...

This also applies to:
./includes/common.inc: cache_set($cid, $data, $bin, $expire);
where

  $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
  $expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : CACHE_PERMANENT;
  cache_set($cid, $data, $bin, $expire);

These seem to indicate that the same bin resolution applies, and items affected will persist unless $elements['#cache']['expire'] resolves to something else meaningful. Another thing to be tested, perhaps.

and for

./modules/simpletest/tests/cache.test:      cache_set($id, $this->default_value, $bin);

at least as far as resolving $bin is concerned.

I don't know simpletest enough to determine whether these are already being tested for or not, nor do I feel confident enough at this moment to write such a test if it can be written, but that's another issue.

Paul Simard’s picture

Status: Needs work » Needs review
FileSize
47.01 KB

Another roll-up.

It's almost done, whomever reviews, search for lines containing: "Proposed replacement:".

They are comments within the code, and there are only a few.

Any added feedback on whether I can make the replacements, please let me know. There are a couple of comments above #61 & #62 that outline my thoughts on the matter.

Anonymous’s picture

great work!

i've uploaded a new patch that resolves your suggestions.

they were all right except for the one in system cron, which should resolve to a cache->expire().

Paul Simard’s picture

Thanks. That's a few hours of work that's ready for the grinder of full review I think.

To beejeebus, chx, crell, and bfroehle ...

Thanks loads for your help and support.

catch’s picture

Status: Needs review » Reviewed & tested by the community

This is ready to go. Thanks folks!

Dries’s picture

Status: Reviewed & tested by the community » Fixed

This looks good. Thanks for all the work and the mentoring. Great to see. Committed to 8.x.

bfroehle’s picture

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Updated issue summary.