Howto Start Memcached and Enable php_memcache in WAMP

Start Memcached

WampDeveloper 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'));

?>

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>