TestRedis.php 2.4 KB

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