Skip to content

Commit

Permalink
fix: fix memcache fortrabbit integration (monicahq/chandler#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jan 4, 2023
1 parent 9205775 commit 8bcd595
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion config/cache.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

use Illuminate\Support\Arr;
use Illuminate\Support\Str;

return [
$config = [

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -108,3 +109,53 @@
'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'),

];

// on fortrabbit: construct credentials from App secrets
if (env('APP_SECRETS')) {
$secrets = json_decode(file_get_contents(env('APP_SECRETS')), true);
if (array_key_exists('MEMCACHE', $secrets)) {
$servers = [[
'host' => $secrets['MEMCACHE']['HOST1'],
'port' => $secrets['MEMCACHE']['PORT1'],
'weight' => 100,
]];
if ($secrets['MEMCACHE']['COUNT'] > 1) {
$servers[] = [
'host' => $secrets['MEMCACHE']['HOST2'],
'port' => $secrets['MEMCACHE']['PORT2'],
'weight' => 100,
];
}
Arr::set($config, 'stores.memcached.servers', $servers);
}
}

if (extension_loaded('memcached')) {
$timeout_ms = 50;
$options = [
// Assure that dead servers are properly removed and ...
\Memcached::OPT_REMOVE_FAILED_SERVERS => true,

// ... retried after a short while (here: 2 seconds)
\Memcached::OPT_RETRY_TIMEOUT => 2,

// KETAMA must be enabled so that replication can be used
\Memcached::OPT_LIBKETAMA_COMPATIBLE => true,

// Replicate the data, write it to both memcached servers
\Memcached::OPT_NUMBER_OF_REPLICAS => 1,

// Those values assure that a dead (due to increased latency or
// really unresponsive) memcached server is dropped fast
\Memcached::OPT_POLL_TIMEOUT => $timeout_ms, // milliseconds
\Memcached::OPT_SEND_TIMEOUT => $timeout_ms * 1000, // microseconds
\Memcached::OPT_RECV_TIMEOUT => $timeout_ms * 1000, // microseconds
\Memcached::OPT_CONNECT_TIMEOUT => $timeout_ms, // milliseconds

// Further performance tuning
\Memcached::OPT_NO_BLOCK => true,
];
Arr::set($config, 'stores.memcached.options', $options);
}

return $config;

0 comments on commit 8bcd595

Please sign in to comment.