/src/Console/Cache.php

https://github.com/friendica/friendica · PHP · 210 lines · 150 code · 28 blank · 32 comment · 13 complexity · 21f2b61e79285dbebb9e10e1baca78ce MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2022, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Friendica\Console;
  22. use Asika\SimpleConsole\CommandArgsException;
  23. use Friendica\App;
  24. use Friendica\Core\Cache\Enum\Duration;
  25. use Friendica\Core\Cache\Capability\ICanCache;
  26. use RuntimeException;
  27. /**
  28. * tool to access the cache from the CLI
  29. *
  30. * With this script you can access the cache of your node from the CLI.
  31. * You can read current values stored in the cache and set new values
  32. * in cache keys.
  33. */
  34. class Cache extends \Asika\SimpleConsole\Console
  35. {
  36. protected $helpOptions = ['h', 'help', '?'];
  37. /**
  38. * @var App\Mode
  39. */
  40. private $appMode;
  41. /**
  42. * @var ICanCache
  43. */
  44. private $cache;
  45. protected function getHelp()
  46. {
  47. $help = <<<HELP
  48. console cache - Manage node cache
  49. Synopsis
  50. bin/console cache list [-h|--help|-?] [-v]
  51. bin/console cache get <key> [-h|--help|-?] [-v]
  52. bin/console cache set <key> <value> [-h|--help|-?] [-v]
  53. bin/console cache flush [-h|--help|-?] [-v]
  54. bin/console cache clear [-h|--help|-?] [-v]
  55. Description
  56. bin/console cache list [<prefix>]
  57. List all cache keys, optionally filtered by a prefix
  58. bin/console cache get <key>
  59. Shows the value of the provided cache key
  60. bin/console cache set <key> <value> [<ttl>]
  61. Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
  62. bin/console cache flush
  63. Clears expired cache keys
  64. bin/console cache clear
  65. Clears all cache keys
  66. Options
  67. -h|--help|-? Show help information
  68. -v Show more debug information.
  69. HELP;
  70. return $help;
  71. }
  72. public function __construct(App\Mode $appMode, ICanCache $cache, array $argv = null)
  73. {
  74. parent::__construct($argv);
  75. $this->appMode = $appMode;
  76. $this->cache = $cache;
  77. }
  78. protected function doExecute(): int
  79. {
  80. if ($this->getOption('v')) {
  81. $this->out('Executable: ' . $this->executable);
  82. $this->out('Class: ' . __CLASS__);
  83. $this->out('Arguments: ' . var_export($this->args, true));
  84. $this->out('Options: ' . var_export($this->options, true));
  85. }
  86. if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
  87. $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
  88. }
  89. if ($this->getOption('v')) {
  90. $this->out('Cache Driver Name: ' . $this->cache->getName());
  91. $this->out('Cache Driver Class: ' . get_class($this->cache));
  92. }
  93. switch ($this->getArgument(0)) {
  94. case 'list':
  95. $this->executeList();
  96. break;
  97. case 'get':
  98. $this->executeGet();
  99. break;
  100. case 'set':
  101. $this->executeSet();
  102. break;
  103. case 'flush':
  104. $this->executeFlush();
  105. break;
  106. case 'clear':
  107. $this->executeClear();
  108. break;
  109. }
  110. if (count($this->args) == 0) {
  111. $this->out($this->getHelp());
  112. return 0;
  113. }
  114. return 0;
  115. }
  116. private function executeList()
  117. {
  118. $prefix = $this->getArgument(1);
  119. $keys = $this->cache->getAllKeys($prefix);
  120. if (empty($prefix)) {
  121. $this->out('Listing all cache keys:');
  122. } else {
  123. $this->out('Listing all cache keys starting with "' . $prefix . '":');
  124. }
  125. $count = 0;
  126. foreach ($keys as $key) {
  127. $this->out($key);
  128. $count++;
  129. }
  130. $this->out($count . ' keys found');
  131. }
  132. private function executeGet()
  133. {
  134. if (count($this->args) >= 2) {
  135. $key = $this->getArgument(1);
  136. $value = $this->cache->get($key);
  137. $this->out("{$key} => " . var_export($value, true));
  138. } else {
  139. throw new CommandArgsException('Too few arguments for get');
  140. }
  141. }
  142. private function executeSet()
  143. {
  144. if (count($this->args) >= 3) {
  145. $key = $this->getArgument(1);
  146. $value = $this->getArgument(2);
  147. $duration = intval($this->getArgument(3, Duration::FIVE_MINUTES));
  148. if (is_array($this->cache->get($key))) {
  149. throw new RuntimeException("$key is an array and can't be set using this command.");
  150. }
  151. $result = $this->cache->set($key, $value, $duration);
  152. if ($result) {
  153. $this->out("{$key} <= " . $this->cache->get($key));
  154. } else {
  155. $this->out("Unable to set {$key}");
  156. }
  157. } else {
  158. throw new CommandArgsException('Too few arguments for set');
  159. }
  160. }
  161. private function executeFlush()
  162. {
  163. $result = $this->cache->clear();
  164. if ($result) {
  165. $this->out('Cache successfully flushed');
  166. } else {
  167. $this->out('Unable to flush the cache');
  168. }
  169. }
  170. private function executeClear()
  171. {
  172. $result = $this->cache->clear(false);
  173. if ($result) {
  174. $this->out('Cache successfully cleared');
  175. } else {
  176. $this->out('Unable to flush the cache');
  177. }
  178. }
  179. }