php中基于ram的wordpress缓存的php互斥体
我试图在php中实现一个高流量wp站点的缓存。到目前为止,我成功地将结果存储到ramfs中,并直接从htaccess加载它们。然而,在高峰时段,某个页面中会出现多个进程,这正成为一个问题php中基于ram的wordpress缓存的php互斥体,php,wordpress,Php,Wordpress,我试图在php中实现一个高流量wp站点的缓存。到目前为止,我成功地将结果存储到ramfs中,并直接从htaccess加载它们。然而,在高峰时段,某个页面中会出现多个进程,这正成为一个问题 我认为互斥锁会有所帮助,我想知道是否有比系统更好的方法(“mkdir cache.mutex”)我同意@gries的观点,反向代理将是一个非常好的方法,可以让高容量Wordpress站点获得高性能。我已经成功地利用了Varnish,不过我怀疑您也可以使用nginx来实现这一点。据我所知,您希望确保一次只有一个进
我认为互斥锁会有所帮助,我想知道是否有比系统更好的方法(“mkdir cache.mutex”)我同意@gries的观点,反向代理将是一个非常好的方法,可以让高容量Wordpress站点获得高性能。我已经成功地利用了Varnish,不过我怀疑您也可以使用nginx来实现这一点。据我所知,您希望确保一次只有一个进程运行某段代码。可以使用互斥或类似的机制来实现这一点。我自己使用lockfiles来提供一个解决方案,该解决方案可以在许多平台上运行,并且不依赖于仅在Linux等平台上可用的特定库 为此,我编写了一个小型的
Lock
类。请注意,它使用了我的库中的一些非标准函数,例如,获取存储临时文件的位置等,但您可以轻松地更改它
<?php
class Lock
{
private $_owned = false;
private $_name = null;
private $_lockFile = null;
private $_lockFilePointer = null;
public function __construct($name)
{
$this->_name = $name;
$this->_lockFile = PluginManager::getInstance()->getCorePlugin()->getTempDir('locks') . $name . '-' . sha1($name . PluginManager::getInstance()->getCorePlugin()->getPreference('EncryptionKey')->getValue()).'.lock';
}
public function __destruct()
{
$this->release();
}
/**
* Acquires a lock
*
* Returns true on success and false on failure.
* Could be told to wait (block) and if so for a max amount of seconds or return false right away.
*
* @param bool $wait
* @param null $maxWaitTime
* @return bool
* @throws \Exception
*/
public function acquire($wait = false, $maxWaitTime = null) {
$this->_lockFilePointer = fopen($this->_lockFile, 'c');
if(!$this->_lockFilePointer) {
throw new \RuntimeException(__('Unable to create lock file', 'dliCore'));
}
if($wait && $maxWaitTime === null) {
$flags = LOCK_EX;
}
else {
$flags = LOCK_EX | LOCK_NB;
}
$startTime = time();
while(1) {
if (flock($this->_lockFilePointer, $flags)) {
$this->_owned = true;
return true;
} else {
if($maxWaitTime === null || time() - $startTime > $maxWaitTime) {
fclose($this->_lockFilePointer);
return false;
}
sleep(1);
}
}
}
/**
* Releases the lock
*/
public function release()
{
if($this->_owned) {
@flock($this->_lockFilePointer, LOCK_UN);
@fclose($this->_lockFilePointer);
@unlink($this->_lockFile);
$this->_owned = false;
}
}
}
过程2
$lock = new Lock('runExpensiveFunction');
// Check will be false since the lock will already be held by someone else so the function is skipped
if($lock->acquire()) {
// Some expensive function that should only run one at a time
runExpensiveFunction();
$lock->release();
}
另一种选择是让第二个进程等待第一个进程完成,而不是跳过代码
$lock = new Lock('runExpensiveFunction');
// Process will now wait for the lock to become available. A max wait time can be set if needed.
if($lock->acquire(true)) {
// Some expensive function that should only run one at a time
runExpensiveFunction();
$lock->release();
}
Ram磁盘
要限制使用锁定文件写入HDD/SSD的次数,可以创建一个RAM磁盘来存储这些文件
在Linux上您可以向/etc/fstab
tmpfs /mnt/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=1024M 0 0
在Windows上您可以下载类似的内容并用它创建一个ramdisk
为什么不使用反向代理来消除服务器负载?
tmpfs /mnt/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=1024M 0 0