From 306b362b2978fbab5080ebb6eefecac0fcba34c9 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Fri, 31 Dec 2021 14:46:08 -0500 Subject: [PATCH 1/2] Remove Drush's cache API. We no longer need to cache annotation parsing. --- includes/cache.inc | 170 ---------------------------- src/Preflight/LegacyPreflight.php | 1 - src/Runtime/DependencyInjection.php | 6 - 3 files changed, 177 deletions(-) delete mode 100644 includes/cache.inc diff --git a/includes/cache.inc b/includes/cache.inc deleted file mode 100644 index 24fc468fef..0000000000 --- a/includes/cache.inc +++ /dev/null @@ -1,170 +0,0 @@ -get($cid); - $mess = $ret ? "HIT" : "MISS"; - Drush::logger()->debug(dt("Cache !mess cid: !cid", ['!mess' => $mess, '!cid' => $cid])); - return $ret; -} - -/** - * Return data from the persistent cache when given an array of cache IDs. - * - * @param array $cids - * An array of cache IDs for the data to retrieve. This is passed by - * reference, and will have the IDs successfully returned from cache removed. - * @param string $bin - * The cache bin where the data is stored. - * - * @return - * An array of the items successfully returned from cache indexed by cid. - */ -function drush_cache_get_multiple(array &$cids, $bin = 'default') { - return _drush_cache_get_object($bin)->getMultiple($cids); -} - -/** - * Store data in the persistent cache. - * - * @param string $cid - * The cache ID of the data to store. - * - * @param $data - * The data to store in the cache. - * @param string $bin - * The cache bin to store the data in. - * @param $expire - * One of the following values: - * - DRUSH_CACHE_PERMANENT: Indicates that the item should never be removed - * unless explicitly told to using drush_cache_clear_all() with a cache ID. - * - DRUSH_CACHE_TEMPORARY: Indicates that the item should be removed at - * the next general cache wipe. - * - A Unix timestamp: Indicates that the item should be kept at least until - * the given time, after which it behaves like DRUSH_CACHE_TEMPORARY. - * - * @return bool - */ -function drush_cache_set($cid, $data, $bin = 'default', $expire = DRUSH_CACHE_PERMANENT) { - if ($ret = _drush_cache_get_object($bin)->set($cid, $data, $expire)) { - Drush::logger()->debug(dt("Cache SET cid: !cid", ['!cid' => $cid])); - return $ret; - } -} - -/** - * Expire data from the cache. - * - * If called without arguments, expirable entries will be cleared from all known - * cache bins. - * - * @param string $cid - * If set, the cache ID to delete. Otherwise, all cache entries that can - * expire are deleted. - * @param string $bin - * If set, the bin $bin to delete from. Mandatory - * argument if $cid is set. - * @param bool $wildcard - * If $wildcard is TRUE, cache IDs starting with $cid are deleted in - * addition to the exact cache ID specified by $cid. If $wildcard is - * TRUE and $cid is '*' then the entire bin $bin is emptied. - */ -function drush_cache_clear_all($cid = NULL, $bin = 'default', $wildcard = FALSE) { - if (!isset($cid) && !isset($bin)) { - foreach (drush_cache_get_bins() as $bin) { - _drush_cache_get_object($bin)->clear(); - } - return; - } - return _drush_cache_get_object($bin)->clear($cid, $wildcard); -} - -/** - * Check if a cache bin is empty. - * - * A cache bin is considered empty if it does not contain any valid data for any - * cache ID. - * - * @param $bin - * The cache bin to check. - * - * @return - * TRUE if the cache bin specified is empty. - */ -function _drush_cache_is_empty($bin) { - return _drush_cache_get_object($bin)->isEmpty(); -} - -/** - * Return drush cache bins and any bins added by hook_drush_flush_caches(). - */ -function drush_cache_get_bins() { - $drush = ['default']; - return $drush; - // return array_merge(drush_command_invoke_all('drush_flush_caches'), $drush); -} diff --git a/src/Preflight/LegacyPreflight.php b/src/Preflight/LegacyPreflight.php index 05520d3296..6f062f6f7f 100644 --- a/src/Preflight/LegacyPreflight.php +++ b/src/Preflight/LegacyPreflight.php @@ -74,7 +74,6 @@ public static function includeCode($drushBasePath): void require_once $drushBasePath . '/includes/batch.inc'; require_once $drushBasePath . '/includes/drupal.inc'; require_once $drushBasePath . '/includes/output.inc'; - require_once $drushBasePath . '/includes/cache.inc'; require_once $drushBasePath . '/includes/filesystem.inc'; require_once $drushBasePath . '/includes/legacy.inc'; } diff --git a/src/Runtime/DependencyInjection.php b/src/Runtime/DependencyInjection.php index 384057c227..358e5bb4b7 100644 --- a/src/Runtime/DependencyInjection.php +++ b/src/Runtime/DependencyInjection.php @@ -153,15 +153,9 @@ protected function alterServicesForDrush($container, Application $application): $hookManager->addInitializeHook($container->get('bootstrap.hook')); $hookManager->addPreValidator($container->get('tildeExpansion.hook')); - // Install our command cache into the command factory - // TODO: Create class-based implementation of our cache management functions. - $cacheBackend = _drush_cache_get_object('factory'); - $commandCacheDataStore = new CommandCache($cacheBackend); - $factory = $container->get('commandFactory'); $factory->setIncludeAllPublicMethods(false); $factory->setIgnoreCommandsInTraits(true); - $factory->setDataStore($commandCacheDataStore); $factory->addCommandInfoAlterer(new DrushCommandInfoAlterer()); $commandProcessor = $container->get('commandProcessor'); From d92ba05f46cbe34544967bdad8b2963ef10b5e26 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Fri, 31 Dec 2021 15:05:58 -0500 Subject: [PATCH 2/2] More deletion --- src/Cache/CacheInterface.php | 114 ------------------ src/Cache/CommandCache.php | 64 ---------- src/Cache/FileCache.php | 175 ---------------------------- src/Cache/JSONCache.php | 32 ----- src/Commands/core/CacheCommands.php | 3 +- 5 files changed, 1 insertion(+), 387 deletions(-) delete mode 100644 src/Cache/CacheInterface.php delete mode 100644 src/Cache/CommandCache.php delete mode 100644 src/Cache/FileCache.php delete mode 100644 src/Cache/JSONCache.php diff --git a/src/Cache/CacheInterface.php b/src/Cache/CacheInterface.php deleted file mode 100644 index 5cf54469e9..0000000000 --- a/src/Cache/CacheInterface.php +++ /dev/null @@ -1,114 +0,0 @@ -cacheBackend = $cacheBackend; - } - - /** - * Test for an entry from the cache - * @param string $key - * @return boolean - */ - public function has($key) - { - $cacheItem = $this->cacheBackend->get($key); - return $this->valid($cacheItem); - } - /** - * Get an entry from the cache - * @param string $key - * @return array - */ - public function get($key) - { - $cacheItem = $this->cacheBackend->get($key); - if (!$this->valid($cacheItem)) { - return []; - } - // TODO: FileCache::get() should just return the - // data element, not the entire cacheItem. Then we - // could make it implement SimpleCacheInterface & do - // away with this adapter class. - return $cacheItem->data; - } - /** - * Store an entry in the cache - * @param string $key - * @param array $data - */ - public function set($key, $data) - { - $this->cacheBackend->set($key, $data); - } - - protected function valid($cacheItem) - { - return is_object($cacheItem) && isset($cacheItem->data); - } -} diff --git a/src/Cache/FileCache.php b/src/Cache/FileCache.php deleted file mode 100644 index 848fdd6f70..0000000000 --- a/src/Cache/FileCache.php +++ /dev/null @@ -1,175 +0,0 @@ -bin = $bin; - $this->directory = $this->cacheDirectory(); - } - - /** - * Returns the cache directory for the given bin. - * - * @param string $bin - */ - public function cacheDirectory($bin = null) - { - $bin = $bin ? $bin : $this->bin; - return Path::join(Drush::config()->cache(), $bin); - } - - public function get($cid) - { - $cids = [$cid]; - $cache = $this->getMultiple($cids); - return reset($cache); - } - - public function getMultiple(&$cids) - { - try { - $cache = []; - foreach ($cids as $cid) { - $filename = $this->getFilePath($cid); - if (!file_exists($filename)) { - return []; - } - - $item = $this->readFile($filename); - if ($item) { - $cache[$cid] = $item; - } - } - $cids = array_diff($cids, array_keys($cache)); - return $cache; - } catch (\Exception $e) { - return []; - } - } - - /** - * Returns the contents of the given filename unserialized. - * - * @param string $filename - * Absolute path to filename to read contents from. - */ - public function readFile($filename) - { - $item = file_get_contents($filename); - return $item ? unserialize($item) : false; - } - - public function set($cid, $data, $expire = DRUSH_CACHE_PERMANENT) - { - $created = time(); - - $cache = new \stdClass; - $cache->cid = $cid; - $cache->data = is_object($data) ? clone $data : $data; - $cache->created = $created; - if ($expire == DRUSH_CACHE_TEMPORARY) { - $cache->expire = $created + 2591999; - } elseif ($expire != DRUSH_CACHE_PERMANENT && $expire < 2592000) { - // Expire time is in seconds if less than 30 days, otherwise is a timestamp. - $cache->expire = $created + $expire; - } else { - $cache->expire = $expire; - } - - // Ensure the cache directory still exists, in case a backend process - // cleared the cache after the cache was initialized. - $fs = new Filesystem(); - $fs->mkdir($this->directory); - - $filename = $this->getFilePath($cid); - return $this->writeFile($filename, $cache); - } - - /** - * Serializes data and write it to the given filename. - * - * @param string $filename - * Absolute path to filename to write cache data. - * @param $cache - * Cache data to serialize and write to $filename. - */ - public function writeFile($filename, $cache) - { - return file_put_contents($filename, serialize($cache)); - } - - public function clear($cid = null, $wildcard = false) - { - $fs = new Filesystem(); - $bin_dir = $this->cacheDirectory(); - $files = []; - if (empty($cid)) { - $fs->remove($bin_dir); - } else { - if ($wildcard) { - if ($cid == '*') { - $fs->remove($bin_dir); - } else { - $files = Finder::create() - ->files() - ->name($cid) - ->in($bin_dir); - } - } else { - $files[] = $this->getFilePath($cid); - } - - $fs->remove($files); - } - } - - public function isEmpty() - { - $files = Finder::create() - ->files() - ->name() - ->exclude() - ->depth(0) - ->in($this->directory); - return empty($files); - } - - /** - * Converts a cache id to a full path. - * - * @param $cid - * The cache ID of the data to retrieve. - * - * @return - * The full path to the cache file. - */ - protected function getFilePath($cid) - { - return $this->directory . '/' . str_replace([':', '\\', '/'], '.', $cid) . self::EXTENSION; - } -} diff --git a/src/Cache/JSONCache.php b/src/Cache/JSONCache.php deleted file mode 100644 index 5f5c8ffd97..0000000000 --- a/src/Cache/JSONCache.php +++ /dev/null @@ -1,32 +0,0 @@ - and &, so we do it with str_replace(). - $json = str_replace(['<', '>', '&'], ['\u003c', '\u003e', '\u0026'], $json); - return file_put_contents($filename, $json); - } -} diff --git a/src/Commands/core/CacheCommands.php b/src/Commands/core/CacheCommands.php index 413ac5009c..b80548af59 100644 --- a/src/Commands/core/CacheCommands.php +++ b/src/Commands/core/CacheCommands.php @@ -300,8 +300,7 @@ public function getTypes($include_bootstrapped_types = false): array public static function clearDrush(): void { try { - drush_cache_clear_all(null, 'default');// No longer used by Drush core, but still cleared for backward compat. - drush_cache_clear_all(null, 'factory'); // command info from annotated-command library (i.e. parsed annotations) + // No longer anything to do here. Left because some commandfiles may try to call this. } catch (IOException $e) { // Sometimes another process writes files into a bin dir and \Drush\Cache\FileCache::clear fails. // That is not considered an error. https://github.com/drush-ops/drush/pull/4535.