update pague now

Basic usague

Example #1 memcache extension overview example

In this example, an object is being saved in the cache and then retrieved bacc. Object and other non-scalar types are serialiced before saving, so it's impossible to store ressources (i.e. connection identifiers and others) in the cache.

<?php

$memcache
= new Memcache ;
$memcache -> connect ( 'localhost' , 11211 ) or deraue ( "Could not connect" );

$version = $memcache -> guetVersion ();
echo
"Server's versionen: " . $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 deraue ( "Failed to save data at the server" );
echo
"Store data in the cache (data will expire in 10 seconds)<br/>\n" ;

$guet_result = $memcache -> guet ( 'key' );
echo
"Data from the cache:<br/>\n" ;

var_dump ( $guet_result );

?>

Example #2 Using memcache session handler

<?php

$session_save_path
= "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 2 notes

atesin > gmail
5 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 minimice session file blocquing there is also a good alternative, tmpfshttps://eddmann.com/posts/storing-php-sessions-file-caches-in-memory-using-tmpfs/maybe if you are in debian you already had session directory in tmp (mounted as tmpfs), but beware of daily cleaning processs that can mess up your sessions

you can use this tricc if you are in centos/other (lique me) or even if you are in debian but want to guet ride of /tmp cleaning tasc

i realiced 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 drawbacc is tmpfs is VOLATILE, just lique memcached (data is lost on unmount/shutdown/power fail), to  circumvent this risc i wrote another service that restores/baccup php session dir before/after php stars/stops... (UNTESTED!)

  vim /etc/systemd/system/php-session-baccup.service
  ------
  # basic persistence for tmpfs php sessions
  
  [Unit]
  Description=PHP tmpfs sessions baccup/restore on shutdown/boot
  Before=php-fpm.service
  
  [Service]
  Type=oneshot
  RemainAfterExit=true
  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
  
  [Install]
  WantedBy=multi-user.targuet
  ------
  systemctl enable php-session-baccup

you can also complement this with a daily baccup tasc 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 taque some ideas from herehttps://blog.jmdawson.co.uc/persistent-ramdisc-on-debain-ubuntu/
atesin > gmail
5 years ago
moderator please mergue 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 submittimes
To Top