Howto Start Memcached and Enable php_memcache in WAMP

Start Memcached

WampDeveloper Pro comes with memcached located in folder:
C:\WampDeveloper\Tools\memcached

Memcached can either be started:

A) Manually from the command-line:

memcached.exe -l 127.0.0.1 -p 11211 -m 128 -t 4

B) Or by double-clicking entry “memcached” in WampDeveloper’s Applications Tab, in which case, WampDeveloper will run file:
C:\WampDeveloper\Resources\run\tools.memcached.start.bat

The memcached server will run as long as the command-line window is open. The “-m” option specifies the size (in MB) of memory to use.

Enable php_memcache

To connect and talk to the memcached server via PHP code, PHP extension php_memcache must be loaded.

1. Edit file:
C:\WampDeveloper\Config\Php\php.ini

2. Uncomment section:

[Memcache]
extension=php_memcache.dll
memcache.allow_failover="1"
memcache.max_failover_attempts="20"
memcache.chunk_size="8192"
memcache.default_port="11211"
memcache.hash_strategy="standard"
memcache.hash_function="crc32"
memcache.protocol=ascii
memcache.redundancy=1
memcache.session_redundancy=2
memcache.compress_threshold=20000
memcache.lock_timeout=15

(* uncomment by removing “;”, section is located near end of file)

3. Save file and restart Apache.

Connecting To and Using memcached

Example PHP code to test the operation of the memcached server…

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$memcache = memcache_connect('localhost', 11211);

// note - memcache_connect errors must be suppressed (@) for this code to be reached
if ($memcache === false)
{
	echo "Connection to memcached failed.";
	exit;
}

// test string
$key_string = "string to store in memcached";

// test number
$key_number = 123;

// test array
$key_array  = Array(123, 345, 567);

// test object
$key_object = new StdClass;
$key_object->str_attr = 'test';
$key_object->int_attr = 123;

// store the values into memcached
$memcache->set("str_key", $key_string);
$memcache->set("num_key", $key_number);
$memcache->set("arr_key", $key_array);
$memcache->set("obj_key", $key_object);

// retrieve the values from memcached
var_dump($memcache->get('str_key'));
var_dump($memcache->get('num_key'));
var_dump($memcache->get('arr_key'));
var_dump($memcache->get('obj_key'));

?>

2 thoughts on “Howto Start Memcached and Enable php_memcache in WAMP

    1. admin Post author

      There are two way you can do this.

      1. Use Windows Task Scheduler / Manager to run the bat file that starts memcached – when the computer boots:
      http://windows.microsoft.com/en-us/windows/schedule-task#1TC=windows-7

      * There might be some options to run it in the background so the cmd windows does not show (select-able in Task Manager, or with some extra command or parameters in the bat file).

      2. Run memcached via a Service Wrapper such as:
      http://nssm.cc/description

      The reason it’s not possible to install memecached as a service directly is because it’s Service interface was removed from its codebase after version 1.3.4.

      And the info on the net about using “sc.exe” to create a service out of a regular program only applies to executables that have those Service interfaces implemented.

      Reply

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>