TestRedis.php 3.1 KB

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