1515class Bootstrap implements BootstrapInterface
1616{
1717
18+ const TIMEOUT_IN_MILLISECONDS = 50 ;
19+
1820 /**
1921 * Bootstrapper
2022 *
@@ -31,6 +33,7 @@ public function bootstrap($app)
3133 'class ' => MemCache::class,
3234 'persistentId ' => getenv ('MEMCACHE_PERSISTENT ' ) ? 'cache ' : null ,
3335 'servers ' => $ this ->getMemcachedServers (),
36+ 'options ' => $ this ->getOptions (),
3437 'useMemcached ' => true
3538 ]);
3639 } catch (InvalidConfigException $ e ) {
@@ -51,13 +54,46 @@ protected function getMemcachedServers(): array
5154 $ servers [] = [
5255 'host ' => getenv ('MEMCACHE_HOST ' . $ num ),
5356 'port ' => getenv ('MEMCACHE_PORT ' . $ num ),
54- 'retryInterval ' => 2 ,
55- 'status ' => true ,
56- 'timeout ' => 2 ,
5757 'weight ' => 1 ,
5858 ];
5959 }
6060
6161 return $ servers ;
6262 }
63+
64+ /**
65+ * @return array
66+ */
67+ protected function getOptions (): array
68+ {
69+ if (!extension_loaded ('memcached ' )) {
70+ return [];
71+ }
72+
73+ return [
74+
75+ // Assure that dead servers are properly removed and ...
76+ \Memcached::OPT_REMOVE_FAILED_SERVERS => true ,
77+
78+ // ... retried after a short while (here: 2 seconds)
79+ \Memcached::OPT_RETRY_TIMEOUT => 2 ,
80+
81+ // KETAMA must be enabled so that replication can be used
82+ \Memcached::OPT_LIBKETAMA_COMPATIBLE => true ,
83+
84+ // Replicate the data, i.e. write it to both memcached servers
85+ \Memcached::OPT_NUMBER_OF_REPLICAS => 1 ,
86+
87+ // Those values assure that a dead (due to increased latency or
88+ // really unresponsive) memcached server increased dropped fast
89+ // and the other is used.
90+ \Memcached::OPT_POLL_TIMEOUT => self ::TIMEOUT_IN_MILLISECONDS , // milliseconds
91+ \Memcached::OPT_SEND_TIMEOUT => self ::TIMEOUT_IN_MILLISECONDS * 1000 , // microseconds
92+ \Memcached::OPT_RECV_TIMEOUT => self ::TIMEOUT_IN_MILLISECONDS * 1000 , // microseconds
93+ \Memcached::OPT_CONNECT_TIMEOUT => self ::TIMEOUT_IN_MILLISECONDS , // milliseconds
94+
95+ // Further performance tuning
96+ \Memcached::OPT_NO_BLOCK => true ,
97+ ];
98+ }
6399}
0 commit comments