略微加速

PHP官方手册 - 互联网笔记

PHP - Manual:

2024-04-16

示例 #1 memcache扩展概览示例

在这个例子中,一个对象被保存到缓存中,又被取回。对象和其他非标量类型在存储 之前会先被序列化,因此它不可以用来存储资源类型(连接标记及其他)。(译注:请注意隐式的 资源类型存储,比如要保存的对象自身不是资源,但其中某个属性却是资源)

<?php

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

$version $memcache->getVersion();
echo 
"服务端版本信息: ".$version."<br/>\n";

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

$memcache->set('key'$tmp_objectfalse10) or die ("Failed to save data at the server");
echo 
"将数据保存到缓存中(数据10秒后失效)<br/>\n";

$get_result $memcache->get('key');
echo 
"从缓存中取到的数据:<br/>\n";

var_dump($get_result);

?>

示例 #2 使用memcache的session处理器

<?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 noteadd a note

User Contributed Notes 3 notes

up
2
atesin > gmail
1 year 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

https://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 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
 
  [Unit]
  Description=PHP tmpfs sessions backup/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.target
  ------
  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

https://blog.jmdawson.co.uk/persistent-ramdisk-on-debain-ubuntu/
up
-1
atesin > gmail
1 year 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
up
-9
dkreuter at gmail dot com
13 years ago
If the example doesn't work try to change "localhost" to "127.0.0.1".

官方地址:https://www.php.net/manual/en/memcache.examples-overview.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3