Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cache.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Friendica\Core\Console;
  3. use Asika\SimpleConsole\CommandArgsException;
  4. use Friendica\App;
  5. use Friendica\Core;
  6. use RuntimeException;
  7. /**
  8. * @brief tool to access the cache from the CLI
  9. *
  10. * With this script you can access the cache of your node from the CLI.
  11. * You can read current values stored in the cache and set new values
  12. * in cache keys.
  13. *
  14. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  15. */
  16. class Cache extends \Asika\SimpleConsole\Console
  17. {
  18. protected $helpOptions = ['h', 'help', '?'];
  19. protected function getHelp()
  20. {
  21. $help = <<<HELP
  22. console cache - Manage node cache
  23. Synopsis
  24. bin/console cache list [-h|--help|-?] [-v]
  25. bin/console cache get <key> [-h|--help|-?] [-v]
  26. bin/console cache set <key> <value> [-h|--help|-?] [-v]
  27. bin/console cache flush [-h|--help|-?] [-v]
  28. bin/console cache clear [-h|--help|-?] [-v]
  29. Description
  30. bin/console cache list [<prefix>]
  31. List all cache keys, optionally filtered by a prefix
  32. bin/console cache get <key>
  33. Shows the value of the provided cache key
  34. bin/console cache set <key> <value> [<ttl>]
  35. Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes.
  36. bin/console cache flush
  37. Clears expired cache keys
  38. bin/console cache clear
  39. Clears all cache keys
  40. Options
  41. -h|--help|-? Show help information
  42. -v Show more debug information.
  43. HELP;
  44. return $help;
  45. }
  46. protected function doExecute()
  47. {
  48. $a = \Friendica\BaseObject::getApp();
  49. if ($this->getOption('v')) {
  50. $this->out('Executable: ' . $this->executable);
  51. $this->out('Class: ' . __CLASS__);
  52. $this->out('Arguments: ' . var_export($this->args, true));
  53. $this->out('Options: ' . var_export($this->options, true));
  54. }
  55. if ($a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  56. $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
  57. }
  58. Core\Cache::init();
  59. if ($this->getOption('v')) {
  60. $this->out('Cache Driver Name: ' . Core\Cache::$driver_name);
  61. $this->out('Cache Driver Class: ' . Core\Cache::$driver_class);
  62. }
  63. switch ($this->getArgument(0)) {
  64. case 'list':
  65. $this->executeList();
  66. break;
  67. case 'get':
  68. $this->executeGet();
  69. break;
  70. case 'set':
  71. $this->executeSet();
  72. break;
  73. case 'flush':
  74. $this->executeFlush();
  75. break;
  76. case 'clear':
  77. $this->executeClear();
  78. break;
  79. }
  80. if (count($this->args) == 0) {
  81. $this->out($this->getHelp());
  82. return 0;
  83. }
  84. return 0;
  85. }
  86. private function executeList()
  87. {
  88. $prefix = $this->getArgument(1);
  89. $keys = Core\Cache::getAllKeys();
  90. if (empty($prefix)) {
  91. $this->out('Listing all cache keys:');
  92. } else {
  93. $this->out('Listing all cache keys starting with "' . $prefix . '":');
  94. }
  95. $count = 0;
  96. foreach ($keys as $key) {
  97. if (empty($prefix) || strpos($key, $prefix) === 0) {
  98. $this->out($key);
  99. $count++;
  100. }
  101. }
  102. $this->out($count . ' keys found');
  103. }
  104. private function executeGet()
  105. {
  106. if (count($this->args) >= 2) {
  107. $key = $this->getArgument(1);
  108. $value = Core\Cache::get($key);
  109. $this->out("{$key} => " . var_export($value, true));
  110. } else {
  111. throw new CommandArgsException('Too few arguments for get');
  112. }
  113. }
  114. private function executeSet()
  115. {
  116. if (count($this->args) >= 3) {
  117. $key = $this->getArgument(1);
  118. $value = $this->getArgument(2);
  119. $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES));
  120. if (is_array(Core\Cache::get($key))) {
  121. throw new RuntimeException("$key is an array and can't be set using this command.");
  122. }
  123. $result = Core\Cache::set($key, $value, $duration);
  124. if ($result) {
  125. $this->out("{$key} <= " . Core\Cache::get($key));
  126. } else {
  127. $this->out("Unable to set {$key}");
  128. }
  129. } else {
  130. throw new CommandArgsException('Too few arguments for set');
  131. }
  132. }
  133. private function executeFlush()
  134. {
  135. $result = Core\Cache::clear();
  136. if ($result) {
  137. $this->out('Cache successfully flushed');
  138. } else {
  139. $this->out('Unable to flush the cache');
  140. }
  141. }
  142. private function executeClear()
  143. {
  144. $result = Core\Cache::clear(false);
  145. if ($result) {
  146. $this->out('Cache successfully cleared');
  147. } else {
  148. $this->out('Unable to flush the cache');
  149. }
  150. }
  151. }