@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Modularize "bin/cache" purgers

Summary: Ref T12859. This is an older command with a lot of hard-coded flags. Modularize cache purging in a modern way so it can be extended.

Test Plan: Ran `bin/cache purge --trace` with various valid and invalid flags.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T12859

Differential Revision: https://secure.phabricator.com/D18146

+160 -92
+10
src/__phutil_library_map__.php
··· 2171 2171 'PhabricatorCacheManagementPurgeWorkflow' => 'applications/cache/management/PhabricatorCacheManagementPurgeWorkflow.php', 2172 2172 'PhabricatorCacheManagementWorkflow' => 'applications/cache/management/PhabricatorCacheManagementWorkflow.php', 2173 2173 'PhabricatorCacheMarkupGarbageCollector' => 'applications/cache/garbagecollector/PhabricatorCacheMarkupGarbageCollector.php', 2174 + 'PhabricatorCachePurger' => 'applications/cache/purger/PhabricatorCachePurger.php', 2174 2175 'PhabricatorCacheSchemaSpec' => 'applications/cache/storage/PhabricatorCacheSchemaSpec.php', 2175 2176 'PhabricatorCacheSetupCheck' => 'applications/config/check/PhabricatorCacheSetupCheck.php', 2176 2177 'PhabricatorCacheSpec' => 'applications/cache/spec/PhabricatorCacheSpec.php', ··· 2320 2321 'PhabricatorCelerityApplication' => 'applications/celerity/application/PhabricatorCelerityApplication.php', 2321 2322 'PhabricatorCelerityTestCase' => '__tests__/PhabricatorCelerityTestCase.php', 2322 2323 'PhabricatorChangeParserTestCase' => 'applications/repository/worker/__tests__/PhabricatorChangeParserTestCase.php', 2324 + 'PhabricatorChangesetCachePurger' => 'applications/cache/purger/PhabricatorChangesetCachePurger.php', 2323 2325 'PhabricatorChangesetResponse' => 'infrastructure/diff/PhabricatorChangesetResponse.php', 2324 2326 'PhabricatorChatLogApplication' => 'applications/chatlog/application/PhabricatorChatLogApplication.php', 2325 2327 'PhabricatorChatLogChannel' => 'applications/chatlog/storage/PhabricatorChatLogChannel.php', ··· 2930 2932 'PhabricatorGarbageCollectorManagementCollectWorkflow' => 'infrastructure/daemon/garbagecollector/management/PhabricatorGarbageCollectorManagementCollectWorkflow.php', 2931 2933 'PhabricatorGarbageCollectorManagementSetPolicyWorkflow' => 'infrastructure/daemon/garbagecollector/management/PhabricatorGarbageCollectorManagementSetPolicyWorkflow.php', 2932 2934 'PhabricatorGarbageCollectorManagementWorkflow' => 'infrastructure/daemon/garbagecollector/management/PhabricatorGarbageCollectorManagementWorkflow.php', 2935 + 'PhabricatorGeneralCachePurger' => 'applications/cache/purger/PhabricatorGeneralCachePurger.php', 2933 2936 'PhabricatorGestureUIExample' => 'applications/uiexample/examples/PhabricatorGestureUIExample.php', 2934 2937 'PhabricatorGitGraphStream' => 'applications/repository/daemon/PhabricatorGitGraphStream.php', 2935 2938 'PhabricatorGitHubAuthProvider' => 'applications/auth/provider/PhabricatorGitHubAuthProvider.php', ··· 3748 3751 'PhabricatorRegistrationProfile' => 'applications/people/storage/PhabricatorRegistrationProfile.php', 3749 3752 'PhabricatorReleephApplication' => 'applications/releeph/application/PhabricatorReleephApplication.php', 3750 3753 'PhabricatorReleephApplicationConfigOptions' => 'applications/releeph/config/PhabricatorReleephApplicationConfigOptions.php', 3754 + 'PhabricatorRemarkupCachePurger' => 'applications/cache/purger/PhabricatorRemarkupCachePurger.php', 3751 3755 'PhabricatorRemarkupControl' => 'view/form/control/PhabricatorRemarkupControl.php', 3752 3756 'PhabricatorRemarkupCowsayBlockInterpreter' => 'infrastructure/markup/interpreter/PhabricatorRemarkupCowsayBlockInterpreter.php', 3753 3757 'PhabricatorRemarkupCustomBlockRule' => 'infrastructure/markup/rule/PhabricatorRemarkupCustomBlockRule.php', ··· 4202 4206 'PhabricatorUserBadgesCacheType' => 'applications/people/cache/PhabricatorUserBadgesCacheType.php', 4203 4207 'PhabricatorUserBlurbField' => 'applications/people/customfield/PhabricatorUserBlurbField.php', 4204 4208 'PhabricatorUserCache' => 'applications/people/storage/PhabricatorUserCache.php', 4209 + 'PhabricatorUserCachePurger' => 'applications/cache/purger/PhabricatorUserCachePurger.php', 4205 4210 'PhabricatorUserCacheType' => 'applications/people/cache/PhabricatorUserCacheType.php', 4206 4211 'PhabricatorUserCardView' => 'applications/people/view/PhabricatorUserCardView.php', 4207 4212 'PhabricatorUserConfigOptions' => 'applications/people/config/PhabricatorUserConfigOptions.php', ··· 7366 7371 'PhabricatorCacheManagementPurgeWorkflow' => 'PhabricatorCacheManagementWorkflow', 7367 7372 'PhabricatorCacheManagementWorkflow' => 'PhabricatorManagementWorkflow', 7368 7373 'PhabricatorCacheMarkupGarbageCollector' => 'PhabricatorGarbageCollector', 7374 + 'PhabricatorCachePurger' => 'Phobject', 7369 7375 'PhabricatorCacheSchemaSpec' => 'PhabricatorConfigSchemaSpec', 7370 7376 'PhabricatorCacheSetupCheck' => 'PhabricatorSetupCheck', 7371 7377 'PhabricatorCacheSpec' => 'Phobject', ··· 7551 7557 'PhabricatorCelerityApplication' => 'PhabricatorApplication', 7552 7558 'PhabricatorCelerityTestCase' => 'PhabricatorTestCase', 7553 7559 'PhabricatorChangeParserTestCase' => 'PhabricatorWorkingCopyTestCase', 7560 + 'PhabricatorChangesetCachePurger' => 'PhabricatorCachePurger', 7554 7561 'PhabricatorChangesetResponse' => 'AphrontProxyResponse', 7555 7562 'PhabricatorChatLogApplication' => 'PhabricatorApplication', 7556 7563 'PhabricatorChatLogChannel' => array( ··· 8253 8260 'PhabricatorGarbageCollectorManagementCollectWorkflow' => 'PhabricatorGarbageCollectorManagementWorkflow', 8254 8261 'PhabricatorGarbageCollectorManagementSetPolicyWorkflow' => 'PhabricatorGarbageCollectorManagementWorkflow', 8255 8262 'PhabricatorGarbageCollectorManagementWorkflow' => 'PhabricatorManagementWorkflow', 8263 + 'PhabricatorGeneralCachePurger' => 'PhabricatorCachePurger', 8256 8264 'PhabricatorGestureUIExample' => 'PhabricatorUIExample', 8257 8265 'PhabricatorGitGraphStream' => 'PhabricatorRepositoryGraphStream', 8258 8266 'PhabricatorGitHubAuthProvider' => 'PhabricatorOAuth2AuthProvider', ··· 9204 9212 'PhabricatorRegistrationProfile' => 'Phobject', 9205 9213 'PhabricatorReleephApplication' => 'PhabricatorApplication', 9206 9214 'PhabricatorReleephApplicationConfigOptions' => 'PhabricatorApplicationConfigOptions', 9215 + 'PhabricatorRemarkupCachePurger' => 'PhabricatorCachePurger', 9207 9216 'PhabricatorRemarkupControl' => 'AphrontFormTextAreaControl', 9208 9217 'PhabricatorRemarkupCowsayBlockInterpreter' => 'PhutilRemarkupBlockInterpreter', 9209 9218 'PhabricatorRemarkupCustomBlockRule' => 'PhutilRemarkupBlockRule', ··· 9756 9765 'PhabricatorUserBadgesCacheType' => 'PhabricatorUserCacheType', 9757 9766 'PhabricatorUserBlurbField' => 'PhabricatorUserCustomField', 9758 9767 'PhabricatorUserCache' => 'PhabricatorUserDAO', 9768 + 'PhabricatorUserCachePurger' => 'PhabricatorCachePurger', 9759 9769 'PhabricatorUserCacheType' => 'Phobject', 9760 9770 'PhabricatorUserCardView' => 'AphrontTagView', 9761 9771 'PhabricatorUserConfigOptions' => 'PhabricatorApplicationConfigOptions',
+48 -92
src/applications/cache/management/PhabricatorCacheManagementPurgeWorkflow.php
··· 6 6 protected function didConstruct() { 7 7 $this 8 8 ->setName('purge') 9 - ->setSynopsis(pht('Drop data from caches. APC-based caches can be '. 10 - 'purged from the web interface.')) 9 + ->setSynopsis(pht('Drop data from readthrough caches.')) 11 10 ->setArguments( 12 11 array( 13 12 array( 14 - 'name' => 'purge-all', 15 - 'help' => pht('Purge all caches.'), 16 - ), 17 - array( 18 - 'name' => 'purge-remarkup', 19 - 'help' => pht('Purge the remarkup cache.'), 13 + 'name' => 'all', 14 + 'help' => pht('Purge all caches.'), 20 15 ), 21 16 array( 22 - 'name' => 'purge-changeset', 23 - 'help' => pht('Purge the Differential changeset cache.'), 24 - ), 25 - array( 26 - 'name' => 'purge-general', 27 - 'help' => pht('Purge the general cache.'), 28 - ), 29 - array( 30 - 'name' => 'purge-user', 31 - 'help' => pht('Purge the user cache.'), 17 + 'name' => 'caches', 18 + 'param' => 'keys', 19 + 'help' => pht('Purge a specific set of caches.'), 32 20 ), 33 21 )); 34 22 } 35 23 36 24 public function execute(PhutilArgumentParser $args) { 37 - $console = PhutilConsole::getConsole(); 38 - 39 - $purge_all = $args->getArg('purge-all'); 40 - 41 - $purge = array( 42 - 'remarkup' => $purge_all || $args->getArg('purge-remarkup'), 43 - 'changeset' => $purge_all || $args->getArg('purge-changeset'), 44 - 'general' => $purge_all || $args->getArg('purge-general'), 45 - 'user' => $purge_all || $args->getArg('purge-user'), 46 - ); 25 + $all_purgers = PhabricatorCachePurger::getAllPurgers(); 47 26 48 - if (!array_filter($purge)) { 49 - $list = array(); 50 - foreach ($purge as $key => $ignored) { 51 - $list[] = "'--purge-".$key."'"; 52 - } 27 + $is_all = $args->getArg('all'); 28 + $key_list = $args->getArg('caches'); 53 29 30 + if ($is_all && strlen($key_list)) { 54 31 throw new PhutilArgumentUsageException( 55 32 pht( 56 - "Specify which cache or caches to purge, or use '%s'. Available ". 57 - "caches are: %s. Use '%s' for more information.", 58 - '--purge-all', 59 - implode(', ', $list), 60 - '--help')); 33 + 'Specify either "--all" or "--caches", not both.')); 34 + } else if (!$is_all && !strlen($key_list)) { 35 + throw new PhutilArgumentUsageException( 36 + pht( 37 + 'Select caches to purge with "--all" or "--caches". Available '. 38 + 'caches are: %s.', 39 + implode(', ', array_keys($all_purgers)))); 61 40 } 62 41 63 - if ($purge['remarkup']) { 64 - $console->writeOut(pht('Purging remarkup cache...')); 65 - $this->purgeRemarkupCache(); 66 - $console->writeOut("%s\n", pht('Done.')); 67 - } 68 - 69 - if ($purge['changeset']) { 70 - $console->writeOut(pht('Purging changeset cache...')); 71 - $this->purgeChangesetCache(); 72 - $console->writeOut("%s\n", pht('Done.')); 42 + if ($is_all) { 43 + $purgers = $all_purgers; 44 + } else { 45 + $key_list = preg_split('/[\s,]+/', $key_list); 46 + $purgers = array(); 47 + foreach ($key_list as $key) { 48 + if (isset($all_purgers[$key])) { 49 + $purgers[$key] = $all_purgers[$key]; 50 + } else { 51 + throw new PhutilArgumentUsageException( 52 + pht( 53 + 'Cache purger "%s" is not recognized. Available caches '. 54 + 'are: %s.', 55 + $key, 56 + implode(', ', array_keys($all_purgers)))); 57 + } 58 + } 59 + if (!$purgers) { 60 + throw new PhutilArgumentUsageException( 61 + pht( 62 + 'When using "--caches", you must select at least one valid '. 63 + 'cache to purge.')); 64 + } 73 65 } 74 66 75 - if ($purge['general']) { 76 - $console->writeOut(pht('Purging general cache...')); 77 - $this->purgeGeneralCache(); 78 - $console->writeOut("%s\n", pht('Done.')); 79 - } 67 + foreach ($purgers as $key => $purger) { 68 + echo tsprintf( 69 + "%s\n", 70 + pht( 71 + 'Purging "%s" cache...', 72 + $key)); 80 73 81 - if ($purge['user']) { 82 - $console->writeOut(pht('Purging user cache...')); 83 - $this->purgeUserCache(); 84 - $console->writeOut("%s\n", pht('Done.')); 74 + $purger->purgeCache(); 85 75 } 86 - } 87 76 88 - private function purgeRemarkupCache() { 89 - $conn_w = id(new PhabricatorMarkupCache())->establishConnection('w'); 90 - 91 - queryfx( 92 - $conn_w, 93 - 'TRUNCATE TABLE %T', 94 - id(new PhabricatorMarkupCache())->getTableName()); 95 - } 96 - 97 - private function purgeChangesetCache() { 98 - $conn_w = id(new DifferentialChangeset())->establishConnection('w'); 99 - queryfx( 100 - $conn_w, 101 - 'TRUNCATE TABLE %T', 102 - DifferentialChangeset::TABLE_CACHE); 103 - } 104 - 105 - private function purgeGeneralCache() { 106 - $conn_w = id(new PhabricatorMarkupCache())->establishConnection('w'); 107 - 108 - queryfx( 109 - $conn_w, 110 - 'TRUNCATE TABLE %T', 111 - 'cache_general'); 112 - } 113 - 114 - private function purgeUserCache() { 115 - $table = new PhabricatorUserCache(); 116 - $conn_w = $table->establishConnection('w'); 117 - 118 - queryfx( 119 - $conn_w, 120 - 'TRUNCATE TABLE %T', 121 - $table->getTableName()); 77 + return 0; 122 78 } 123 79 124 80 }
+30
src/applications/cache/purger/PhabricatorCachePurger.php
··· 1 + <?php 2 + 3 + abstract class PhabricatorCachePurger 4 + extends Phobject { 5 + 6 + private $viewer; 7 + 8 + abstract public function purgeCache(); 9 + 10 + final public function setViewer(PhabricatorUser $viewer) { 11 + $this->viewer = $viewer; 12 + return $this; 13 + } 14 + 15 + final public function getViewer() { 16 + return $this->viewer; 17 + } 18 + 19 + final public function getPurgerKey() { 20 + return $this->getPhobjectClassConstant('PURGERKEY'); 21 + } 22 + 23 + final public static function getAllPurgers() { 24 + return id(new PhutilClassMapQuery()) 25 + ->setAncestorClass(__CLASS__) 26 + ->setUniqueMethod('getPurgerKey') 27 + ->execute(); 28 + } 29 + 30 + }
+18
src/applications/cache/purger/PhabricatorChangesetCachePurger.php
··· 1 + <?php 2 + 3 + final class PhabricatorChangesetCachePurger 4 + extends PhabricatorCachePurger { 5 + 6 + const PURGERKEY = 'changeset'; 7 + 8 + public function purgeCache() { 9 + $table = new DifferentialChangeset(); 10 + $conn = $table->establishConnection('w'); 11 + 12 + queryfx( 13 + $conn, 14 + 'TRUNCATE TABLE %T', 15 + DifferentialChangeset::TABLE_CACHE); 16 + } 17 + 18 + }
+18
src/applications/cache/purger/PhabricatorGeneralCachePurger.php
··· 1 + <?php 2 + 3 + final class PhabricatorGeneralCachePurger 4 + extends PhabricatorCachePurger { 5 + 6 + const PURGERKEY = 'general'; 7 + 8 + public function purgeCache() { 9 + $table = new PhabricatorMarkupCache(); 10 + $conn = $table->establishConnection('w'); 11 + 12 + queryfx( 13 + $conn, 14 + 'TRUNCATE TABLE %T', 15 + 'cache_general'); 16 + } 17 + 18 + }
+18
src/applications/cache/purger/PhabricatorRemarkupCachePurger.php
··· 1 + <?php 2 + 3 + final class PhabricatorRemarkupCachePurger 4 + extends PhabricatorCachePurger { 5 + 6 + const PURGERKEY = 'remarkup'; 7 + 8 + public function purgeCache() { 9 + $table = new PhabricatorMarkupCache(); 10 + $conn = $table->establishConnection('w'); 11 + 12 + queryfx( 13 + $conn, 14 + 'TRUNCATE TABLE %T', 15 + $table->getTableName()); 16 + } 17 + 18 + }
+18
src/applications/cache/purger/PhabricatorUserCachePurger.php
··· 1 + <?php 2 + 3 + final class PhabricatorUserCachePurger 4 + extends PhabricatorCachePurger { 5 + 6 + const PURGERKEY = 'user'; 7 + 8 + public function purgeCache() { 9 + $table = new PhabricatorUserCache(); 10 + $conn = $table->establishConnection('w'); 11 + 12 + queryfx( 13 + $conn, 14 + 'TRUNCATE TABLE %T', 15 + $table->getTableName()); 16 + } 17 + 18 + }