2
0

TestRedis.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $value='this is a test';
  41. $this->info("test redis start");
  42. $remember = Cache::store('redis')->remember('dd',10,function(){
  43. return 'remember ok';
  44. });
  45. $this->info("test store remember value=".$remember);
  46. $key = "test-redis";
  47. Redis::set($key,$value);
  48. if(Redis::exists($key)){
  49. $this->info("has key ".$key);
  50. }else{
  51. $this->error("no key ".$key);
  52. }
  53. $expire = Redis::expire($key,10);
  54. $this->info("key expire ".$expire);
  55. $this->info('del key '.Redis::del($key));
  56. Redis::set($key,$value);
  57. $getValue = Redis::get($key);
  58. if($getValue === $value){
  59. $this->info("redis set ok ");
  60. }else{
  61. $this->error("redis set fail ");
  62. }
  63. Redis::hSet("test-redis-hash",'hash',$value);
  64. if(Redis::hGet("test-redis-hash",'hash')==$value){
  65. $this->info("redis hash set ok ");
  66. }else{
  67. $this->error("redis hash set fail ");
  68. }
  69. $this->info("test cache start");
  70. $this->info("testing cache put");
  71. $key = 'cache-key';
  72. Cache::put($key,$value,1000);
  73. if(Cache::has($key)){
  74. $this->info("cache::put() exist key={$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');
  82. }
  83. $key = 'cache-key-2';
  84. $this->info("testing cache() function");
  85. $this->info("cache() key={$key} value={$value}");
  86. cache(["cache-key-2"=>$value]);
  87. if(Cache::has($key)){
  88. if(Cache::get($key)==$value){
  89. $this->info("cache() get ok value={$value}");
  90. }else{
  91. $this->error("cache::get() fail ");
  92. }
  93. }else{
  94. $this->error('no key cache-key-2');
  95. }
  96. $key = 'cache-key-3';
  97. $this->info("testing cache remember()");
  98. $value = Cache::remember($key,600,function(){
  99. return 'cache-value-3';
  100. });
  101. if(Cache::has($key)){
  102. $this->info("{$key} exist value=".Cache::get($key));
  103. }else{
  104. $this->error("cache::remember() fail.");
  105. }
  106. $key = 'cache-key-clusters';
  107. $this->info("testing RedisClusters remember()");
  108. $this->info('get='.RedisClusters::get($key));
  109. RedisClusters::put($key,'RedisClusters');
  110. if(RedisClusters::has($key)){
  111. $this->info("RedisClusters has key value=".RedisClusters::get($key));
  112. }
  113. RedisClusters::forget($key);
  114. if(RedisClusters::has($key)){
  115. $this->error("RedisClusters forget fail ");
  116. }else{
  117. $this->info("RedisClusters forget successful ");
  118. }
  119. $null = null;
  120. $value = RedisClusters::remember($key,2,function() use($null){
  121. return $null;
  122. });
  123. $this->info("null=".$value);
  124. RedisClusters::forget($key);
  125. $value1 = ['data'=>'cache-key-clusters'];
  126. $value = RedisClusters::remember($key,2,function() use($value1){
  127. return $value1;
  128. });
  129. if(RedisClusters::has($key)){
  130. $this->info("{$key} exist value=");
  131. var_dump(RedisClusters::get($key));
  132. sleep(3);
  133. if(RedisClusters::has($key)){
  134. $this->error('exp fail');
  135. }else{
  136. $this->info('exp successful');
  137. }
  138. }else{
  139. $this->error("cache::remember() fail.");
  140. }
  141. return 0;
  142. }
  143. }