Keep something in cache even after flushing the cache from the Drupal back-end (not drush) I have a particular use case where we use a cache warmer to keep a custom resource in cache (/api/the-url/xxx). The cache warming is done with a bash script that is added to the crontab and runs at night.

For this particular use case I want /api/the-url/xxx to stay in cache even if admins flush the cache in the Drupal back-end.

For now I wrote 2 queries:

One query that moves all the /api/the-url/xxx from cached_data to cached_data_temp on flush cache.

/**
 * Copies xxx cache from "cache_data" to "cache_data_temp".
 * @param $cid
 */
public function copyCacheDataToTempDataCache($cid) {
    $select = $this-&gt;connection-&gt;select('cache_data', 'c');
    $select-&gt;fields('c');
    $select-&gt;condition('cid', $cid . '%', 'LIKE');
    $query = $this-&gt;connection-&gt;insert('cache_data_temp')-&gt;from($select);
    $query-&gt;execute();
}


One query to move all the /api/the-url/xxx from the cached_data_temp after the flush cache.

/**
 * Copies xxx cache from "cache_data_temp" to "cache_data".
 */
public function copyTempCacheToCacheData() {
    // Select query to fetch all rows.
    $select = $this-&gt;connection-&gt;select('cache_data_temp', 'c');
    $select-&gt;fields('c');
    // Insert rows into "cache_data".
    $query = $this-&gt;connection-&gt;insert('cache_data')-&gt;from($select);
    $query-&gt;execute();
    // Empty table.
    $this-&gt;connection-&gt;truncate('cache_data_temp')-&gt;execute();
 }


I've implemented hook_cache_flush which implements the following:

/**
 * Implements hook_cache_flush.
 */
function MY_MODULE_cache_flush() {
    // Before cache is cleared, get all CID's that start with route:api/the-url/%
    // And store it in "cache_data_temp" table.
    $manager = Drupal::service('xxx.manager');
    $manager-&gt;copyCacheDataToTempDataCache('route:api/the-url/');
}


And to rebuild the cache:

/**
 * Implements hook_rebuild.
 */
function MY_MODULE_rebuild() {
    // On info rebuild, put back all data stored in "cache_data_temp" in cache_data.
    $manager = Drupal::service('xxx.manager');
    $manager-&gt;copyTempCacheToCacheData();
}


Technically everything works fine. The data is kept in cache_data on flush (after moving it to the temp table) but it seems the cache is invalidated. What am I missing here?
For this use case, you might need to consider another storage instead of cache. 

warmer -> cache 

warmer -> storage -> cache

storage could be either key value or entity or any other storage you prefer.Check here if you are not sure which one to pick.

This way, your warmed data always persistent in the system and can be purged only by warmer (crontab). 

When cache clear happen, you can always fallback to storage.