Problem/Motivation

It looks like the current cache clearing implementation in EntityCacheControllerHelper::resetEntityCache() is wrong. This could lead to unexpected behaviour if an explicit cache clear is requested.

The current code looks like this:

    // Reset the persistent cache.
    if (isset($ids)) {
      cache_clear_all($ids, 'cache_entity_' . $controller->entityType);
    }

If I understand that right the cache_clear_all is called only if $ids is set (and is not NULL).
According to DrupalEntityControllerInterface::resetCache() $ids can be NULL and in such case the whole cache has to be cleared.
This means EntityCacheControllerHelper::resetEntityCache() misses a handling for the case $ids is NULL.
Further cache_clear_all() doesn't seem to support an array as $cid - this means passing the $ids as parameter for $cid won't lead to the expected behaviour.

Proposed resolution

  • Add iteration over $ids to call cache_clear_all() - similar approach as this, never comitted one, here: #1050562: Does not support $reset argument
  • Add handling for the case $ids is NULL. I'm not sure yet what to use:
    • cache_clear_all(NULL, 'cache_entity_' . $controller->entityType); - deletes only expired items.
    • cache_clear_all('*', 'cache_entity_' . $controller->entityType, TRUE); - deletes all items and seems to work better, but that can be caused by the fact I used memcache to test this.

Remaining tasks

Review patch.
Decide which cache_clear_all() call should be used in the "$ids is NULL" case.

User interface changes

none

API changes

none

Comments

catch’s picture

Status: Needs review » Needs work

cache_clear_all() accepts $cids as an array in D7.

However otherwise this looks good - should definitely be '*' and not NULL passed to cache_clear_all().

das-peter’s picture

Status: Needs work » Needs review
StatusFileSize
new1.13 KB

Thanks for the feedback.
I must have missed somehow that arrays are accepted :|
However, here's the adjusted patch which checks if $ids is empty and takes the appropriate action.

catch’s picture

Status: Needs review » Fixed

Thanks! Committed/pushed to 7.x-1.x

Status: Fixed » Closed (fixed)

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

j0rd’s picture

Just encountered this causing problems with ApacheSolr module, which clears the cache via a node_load($nid, NULL, TRUE); I'm sure this functionality node_load with cache reset is used quite frequently in contrib and if someone has entitycache enabled, it will not work and cause problems.

Would it be possible to rollout a new release with this fix. It's been a while and I think this could be causing issues for people who don't know it.