Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use memcache with PHP

Tags:

php

memcached

I finally got memcache running on my home pc so I can start developing with it at last!

I am not off to a good start though I am trying to use the code on

php.net @ memcache-set I am unable to get either example code to work that they post

I tried this:

<?php
/* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_set($memcache_obj, 'var_key', 'some variable', 0, 30);
echo memcache_get($memcache_obj, 'var_key');
?>


And then

<?php
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
echo $memcache_obj->get('var_key');
?>


And got these errors from the code above;

Warning: Memcache::connect() [memcache.connect]: Can't connect to memcache_host:11211, A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060) in C:\webserver\htdocs\test\memcache\index.php on line 36

Warning: Memcache::set() [memcache.set]: Failed to extract 'connection' variable from object in C:\webserver\htdocs\test\memcache\index.php on line 42

Warning: Memcache::get() [memcache.get]: Failed to extract 'connection' variable from object in C:\webserver\htdocs\test\memcache\index.php on line 44


I then found this code on the net somewhere and it does work

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
// add cache
$memcache->set('key', $tmp_object, false, 30) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 30 seconds)<br/>\n";
// get cache
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>


How can I get the examples from PHP.net to work though?


Also I would love to see any emample code involving memcache you might want to share I would really appreciate seeing some working examples

like image 902
JasonDavis Avatar asked Jul 30 '09 12:07

JasonDavis


People also ask

What does memcached do in PHP?

Memcached is an object caching system. It is primarily used to cache the results of database queries, helping dynamic websites like WordPress® and Drupal to serve pages faster. It can also significantly decrease resource use on a busy web server by reducing calls to the database.

How enable memcache in PHP INI?

To enable the PHP memcache extensions, build PHP using the --enable-memcache option to configure when building from source. On Debian-based distributions, use the php-memcache package. To set global runtime configuration options, specify the configuration option values within your php. ini file.

What is the difference between Memcache and Memcached?

These two PHP extensions are not identical. PHP Memcache is older, very stable but has a few limitations. The PHP Memcache module utilizes the daemon directly, while the PHP Memcached module uses the libMemcached client library and contains some added features.


1 Answers

You do realise that you need to replace "memcache_host" with your hostname and/or localhost? Or am I missing the point completly? Also, try to telnet localhost 11211 and then telnet your-memcache-host-name 11211 and see if you get the same result (you should).

like image 167
Nir Levy Avatar answered Sep 23 '22 23:09

Nir Levy