2
0

TestRedis.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Redis;
  5. use Illuminate\Support\Facades\Cache;
  6. class TestRedis extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'test:redis';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'testing redis';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $value = 'this is a test';
  37. $this->info("test redis start");
  38. $key = "test-redis";
  39. Redis::set($key, $value);
  40. if (Redis::exists($key)) {
  41. $this->info("has key " . $key);
  42. } else {
  43. $this->error("no key " . $key);
  44. }
  45. $expire = Redis::expire($key, 10);
  46. $this->info("key expire " . $expire);
  47. $this->info('del key ' . Redis::del($key));
  48. Redis::set($key, $value);
  49. $getValue = Redis::get($key);
  50. if ($getValue === $value) {
  51. $this->info("redis set ok ");
  52. } else {
  53. $this->error("redis set fail ");
  54. }
  55. Redis::hSet("test-redis-hash", 'hash', $value);
  56. if (Redis::hGet("test-redis-hash", 'hash') == $value) {
  57. $this->info("redis hash set ok ");
  58. } else {
  59. $this->error("redis hash set fail ");
  60. }
  61. $this->info("test cache start");
  62. $this->info("testing cache put");
  63. $key = 'cache-key';
  64. Cache::put($key, $value, 1000);
  65. if (Cache::has($key)) {
  66. $this->info("cache::put() exist key={$key}");
  67. if (Cache::get($key) == $value) {
  68. $this->info("cache::get() ok value={$value}");
  69. } else {
  70. $this->error("cache::get() fail ");
  71. }
  72. } else {
  73. $this->error('no key cache-key');
  74. }
  75. $key = 'cache-key-2';
  76. $this->info("testing cache() function");
  77. $this->info("cache() key={$key} value={$value}");
  78. cache(["cache-key-2" => $value]);
  79. if (Cache::has($key)) {
  80. if (Cache::get($key) == $value) {
  81. $this->info("cache() get ok value={$value}");
  82. } else {
  83. $this->error("cache::get() fail ");
  84. }
  85. } else {
  86. $this->error('no key cache-key-2');
  87. }
  88. $key = 'cache-key-3';
  89. $this->info("testing cache remember()");
  90. $value = Cache::remember($key, 600, function () {
  91. return 'cache-value-3';
  92. });
  93. if (Cache::has($key)) {
  94. $this->info("{$key} exist value=" . Cache::get($key));
  95. } else {
  96. $this->error("cache::remember() fail.");
  97. }
  98. }
  99. }