TestRedis.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Redis::set("test-redis",$value);
  39. if(Redis::get("test-redis")==$value){
  40. $this->info("redis set ok ");
  41. }else{
  42. $this->error("redis set fail ");
  43. }
  44. Redis::hSet("test-redis-hash",'hash',$value);
  45. if(Redis::hGet("test-redis-hash",'hash')==$value){
  46. $this->info("redis hash set ok ");
  47. }else{
  48. $this->error("redis hash set fail ");
  49. }
  50. $this->info("test cache start");
  51. $this->info("testing cache put");
  52. $key = 'cache-key';
  53. Cache::put($key,$value,1000);
  54. if(Cache::has($key)){
  55. $this->info("cache::put() exist key={$key}");
  56. if(Cache::get($key)==$value){
  57. $this->info("cache::get() ok value={$value}");
  58. }else{
  59. $this->error("cache::get() fail ");
  60. }
  61. }else{
  62. $this->error('no key cache-key');
  63. }
  64. $key = 'cache-key-2';
  65. $this->info("testing cache() function");
  66. $this->info("cache() key={$key} value={$value}");
  67. cache(["cache-key-2"=>$value]);
  68. if(Cache::has($key)){
  69. if(Cache::get($key)==$value){
  70. $this->info("cache() get ok value={$value}");
  71. }else{
  72. $this->error("cache::get() fail ");
  73. }
  74. }else{
  75. $this->error('no key cache-key-2');
  76. }
  77. $key = 'cache-key-3';
  78. $this->info("testing cache remember()");
  79. $value = Cache::remember($key,600,function(){
  80. return 'cache-value-3';
  81. });
  82. if(Cache::has($key)){
  83. $this->info("{$key} exist value=".Cache::get($key));
  84. }else{
  85. $this->error("cache::remember() fail.");
  86. }
  87. return 0;
  88. }
  89. }