getCache sometimes fails on Macs when using ddev

Created on 29 May 2023, about 1 year ago
Updated 31 May 2023, about 1 year ago

Problem/Motivation

While working on Mac's sometimes an error raises on the getCache method

Steps to reproduce

Use a Mac and use ddev on your local environment

Proposed resolution

Add staticTagsCache and the corresponding workaround to use on the required methods (getCache & doSetCache)

Remaining tasks

User interface changes

API changes

Data model changes

Hope this helps Matt, I'm going to try to create a merge request in order to make this work.

πŸ› Bug report
Status

Closed: duplicate

Version

1.0

Component

Code

Created by

πŸ‡ͺπŸ‡¨Ecuador aiglesiasn

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @aiglesiasn
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.3 & MariaDB 10.3.22
    last update about 1 year ago
    37 pass, 3 fail
  • @aiglesiasn opened merge request.
  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States mrweiner

    Chatting on slack but adding some notes in here just for future reference.

    /**
    * @var object
    */
    protected $staticTagsCache;

    We don't want to keep this "temp" cache on the slot object, because the same data needs to be accessible from every slot instance with the same ID. As in

    $slot_1 = $manager->openSlot('my_module', 'some_api', 'id');
    $slot_2 = $manager->openSlot('my_module', 'some_api', 'id');
    $slot_1->setCache('some_data');
    $slot_1->getCacheData == $slot_2->getCacheData(); // <-- this needs to be true, but will not be if you're returning data from a property on a specific slot.
    
    $this->staticTagsCache = &drupal_static(__FUNCTION__);

    This is problematic for a similar reason. Magic __FUNCTION__ is scoped to the class instance, and not the cache ID. We'd need to do like &drupal_static($cache_id); to ensure that the data is associated with the proper cache entry and not the name of the doSetCache function.

        if (empty($this->staticTagsCache)) {
          $this->staticTagsCache = (object) [ 'id' => $this->id, 'data' => $data, 'expire' => $expire, 'tags' => $tags];
        }
    

    We don't want to check whether it's empty. This should just mimic the regular setter in this method, setting the static value with the same data as the cache entry. We also need to make sure that the static variable is set such that it has the same properties as on $this->getCache() (data on the data property, etc). It needs to mimic a cache entry.

        $cache_item = $this->cache->get($this->id, $allow_invalid);
        if (!$cache_item) {
          if (!empty($this->staticTagsCache)) {
            $cache_item = $this->staticTagsCache;
          }
        }
        return $cache_item ?: NULL;
    

    If we always set a value in doSetCache, then these conditionals need to be handled differently, so something like.

    $static_cached = drupal_static($this->id);
    $cache_item = $this->cache->get($this->id, $allow_invalid);
    
    return $static_cached ?: $cache_item ?: NULL;
    

    Only snag is $allow_invalid -- I don't know how this would be handled by the static implementation.

  • Status changed to Closed: duplicate about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States mrweiner

    @aiglesiasn closing this as a duplicate. I've made some progress on this in the other ticket.

Production build 0.69.0 2024