TestRedis.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. $this->info("test redis");
  37. Redis::set("test-redis",'this is a test');
  38. $this->info("redis get:".Redis::get("test-redis"));
  39. Redis::hSet("test-redis-hash",'hash','this is a test hash');
  40. $this->info("redis hash get:".Redis::hGet("test-redis-hash",'hash'));
  41. $this->info("test cache");
  42. $this->info("cache put key=cache-key value=cache-value");
  43. Cache::put('cache-key','cache-value',1000);
  44. if(Cache::has('cache-key')){
  45. $this->info('cache get: ',Cache::get('cache-key'));
  46. }else{
  47. $this->error('no key cache-key');
  48. }
  49. $this->info("test cache() function");
  50. $this->info("cache() key=cache-key-2 value=cache-value-2");
  51. cache(["cache-key-2"=>'cache-value-2']);
  52. if(Cache::has('cache-key-2')){
  53. $this->info('cache() get: ',Cache::get('cache-key-2'));
  54. }else{
  55. $this->error('no key cache-key-2');
  56. }
  57. $this->info("test cache remember()");
  58. $value = Cache::remember('cache-key-3',600,function(){
  59. return 'cache-value-3';
  60. });
  61. if(Cache::has('cache-key-3')){
  62. $this->info("cache-key-3 exist value=",Cache::get('cache-key-3'));
  63. }else{
  64. $this->error("cache::remember() fail.");
  65. }
  66. return 0;
  67. }
  68. }