coredis.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=0);
  3. define('MOBILE_SERVER', true);
  4. define('USE_COROUTINE', true);
  5. define('SUPPORT_PTHREAD', false);
  6. define('APP_ID', 'coredis');
  7. define('BASE_ROOT_PATH', str_replace('/server', '', dirname(__FILE__)));
  8. define('BASE_PATH', BASE_ROOT_PATH . '/server');
  9. define('COROUTINE_HOOK_TCP',true);
  10. require_once(BASE_ROOT_PATH . '/global.php');
  11. require_once(BASE_ROOT_PATH . '/fooder.php');
  12. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  13. require_once(BASE_HELPER_PATH . '/refill_proxy.php');
  14. require_once(BASE_CORE_PATH . '/framework/libraries/CoPool.php');
  15. require_once(BASE_CORE_PATH . '/framework/libraries/CoRedisPool.php');
  16. Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL | SWOOLE_HOOK_SLEEP | SWOOLE_HOOK_TCP]);
  17. class RedisPoolTest
  18. {
  19. public function setval()
  20. {
  21. for ($i = 0; $i < 200; $i++)
  22. {
  23. Log::record("cur index = {$i}",Log::DEBUG);
  24. go(function ()
  25. {
  26. $key = '100905';
  27. $ins = Cache::getInstance('cacheredis');
  28. $ret = $ins->incr($key);
  29. Log::record("setval value={$ret}",Log::DEBUG);
  30. });
  31. }
  32. }
  33. public function readval()
  34. {
  35. for ($i = 0; $i < 200; $i++)
  36. {
  37. Log::record("cur index = {$i}",Log::DEBUG);
  38. go(function ()
  39. {
  40. $key = '100905';
  41. $ins = Cache::getInstance('cacheredis');
  42. $ret = $ins->get_org($key);
  43. Log::record("readval value={$ret}",Log::DEBUG);
  44. });
  45. }
  46. }
  47. }
  48. Swoole\Coroutine::set(['max_coroutine' => 500]);
  49. go(function ()
  50. {
  51. Base::run_util();
  52. $key = '100905';
  53. $ins = Cache::getInstance('cacheredis');
  54. $ret = $ins->incr($key);
  55. Log::record("setval value={$ret}",Log::DEBUG);
  56. $test = new RedisPoolTest();
  57. for ($i = 0; $i < 100; $i++)
  58. {
  59. Log::record("first index = {$i}",Log::DEBUG);
  60. go(function () use($test)
  61. {
  62. $test->setval();
  63. });
  64. sleep(1);
  65. }
  66. for ($i = 0; $i < 100; $i++)
  67. {
  68. Log::record("second index = {$i}",Log::DEBUG);
  69. go(function () use($test)
  70. {
  71. $test->readval();
  72. });
  73. sleep(1);
  74. }
  75. sleep(10);
  76. CoRedisPool::instance()->stop();
  77. });
  78. //docker-compose run phpswoole php /var/www/html/server/coredis.php