Uso básico

Ejemplo #1 Ejemplos de la extensión memcache extension

En este ejemplo, se guarda un objeto en cache y luego se devuelve de nuevo. Objetos y otros tipos no escalares se serializan antes de guardarse, por lo tanto es imposible guardar recursos. (ej. identificadores de conexión y otros tipos) en caché.


= new Memcache;
$memcache->connect('localhost', 11211) or die ("No se pudo contectar");

$version = $memcache->getVersion();
"Versión del servidor: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Falló al intentar guardar datos en el servidor");
"Guarda datos en caché (los datos expirarán en 10 segundos)<br/>\n";

$get_result = $memcache->get('key');
"Datos desde la caché:<br/>\n";



Ejemplo #2 Usando el gestor de sesiones de memcache


= "tcp://$host:$port?persistent=1&weight=2&timeout=2&retry_interval=10, ,tcp://$host:$port ";
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', $session_save_path);


add a note

User Contributed Notes 3 notes

atesin > gmail
2 years ago
memcached is great, is lightning fast, very versatile and useful, scalable, and is a must have for many projects

but if you only want speed to minimize session file blocking there is also a good alternative, tmpfs

maybe if you are in debian you already had session directory in tmp (mounted as tmpfs), but beware of daily cleaning process that can mess up your sessions

you can use this trick if you are in centos/other (like me) or even if you are in debian but want to get ride of /tmp cleaning task

i realized in my system /run is also mounted as tmpfs, so i shut php-fpm down, moved my php session dir to /tmp/, reconfigure php and start again... (you can adapt it to your situation)

  systemctl stop php-fpm
  cp -a /var/lib/php/session /tmp/php-session
  vim /etc/php-fpm-d/www.conf
  php_value[session.save_path] = /run/php-session
  systemctl start php-fpm

the only drawback is tmpfs is VOLATILE, just like memcached (data is lost on unmount/shutdown/power fail), to  circumvent this risk i wrote another service that restores/backup php session dir before/after php starts/stops... (UNTESTED!)

  vim /etc/systemd/system/php-session-backup.service
  # basic persistence for tmpfs php sessions
  Description=PHP tmpfs sessions backup/restore on shutdown/boot
  ExecStart=rm -fr /run/php-session
  ExecStart=cp -fa /var/lib/php/session /run/php-session
  ExecStop=rm -fr /var/lib/php/session
  ExecStop=cp -fa /run/php-session /var/lib/php/session
  systemctl enable php-session-backup

you can also complement this with a daily backup task in case of system crash so you will lose just one day

  crontab -e
  0 4 * * * rm -fr /var/lib/php/session;cp -fa /run/php-session /var/lib/php/session

this is very rough though, you can better use inotify + rsync, could take some ideas from here
atesin > gmail
2 years ago
moderator please merge these posts

an errata to my comment done on 2020-07-28 01:05 about tmpfs session dir...

the tmpfs directory i used to install session files is "/run" not "/tmp"...  as /tmp is auto (or manual) deleted sometimes
dkreuter at gmail dot com
13 years ago
If the example doesn't work try to change "localhost" to "".
To Top