oihana / php-memcached
The Oihana PHP memcached library
Requires
- php: >=8.4
- ext-memcached: *
- oihana/php-commands: dev-main
- oihana/php-core: dev-main
- oihana/php-files: dev-main
- oihana/php-schema: dev-main
- oihana/php-standards: dev-main
- oihana/php-system: dev-main
Requires (Dev)
- mikey179/vfsstream: ^1.6
- nunomaduro/collision: ^8.8
- phpdocumentor/shim: ^3.8
- phpunit/phpunit: ^12
README
This plugin provides CLI commands to control Memcached directly, using the PHP Memcached extension.
Built on top of the Oihana PHP Commands Library.
📦 Installation
Requires PHP 8.4+
Install via Composer:
composer require oihana/php-memcached
🚀 Quick Start
Memcached is an in-memory key–value store used to speed up dynamic web applications by caching data in RAM, reducing database load and latency. In PHP, you can use it through the Memcached extension.
Memcached is best for storing small, frequently accessed data like session data, API responses, or precomputed query results.
⚠️ It’s not persistent — data is lost if the server restarts.
Initial setup
To run the memcached command locally, you need to create the configuration file config/config.toml
.
You can do this by copying and editing the example config:
cp config/config.example.toml config/config.toml
List Memcached info
Display basic Memcached information:
bin/console command:memcached
Example output:
Command:memcached ================= localhost:11211 --------------- +--------------------+-------+ | Name | Value | +--------------------+-------+ | Current cache size | 0 MB | | Cache used | 0 % | +--------------------+-------+ ✅ Done in 5 ms ---------------- Thank you and see you soon!
List detailed Memcached info
Display full Memcached statistics:
bin/console command:memcached -v
Example output:
Command:memcached ================= localhost:11211 --------------- +---------------------+-------+ | Name | Value | +---------------------+-------+ | Current cache size | 0 MB | | Cache used | 0 % | | Maximum cache size | 64 MB | | Total items | 0 | | Current connections | 2 | | Total connections | 10 | | Get operations | 0 | | Set operations | 0 | +---------------------+-------+ ✅ Done in 6 ms ---------------- Thank you and see you soon!
Flush Memcached cache
Clear the entire Memcached cache:
bin/console command:memcached --flush
Example output:
Command:memcached
=================
Flush the cache
---------------
[OK] [✓] Flush operation succeeded
✅ Done in 4 ms
----------------
Use composer
You can run a composer script:
composer memcache composer memcache -- -v composer memcache -- --verbose composer memcache -- -f composer memcache -- --flush
✅ Running Unit Tests
To run all tests:
composer test
🧾 License
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).
👤 About the author
- Author : Marc ALCARAZ (aka eKameleon)
- Mail : marc@ooop.fr
- Website : http://www.ooop.fr
⚙️ Installing Memcached
This section provides a step-by-step guide to installing and configuring Memcached.
Linux (Debian)
1. Update packages (recommended)
Before installing, make sure your package lists are up to date:
sudo apt update sudo apt upgrade
2. Install the Memcached server
This installs the Memcached daemon, which handles in-memory caching:
$ sudo apt update $ sudo apt install memcached $ sudo apt install libmemcached-tools
Package descriptions:
- memcached — The Memcached server daemon.
- libmemcached-tools — Command-line utilities to interact with Memcached (e.g., for viewing statistics).
3. Install memcflush (optional but useful)
curl -O https://raw.githubusercontent.com/memcached/memcached/master/scripts/memcflush.pl chmod +x memcflush.pl sudo mv memcflush.pl /usr/local/bin/memcflush
4. Verify Perl installation
$ perl --version
5. Start Memcached if necessary
$ sudo systemctl start memcached
6. Configure the Memcached server (optional but recommended)
The main configuration file is:
sudo pico /etc/memcached.conf
Common parameters:
- -m 64 (ou -m 1024 par exemple) : Memory allocation in MB for Memcached (default is often 64 MB).
- -p 11211 : TCP port to listen on (default: 11211).
- -l 127.0.0.1 : Listening IP address. For security, keep this as 127.0.0.1 unless remote access is required (and firewall rules are properly set).
- -U 0 : Disables the UDP protocol to mitigate certain attacks.
After modifying the configuration file, restart Memcached:
sudo systemctl restart memcached sudo systemctl enable memcached # Start automatically at boot
7. Install the PHP Memcached extension
sudo apt install php8.4-memcached sudo systemctl restart nginx sudo systemctl restart php8.4-fpm
If you have multiple PHP versions, ensure you install the extension for the version your application uses (e.g., php8.4-memcached for PHP 8.4).
Verify installation:
<?php phpinfo(); ?>
Open it in a browser and search for “memcached” — you should see a dedicated section showing it’s enabled.
You can also check via CLI:
php -m | grep memcached
8. Verify Memcached is running
echo "stats settings" | nc localhost 11211
9. Secure Memcached
Limit access to localhost
By default, Memcached listens on 127.0.0.1 (localhost). Verify this setting:
sudo pico /etc/memcached.conf
Check for:
-l 127.0.0.1
This prevents external connections.
⚠️ Never expose Memcached directly to the internet.
MacOS
1. Install Memcached and tools
Make sure you have Homebrew installed first.
brew install memcached brew install libmemcached
2. Install memcflush
curl -O https://raw.githubusercontent.com/memcached/memcached/master/scripts/memcflush.pl chmod +x memcflush.pl sudo mv memcflush.pl /usr/local/bin/memcflush
3. Verify Perl installation
perl --version
4. Test memflush
$ memflush --servers=localhost:11211