If you are using Caching with groups, Cache::clearGroup will clear all the cache groups not just the one you are trying to clear

Now that I finally fixed the groupCaching, I run in to an other problem. I found that there are few times when I try to clear a group, but the group counter in the cache key wont change. But other counters that should not change is changing. I tried to debug this problem and found, that in Cake\Cache\Engine\MemcachedEngine there is a function groups(). In this function we get all the counters for the groups from the memcached, and generate and array with the groups and the counters together. We use this when we generate the cache keys. But unfortunately memcached in windows will NOT give back the result in the same order as we asked. It might be only windows memcached problem, i only tested this in windows. There is a ksort in the function, but only if the retrieved dataset count is not the same as the groupcount, so when we initalize the group counts.

public function groups(): array
    {
        if (empty($this->_compiledGroupNames)) {
            foreach ($this->_config['groups'] as $group) {
                $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
            }
        }

        $groups = $this->_Memcached->getMulti($this->_compiledGroupNames) ?: [];
        if (count($groups) !== count($this->_config['groups'])) {
            foreach ($this->_compiledGroupNames as $group) {
                if (!isset($groups[$group])) {
                    $this->_Memcached->set($group, 1, 0);
                    $groups[$group] = 1;
                }
            }
            ksort($groups);
        }

        $result = [];
        $groups = array_values($groups);
        foreach ($this->_config['groups'] as $i => $group) {
            $result[] = $group . $groups[$i];
        }

        return $result;
    }

Example of the error:

$this->_Memcached->getMulti($this->_compiledGroupNames)
[
 (int) 0 => 'data_cache_activedatasets',
 (int) 1 => 'data_cache_activeenvironments',
 (int) 2 => 'data_cache_activepermissions',
 (int) 3 => 'data_cache_activevpns',
 (int) 4 => 'data_cache_applicationenvironmentrels',
 (int) 5 => 'data_cache_applicationexecuterrels',
 (int) 6 => 'data_cache_applicationownerrels',
 (int) 7 => 'data_cache_applications',
 (int) 8 => 'data_cache_companies',
 (int) 9 => 'data_cache_datacategories',
 (int) 10 => 'data_cache_datasets',
 (int) 11 => 'data_cache_departments',
 (int) 12 => 'data_cache_emailtemplates',
 (int) 13 => 'data_cache_employees',
 (int) 14 => 'data_cache_environments',
 (int) 15 => 'data_cache_filters',
 (int) 16 => 'data_cache_jobtitles',
 (int) 17 => 'data_cache_logactions',
 ...
 (int) 86 => 'data_cache_workingdays',
 (int) 87 => 'data_cache_worksteps',
]
$this->_compiledGroupNames
[
 'data_cache_activedatasets' => (int) 1,
 'data_cache_activepermissions' => (int) 1,
 'data_cache_activevpns' => (int) 1,
 'data_cache_applicationownerrels' => (int) 1,
 'data_cache_applications' => (int) 1,
 'data_cache_datacategories' => (int) 1,
 'data_cache_departments' => (int) 1,
 'data_cache_environments' => (int) 3,
 'data_cache_logactions' => (int) 9,
 'data_cache_logapplicationenvironmentrels' => (int) 1,
 'data_cache_logapplicationexecuterrels' => (int) 1,
 'data_cache_logapplicationownerrels' => (int) 1,
 'data_cache_logcompanies' => (int) 1,
 'data_cache_logdatacategories' => (int) 1,
 'data_cache_logdatasets' => (int) 1,
 'data_cache_logdepartments' => (int) 1,
 'data_cache_logemailtemplates' => (int) 1,
 'data_cache_logoutsiders' => (int) 1,
 ...
 'data_cache_workflows' => (int) 5,
 'data_cache_worksteps' => (int) 5,
]

If I move ksort in the function outside of the initalize part, it will work fine. So the fixed function looks like this:

public function groups(): array
    {
        if (empty($this->_compiledGroupNames)) {
            foreach ($this->_config['groups'] as $group) {
                $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
            }
        }
        $groups = $this->_Memcached->getMulti($this->_compiledGroupNames) ?: [];
        if (count($groups) !== count($this->_config['groups'])) {
            foreach ($this->_compiledGroupNames as $group) {
                if (!isset($groups[$group])) {
                    $this->_Memcached->set($group, 1, 0);
                    $groups[$group] = 1;
                }
            }
        }
        ksort($groups);
        $result = [];
        $groups = array_values($groups);
        foreach ($this->_config['groups'] as $i => $group) {
            $result[] = $group . $groups[$i];
        }

        return $result;
    }

So now after some other fixing my CustomcacheEngine looks like this:

<?php
namespace App\Cache\Engine;

use Cake\Cache\Engine\AnyEngine;
use Cake\Datasource\ConnectionManager;

class CustomEngine extends AnyEngine
{
    public function init(array $config = []): bool
    {
        if ($config != 'debug_kit') {
            $groups = [];
            foreach (ConnectionManager::configured() as $configured) {
                if ($configured != 'debug_kit') {
                    $groups = array_merge($groups, ConnectionManager::get($configured)->getSchemaCollection()->listTables());
                }
            }
            $groups = array_unique($groups);
            sort($groups);
            $config['groups'] = $groups;
        
        }
        return parent::init($config);
        
    }
	
	public function groups(): array
    {
        if (empty($this->_compiledGroupNames)) {
            foreach ($this->_config['groups'] as $group) {
                $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
            }
        }
        $groups = $this->_Memcached->getMulti($this->_compiledGroupNames) ?: [];
        if (count($groups) !== count($this->_config['groups'])) {
            foreach ($this->_compiledGroupNames as $group) {
                if (!isset($groups[$group])) {
                    $this->_Memcached->set($group, 1, 0);
                    $groups[$group] = 1;
                }
            }
        }
        ksort($groups);
        $result = [];
        $groups = array_values($groups);
        foreach ($this->_config['groups'] as $i => $group) {
            $result[] = $group . $groups[$i];
        }

        return $result;
    }
    
    protected function _key(string $key): string
    {
        $this->ensureValidKey($key);

        $prefix = '';
        if ($this->_groupPrefix) {
            $prefix = md5($this->groups()[array_search(substr($key, 0, strpos($key, '_')), $this->_config['groups'])]);
        }
        $key = preg_replace('/[\s]+/', '_', $key);

        return $this->_config['prefix'] . $prefix . $key;
    }
}

I hope this helps to others having the same strange issues with groupCaching.