2
0

TestRedis.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Facades\RedisCache;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Redis;
  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. RedisCache::put($key, $value, 1000);
  65. if (RedisCache::has($key)) {
  66. $this->info("cache::put() exist key={$key}");
  67. if (RedisCache::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-3';
  76. $this->info('testing cache remember()');
  77. $value = RedisCache::remember($key, 600, function () {
  78. return 'cache-value-3';
  79. });
  80. if (RedisCache::has($key)) {
  81. $this->info("{$key} exist value=".RedisCache::get($key));
  82. } else {
  83. $this->error('cache::remember() fail.');
  84. }
  85. }
  86. }