g4 / mcache
mcache php library
Requires
- php: >=8.2
- couchbase/couchbase: *
- g4/profiler: >=1.10.0
- g4/value-object: *
Requires (Dev)
- phpunit/phpunit: 10.*
Suggests
- ext-couchbase: ^4.0
Provides
None
Conflicts
None
Replaces
None
README
mcache - php cache wrapper library
Install
Install through composer package manager. Find it on packagist.
composer require g4/mcache
Supported caching systems
Usage
Memcached instance
<?php $driverName = 'Libmemcached'; $options = array( 'servers' => array( '127.0.0.1:11211' ); ); $prefix = 'my_prefix'; $mcache = \G4\Mcache\McacheFactory::createInstance($driverName, $options, $prefix);
Couchbase instance
<?php $driverName = 'Couchbase'; $options = array( 'bucket' => 'my_bucket', 'servers' => array( 'couchbase01.local', 'couchbase02.local', ), 'user' => 'cb_user', 'pass' => 'cb_pass', ); $prefix = 'my_prefix'; $mcache = \G4\Mcache\McacheFactory::createInstance($driverName, $options, $prefix);
The driver picks the client by the installed extension: Couchbase SDK 1.x, 2.x or 4.x
(ext-couchbase ^4.0 with couchbase/couchbase on PHP 8.2+).
Available options
| Option | Type | Unit | Default | SDK | Description |
|---|---|---|---|---|---|
bucket |
string | required | all | Bucket name | |
servers |
array | required | all | Cluster nodes; empty and non-string entries are skipped | |
user |
string | '' |
all | Username | |
pass |
string | '' |
all | Password | |
persistent |
bool | false |
1.x | Persistent connection (SDK 4 keeps connections per process by itself, see couchbase.max_persistent) |
|
timeout |
int | microseconds | 500000 |
all | KV operation timeout. SDK 4: converted to milliseconds, overridden by kv_timeout |
kv_timeout |
int | milliseconds | from timeout |
4.x | KV operation timeout (get, upsert, replace, remove) |
connect_timeout |
int | milliseconds | SDK default (2000) | 4.x | TCP connect timeout for one socket |
bootstrap_timeout |
int | milliseconds | 3000 (SDK default is 10000) |
4.x | Whole cluster/bucket open: DNS, connect, auth, bucket selection, cluster config |
retry_after |
int | float | seconds | 10 |
4.x | After a failed connect, skip reconnecting for this long; 0 disables |
All SDK 4 timeouts are in milliseconds. timeout keeps its old unit (microseconds) for
backward compatibility with the 1.x/2.x drivers, so timeout = 300000 means 300 ms.
Connection failures (SDK 4)
When the cluster cannot be reached, the driver triggers one E_USER_WARNING:
Could not connect to couchbase cluster couchbase://..., bucket ..., message: unambiguous_timeout (14)
When the cluster is reachable but the bucket cannot be opened (SDK 4 opens it right away: missing bucket, no access for the user, ...), the warning is:
Could not open couchbase bucket ... on cluster couchbase://..., message: bucket_not_found (10)
In both cases every get, set, replace and delete returns false. The failure is remembered in a
static property (shared by all instances with the same servers, bucket and user) for
retry_after seconds. Calls in that window return false immediately instead of waiting for
another bootstrap_timeout, and no further warnings are logged. The first call after the window
tries to connect again.
How long the failure is remembered depends on how PHP runs:
- php-fpm / web requests: PHP resets static properties at the end of every request, so the
failure is remembered only within one request. A request waits for at most one
bootstrap_timeoutinstead of one per cache call;retry_afterhas practically no effect. - Long running CLI processes (workers, consumers, task runners): the failure is remembered
across jobs. Worst case wait is one
bootstrap_timeouteveryretry_afterseconds.
Note that delete is skipped too while the failure is remembered. If the network recovers before
retry_after expires, the process still does not invalidate keys until the window ends, and other
processes can read stale values until then (or until the key expires). This is why the default is
short (10 s); do not raise it much without a reason.
Configuration in application.ini
Example for a Zend style application.ini, where the mcache.options section is passed as
$options to McacheFactory::createInstance():
[production] mcache.driver = Couchbase mcache.options.servers[] = couchbase01.example.int mcache.options.servers[] = couchbase02.example.int mcache.options.servers[] = couchbase03.example.int mcache.options.user = cb_user mcache.options.pass = cb_pass mcache.options.bucket = my_bucket_production mcache.options.persistent = 1 mcache.options.timeout = 300000 ; microseconds, KV operation timeout (legacy unit, = 300 ms) mcache.options.bootstrap_timeout = 3000 ; milliseconds mcache.options.retry_after = 10 ; seconds ; optional, SDK 4 only ;mcache.options.kv_timeout = 300 ; milliseconds, overrides "timeout" ;mcache.options.connect_timeout = 2000 ; milliseconds, SDK default when not set [stage : production] mcache.options.servers[] = couchbase01.stage.int mcache.options.servers[] = couchbase02.stage.int mcache.options.servers[] = null mcache.options.bucket = my_bucket_stage
Choosing timeout values
Keep timeouts above the Linux TCP retransmission timers, otherwise a single lost packet fails the request before the kernel even retransmits it:
- KV operation timeout (
timeout/kv_timeout): at least 300 ms. A lost request or response on an open connection is retransmitted after the minimum RTO, 200 ms on Linux (TCP_RTO_MIN; see thertofield inss -ti). Below that, the retry never happens. connect_timeout: well above 1 s. A lost SYN is retransmitted after the initial RTO of 1 s, then 3 s, 7 s, ... (TCP_TIMEOUT_INIT,net.ipv4.tcp_syn_retries). The SDK default 2 s allows one SYN retransmission.bootstrap_timeout: larger thanconnect_timeout, so the client can still try another node after one connect fails.3000allows one SYN retransmission plus authentication and bucket selection on a LAN.
retry_after matters only for long running processes. With bootstrap_timeout = 3000 and
retry_after = 10, a worker spends about a quarter of an outage waiting on connect attempts and
skips cache invalidation for at most 10 s after the network recovers.
Upgrading from 2.1.0: timeout was passed to SDK 4 as connectTimeout in milliseconds, so
timeout = 100000 meant a 100 second connect timeout. It is now the KV operation timeout
(100 ms), as it was with SDK 1.x/2.x. That is below the Linux minimum retransmission timeout, so
raise it to at least 300000 (or set kv_timeout = 300) when upgrading; SDK 4 default is 2500 ms.
Methods
<?php // Get from cache $value = $mcache ->key('my_key') ->get(); // Save to cache $mcache ->key('my_key') ->value('my_value') ->set(); // Sava to cache with expiration $mcache ->key('my_key') ->value('my_value') ->expiration(3600) // in seconds (default 0) ->set(); // Delete from cache $mcache ->key('my_key') ->delete(); // Replace a value $mcache ->key('my_key') ->value('my_value') ->replace();
Development
Install dependencies
$ make install
Run tests
$ make test
License
(The MIT License) see LICENSE file for details...