TestRedis.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Redis::set($key,$value);
  54. $getValue = Redis::get($key);
  55. if($getValue === $value){
  56. $this->info("redis set ok ");
  57. }else{
  58. $this->error("redis set fail ");
  59. }
  60. Redis::hSet("test-redis-hash",'hash',$value);
  61. if(Redis::hGet("test-redis-hash",'hash')==$value){
  62. $this->info("redis hash set ok ");
  63. }else{
  64. $this->error("redis hash set fail ");
  65. }
  66. $this->info("test cache start");
  67. $this->info("testing cache put");
  68. $key = 'cache-key';
  69. Cache::put($key,$value,1000);
  70. if(Cache::has($key)){
  71. $this->info("cache::put() exist key={$key}");
  72. if(Cache::get($key)==$value){
  73. $this->info("cache::get() ok value={$value}");
  74. }else{
  75. $this->error("cache::get() fail ");
  76. }
  77. }else{
  78. $this->error('no key cache-key');
  79. }
  80. $key = 'cache-key-2';
  81. $this->info("testing cache() function");
  82. $this->info("cache() key={$key} value={$value}");
  83. cache(["cache-key-2"=>$value]);
  84. if(Cache::has($key)){
  85. if(Cache::get($key)==$value){
  86. $this->info("cache() get ok value={$value}");
  87. }else{
  88. $this->error("cache::get() fail ");
  89. }
  90. }else{
  91. $this->error('no key cache-key-2');
  92. }
  93. $key = 'cache-key-3';
  94. $this->info("testing cache remember()");
  95. $value = Cache::remember($key,600,function(){
  96. return 'cache-value-3';
  97. });
  98. if(Cache::has($key)){
  99. $this->info("{$key} exist value=".Cache::get($key));
  100. }else{
  101. $this->error("cache::remember() fail.");
  102. }
  103. $key = 'cache-key-clusters';
  104. $this->info("testing RedisClusters remember()");
  105. RedisClusters::put($key,'RedisClusters');
  106. if(RedisClusters::has($key)){
  107. $this->info("RedisClusters has key value=".RedisClusters::get($key));
  108. }
  109. RedisClusters::forget($key);
  110. if(RedisClusters::has($key)){
  111. $this->error("RedisClusters forget fail ");
  112. }else{
  113. $this->info("RedisClusters forget successful ");
  114. }
  115. $value = RedisClusters::remember($key,2,function(){
  116. return 'cache-key-clusters';
  117. });
  118. if(RedisClusters::has($key)){
  119. $this->info("{$key} exist value=".RedisClusters::get($key));
  120. sleep(3);
  121. if(RedisClusters::has($key)){
  122. $this->error('exp fail');
  123. }else{
  124. $this->info('exp successful');
  125. }
  126. }else{
  127. $this->error("cache::remember() fail.");
  128. }
  129. return 0;
  130. }
  131. }