Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. While primarily designed for Linux/Unix environments, it is possible to get Memcached running on Windows for development and testing purposes.
This article will guide you through the process of installing the Memcached server on Windows and configuring the PHP Memcached extension from PECL.
1. Installing the Memcached Server
Since there isn’t an official Windows binary for Memcached, we’ll use a pre-compiled version.
Step 1: Download Memcached
- Download the latest stable Memcached binary for Windows. A common source for these binaries is the Memcached GitHub repository or other community-maintained sources. Look for a
memcached.exe
file.- Self-correction: As of my last update, a reliable direct download link for a pre-compiled
memcached.exe
for Windows is often found on community sites or specific GitHub releases. A quick search for “memcached windows binary download” usually yields results. For instance, you might find it on a site likehttps://commondatastorage.googleapis.com/daemon-tools/binaries/memcached-win64-1.4.4-14.zip
(this is an example and might be outdated; always verify the source).
- Self-correction: As of my last update, a reliable direct download link for a pre-compiled
- Once downloaded, extract the
memcached.exe
file to a directory of your choice, for example,C:\memcached
.
Step 2: Install Memcached as a Windows Service
Running Memcached as a service ensures it starts automatically with Windows and runs in the background.
- Open Command Prompt as Administrator. Search for “cmd” in the Start menu, right-click on “Command Prompt,” and select “Run as administrator.”
- Navigate to the Memcached directory:
cd C:\memcached
- Install Memcached as a service:
memcached.exe -d install
You should see a message indicating the service has been installed successfully.
Step 3: Start the Memcached Service
- Start the service:
memcached.exe -d start
Alternatively, you can go toservices.msc
(Windows Services Manager), find “Memcached,” and start it manually.
Step 4: Configure Memcached (Optional but Recommended)
You can configure Memcached by passing parameters when installing the service. For example, to set the memory limit and port:
- Uninstall the existing service (if already installed):
memcached.exe -d uninstall
- Install with parameters (e.g., 64MB memory, port 11211):
memcached.exe -d install -m 64 -p 11211
-m <memory_in_MB>
: Sets the maximum memory to use for objects.-p <port_number>
: Sets the TCP port to listen on.-l <ip_address>
: Sets the IP address to listen on (e.g.,127.0.0.1
for local access only).
- Start the service again:
memcached.exe -d start
2. Installing the PHP Memcached Extension (from PECL)
The memcached
PHP extension (not to be confused with the older memcache
extension) is recommended for its richer feature set.
Step 1: Identify Your PHP Version and Architecture
- Check your PHP version and architecture (x86 or x64) and thread safety (TS or NTS). Create a
phpinfo.php
file in your web server’s document root (e.g.,htdocs
for Apache,www
for Nginx) with the following content:<?php phpinfo(); ?>
- Access this file in your browser (e.g.,
http://localhost/phpinfo.php
). Look for:PHP Version
(e.g., 8.2.10)Architecture
(e.g.,x64
orx86
)Thread Safety
(e.g.,enabled
for TS,disabled
for NTS)
Step 2: Download the PHP Memcached Extension DLL
- Go to the PECL website: https://pecl.php.net/
- Search for “memcached”.
- Click on the “DLL” link for the latest stable version.
- Find the download link that matches your PHP version, architecture (x86/x64), and thread safety (TS/NTS). For example, if you have PHP 8.2, x64, NTS, you’d look for
php_memcached-x.x.x-8.2-nts-x64.zip
. - Download the ZIP file and extract the
php_memcached.dll
file.
Step 3: Place the DLL and Dependencies
- Place
php_memcached.dll
: Copyphp_memcached.dll
into your PHPext
directory (e.g.,C:\php\ext
). - Copy dependency DLLs: The
memcached
extension often requires additional DLLs, such aslibmemcached.dll
,libmemcached-*.dll
, libhashkit.dll,libsasl.dll
, etc. These are usually included in the downloaded PECL ZIP file alongsidephp_memcached.dll
. Copy all these dependency DLLs into your PHP root directory (e.g.,C:\php
) or into a directory that is in your system’s PATH. The PHP root directory is generally safer.
Step 4: Configure php.ini
- Open your
php.ini
file. This file is usually located in your PHP root directory (e.g.,C:\php\php.ini
). - Add the extension line: Add the following line under the
extension
section (or anywhere in the file if the section doesn’t exist):extension=php_memcached.dll
- Save the
php.ini
file.
Step 5: Restart Your Web Server
- Restart your web server (Apache, Nginx, IIS, etc.) for the changes to
php.ini
to take effect.
Step 6: Verify Installation
- Access your
phpinfo.php
file again in your browser. - Search for “memcached”. If installed correctly, you should see a “memcached” section with configuration details.
3. Basic PHP Usage Example
Here’s a simple PHP script to test your Memcached installation:
<?php
// Create a Memcached object
$memcached = new Memcached();
// Add a Memcached server (replace with your server details if different)
// Default is localhost:11211
$memcached->addServer('127.0.0.1', 11211);
// Set a key-value pair
$key = 'my_test_key';
$value = 'Hello, Memcached!';
$expiration = 60; // Cache for 60 seconds
if ($memcached->set($key, $value, $expiration)) {
echo "Successfully set '$key' to '$value' in Memcached.<br>";
} else {
echo "Failed to set '$key' in Memcached. Error: " . $memcached->getResultMessage() . "<br>";
}
// Get the value
$retrievedValue = $memcached->get($key);
if ($retrievedValue !== false) {
echo "Successfully retrieved '$key': '$retrievedValue'.<br>";
} else {
echo "Failed to retrieve '$key'. It might not exist or has expired. Error: " . $memcached->getResultMessage() . "<br>";
}
// Delete a key (optional)
// if ($memcached->delete($key)) {
// echo "Successfully deleted '$key'.<br>";
// } else {
// echo "Failed to delete '$key'. Error: " . $memcached->getResultMessage() . "<br>";
// }
// Get server statistics
$stats = $memcached->getStats();
if ($stats) {
echo "<br>Memcached Server Statistics:<br>";
echo "<pre>";
print_r($stats);
echo "</pre>";
} else {
echo "Failed to get Memcached server statistics.<br>";
}
?>
Save this code as test_memcached.php
in your web server’s document root and access it via your browser. You should see messages indicating successful interaction with Memcached.
Troubleshooting Tips
- “memcached.exe is not recognized…”: Ensure you are in the directory where
memcached.exe
is located or that its path is added to your system’s PATH environment variable. - “Failed to start Memcached service”: Check the Windows Event Viewer for more detailed error messages. It might be a port conflict or insufficient permissions.
- PHP
memcached
extension not loading:- Double-check that you downloaded the correct DLL (PHP version, architecture, TS/NTS).
- Ensure all dependency DLLs (e.g.,
libmemcached.dll
,libsasl.dll
) are in your PHP root directory or a directory in your system’s PATH. - Verify the
extension=php_memcached.dll
line is correctly added tophp.ini
and not commented out. - Ensure you restarted your web server after modifying
php.ini
.
- PHP script cannot connect to Memcached:
- Verify the Memcached service is running (check
services.msc
). - Ensure the IP address and port in your PHP script (
addServer('127.0.0.1', 11211)
) match the Memcached server’s configuration. - Check if a firewall is blocking the port.
- Verify the Memcached service is running (check
By following these steps, you should have Memcached up and running on your Windows development environment, ready to be used with your PHP applications.
Leave a Reply