Skip to content

Commit 8bcd595

Browse files
authored
fix: fix memcache fortrabbit integration (monicahq/chandler#372)
1 parent 9205775 commit 8bcd595

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

config/cache.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3+
use Illuminate\Support\Arr;
34
use Illuminate\Support\Str;
45

5-
return [
6+
$config = [
67

78
/*
89
|--------------------------------------------------------------------------
@@ -108,3 +109,53 @@
108109
'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'),
109110

110111
];
112+
113+
// on fortrabbit: construct credentials from App secrets
114+
if (env('APP_SECRETS')) {
115+
$secrets = json_decode(file_get_contents(env('APP_SECRETS')), true);
116+
if (array_key_exists('MEMCACHE', $secrets)) {
117+
$servers = [[
118+
'host' => $secrets['MEMCACHE']['HOST1'],
119+
'port' => $secrets['MEMCACHE']['PORT1'],
120+
'weight' => 100,
121+
]];
122+
if ($secrets['MEMCACHE']['COUNT'] > 1) {
123+
$servers[] = [
124+
'host' => $secrets['MEMCACHE']['HOST2'],
125+
'port' => $secrets['MEMCACHE']['PORT2'],
126+
'weight' => 100,
127+
];
128+
}
129+
Arr::set($config, 'stores.memcached.servers', $servers);
130+
}
131+
}
132+
133+
if (extension_loaded('memcached')) {
134+
$timeout_ms = 50;
135+
$options = [
136+
// Assure that dead servers are properly removed and ...
137+
\Memcached::OPT_REMOVE_FAILED_SERVERS => true,
138+
139+
// ... retried after a short while (here: 2 seconds)
140+
\Memcached::OPT_RETRY_TIMEOUT => 2,
141+
142+
// KETAMA must be enabled so that replication can be used
143+
\Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
144+
145+
// Replicate the data, write it to both memcached servers
146+
\Memcached::OPT_NUMBER_OF_REPLICAS => 1,
147+
148+
// Those values assure that a dead (due to increased latency or
149+
// really unresponsive) memcached server is dropped fast
150+
\Memcached::OPT_POLL_TIMEOUT => $timeout_ms, // milliseconds
151+
\Memcached::OPT_SEND_TIMEOUT => $timeout_ms * 1000, // microseconds
152+
\Memcached::OPT_RECV_TIMEOUT => $timeout_ms * 1000, // microseconds
153+
\Memcached::OPT_CONNECT_TIMEOUT => $timeout_ms, // milliseconds
154+
155+
// Further performance tuning
156+
\Memcached::OPT_NO_BLOCK => true,
157+
];
158+
Arr::set($config, 'stores.memcached.options', $options);
159+
}
160+
161+
return $config;

0 commit comments

Comments
 (0)