11.5.1问题
You want to store session data in shared memory to maximize performance.
你想要存储session数据到公用存储器,以保持最佳状态
11.5.2解答
Use the pc_Shm_Session class shown in Example 11-3. For example:
使用这个pc_Shm_Session 类例如在例子11-3、例如
?php
$s = new pc_Shm_Session();
ini_get('session.auto_start') or session_start();
11.5.3讨论
As discussed in Recipe 11.4, the session module allows users to define their own session handling
methods. While this flexibility is most commonly used to store session data in a database, you may
find that performance suffers with the overhead of the database connection and the subsequent
queries. If sharing session data across a bunch of web servers is not a concern, you can boost
session handling performance by storing that data in shared memory.
在第11.4的讨论中,这个session模块允许使用者定义他们自己的session讨论方法,它适用大多一般的经常存储
session数据在一个数据库,你可以找到受损害的session在数据库链接上面和后发的问题。如果共享session数据
横过web服务器而不涉及,你可以推进session处理执行存储这个数据在公用存储器
Before deciding to use shared memory for session storage, make sure that you can spare the amount of
memory that your traffic plus your average session data size will consume. The performance boost of
shared memory session storage won’t matter if your site’s sessions consume all available memory on
your system!
决定使用公用存储器存储session。确认你可以节省内存数量加上你的平均session数据量可以消耗。这个执行推进
公用存储器存储session.不用担心如果你的站点sessions消耗所有可利用的内容在你的系统
To store session data in shared memory, you need to have the shared memory functions explicitly
enabled by building PHP with –enable-shmop. You will also need the pc_Shm class shown in Example
11-3, as well as the pc_Shm_Session class shown in Example 11-3.
存储session数据在公用存储器,你需要明确公用存储器的函数记忆功能。激活建立PHP–enable-shmop你同样需要
这个pc_Shm 类例如在例子11-3当作pc_Shm_Session类。例如在例子11-3
class pc_Shm {
var $tmp;
var $size;
var $shm;
var $keyfile;
function pc_Shm($tmp = '') {
if (!function_exists('shmop_open')) {
trigger_error('pc_Shm: shmop extension is required.', E_USER_ERROR);
return;
}
if ($tmp != '' && is_dir($tmp) && is_writable($tmp)) {
$this->tmp = $tmp;
} else {
$this->tmp = '/tmp';
}
// default to 16k
$this->size = 16384;
return true;
}
function __construct($tmp = '') {
return $this->pc_Shm($tmp);
}
function setSize($size) {
if (ctype_digit($size)) {
$this->size = $size;
}
}
function open($id) {
$key = $this->_getKey($id);
$shm = shmop_open($key, 'c', 0644, $this->size);
if (!$shm) {
trigger_error('pc_Shm: could not create shared memory segment', E_USER_ERROR);
return false;
}
$this->shm = $shm;
return true;
}
function write($data) {
$written = shmop_write($this->shm, $data, 0);
if ($written != strlen($data)) {
trigger_error('pc_Shm: could not write entire length of data', E_USER_ERROR);
return false;
}
return true;
}
function read() {
$data = shmop_read($this->shm, 0, $this->size);
if (!$data) {
trigger_error('pc_Shm: could not read from shared memory block', E_USER_ERROR);
return false;
}
return $data;
}
function delete() {
if (shmop_delete($this->shm)) {
if (file_exists($this->tmp . DIRECTORY_SEPARATOR . $this->keyfile)) {
unlink($this->tmp . DIRECTORY_SEPARATOR . $this->keyfile);
}
}
return true;
}
function close() {
return shmop_close($this->shm);
}
function fetch($id) {
$this->open($id);
$data = $this->read();
$this->close();
return $data;
}
function save($id, $data) {
$this->open($id);
$result = $this->write($data);
if (! (bool) $result) {
return false;
} else {
$this->close();
return $result;
}
}
function _getKey($id) {
$this->keyfile = 'pcshm_' . $id;
if (!file_exists($this->tmp . DIRECTORY_SEPARATOR . $this->keyfile)) {
touch($this->tmp . DIRECTORY_SEPARATOR . $this->keyfile);
}
return ftok($this->tmp . DIRECTORY_SEPARATOR . $this->keyfile, 'R');
}
}
The pc_Shm class provides an object-oriented wrapper around PHP’s shmop functions. The
pc_Shm::_getKey( ) method provides a convenient way to transparently calculate a memory address,
which is often the biggest obstacle for people getting familiar with the shmop functions. By
abstracting the memory address, reading and writing from shared memory is as easy as manipulating a
value in an associative array.
这个pc_Shm类规定一个object_oriented环绕PHP的shmop函数。这个pc_Shm::_getKey()方法规定一个方便的方法去
明显的计算存储地址,那个是经常使用的大妨碍物人们获得熟悉的shmop函数,显示这个存储地址。读写公用存储
器更加简单熟练的一个值在一个联合数组
pc_Shm creates 16k memory blocks by default. To adjust the size of the blocks used, pass a value in
bytes to the pc_Shm::setSize( ) method.
pc_Shm 创造16K存储区。整修使用者大小通过一个数值在pc_Shm::setSize()方法
With pc_Shm defined, pc_Shm_Session has what it needs to easily provide custom methods for
session_set_save_handler( ). Example 11-3 shows the pc_Shm_Session class.
定义pc_Shm 它可以容易的规定习惯方法对于session_set_save_handler(),例子11-3显示这个pc_Shm_Session类
class pc_Shm_Session {
var $shm;
function pc_Shm_Session($tmp = '') {
if (!function_exists('shmop_open')) {
trigger_error("pc_Shm_Session: shmop extension is required.",E_USER_ERROR);
return;
}
if (! session_set_save_handler(array(&$this, '_open'),
array(&$this, '_close'),
array(&$this, '_read'),
array(&$this, '_write'),
array(&$this, '_destroy'),
array(&$this, '_gc'))) {
trigger_error('pc_Shm_Session: session_set_save_handler() failed', E_USER_ERROR);
return;
}
$this->shm = new pc_Shm();
return true;
}
function __construct() {
return $this->pc_Shm_Session();
}
function setSize($size) {
if (ctype_digit($size)) {
$this->shm->setSize($size);
}
}
function _open() {
return true;
}
function _close() {
return true;
}
function _read($id) {
$this->shm->open($id);
$data = $this->shm->read();
$this->shm->close();
return $data;
}
function _write($id, $data) {
$this->shm->open($id);
$this->shm->write($data);
$this->shm->close();
return true;
}
function _destroy($id) {
$this->shm->open($id);
$this->shm->delete();
$this->shm->close();
}
function _gc($maxlifetime) {
$d = dir($this->tmp);
while (false !== ($entry = $d->read())) {
if (substr($entry, 0, 6) == 'pcshm_') {
$tmpfile = $this->tmp . DIRECTORY_SEPARATOR . $entry;
$id = substr($entry, 6);
$fmtime = filemtime($tmpfile);
$age = now() - $fmtime;
if ($age >= $maxlifetime) {
$this->shm->open($id);
$this->shm->delete();
$this->shm->close();
}
}
}
$d->close();
return true;
}
}
Versions of Microsoft Windows prior to Windows 2000 do not support shared memory. Also, when using
PHP in a Windows server environment, shmop functions will only work if PHP is running as a web
server module, such those provided by Apache or IIS. CLI and CGI interfaces to PHP do not support
shmop functions under Windows.
微软2000操作系统之前的版本都不支持公用存储器,同样当使用PHP在一个Windows服务器环境。shmop函数仅仅可
以工作。如果PHP运行在一个web服务器模块 例如那些Apache或者IIS。CLT和CGI界面不支持shmop函数在Windows
?php
ini_set('session.save_path', '/dev/shm');
ini_get('session.auto_start') or session_start();