gpt4 book ai didi

php - 这是使用内存缓存的最佳方式吗?

转载 作者:行者123 更新时间:2023-12-04 21:24:27 28 4
gpt4 key购买 nike

我昨晚才开始玩 memcache(d),所以我有很多东西要学

我想知道这段代码是否是完成它正在做的事情的好方法,或者我是否应该使用其他内存缓存函数

我想显示某物的缓存版本,如果缓存不存在,那么我从 mysql 生成内容并将其设置到缓存中,然后在页面上显示 mysql 结果,然后下一页加载它将检查缓存并查看它在那里,所以它会显示它。

这段代码似乎可以解决问题,但有几种不同的内存缓存函数,我应该使用其他函数来实现吗?

<?PHP
$memcache= new Memcache();
$memcache->connect('127.0.0.1', 11211);

$rows2= $memcache->get('therows1');
if($rows2 == ''){
$myfriends = findfriend2(); // this function gets our array from mysql
$memcache->set('therows1', $myfriends, 0, 30);
echo '<pre>';
print_r($myfriends); // print the mysql version
echo '</pre>';
}else{
echo '<pre>';
print_r($rows2); //print the cached version
echo '</pre>';
}
?>

这是@crescentfresh 发布的链接中提供的锁定功能

<?PHP
// {{{ locked_mecache_update($memcache,$key,$updateFunction,$expiryTime,$waitUTime,$maxTries)
/**
* A function to do ensure only one thing can update a memcache at a time.
*
* Note that there are issues with the $expiryTime on memcache not being
* fine enough, but this is the best I can do. The idea behind this form
* of locking is that it takes advantage of the fact that
* {@link memcache_add()}'s are atomic in nature.
*
* It would be possible to be a more interesting limiter (say that limits
* updates to no more than 1/second) simply by storing a timestamp or
* something of that nature with the lock key (currently stores "1") and
* not deleitng the memcache entry.
*
* @package TGIFramework
* @subpackage functions
* @copyright 2009 terry chay
* @author terry chay &lt;tychay@php.net&gt;
* @param $memcache memcache the memcache object
* @param $key string the key to do the update on
* @param $updateFunction mixed the function to call that accepts the data
* from memcache and modifies it (use pass by reference).
* @param $expiryTime integer time in seconds to allow the key to last before
* it will expire. This should only happen if the process dies during update.
* Choose a number big enough so that $updateFunction will take much less
* time to execute.
* @param $waitUTime integer the amount of time in microseconds to wait before
* checking for the lock to release
* @param $maxTries integer maximum number of attempts before it gives up
* on the locks. Note that if $maxTries is 0, then it will RickRoll forever
* (never give up). The default number ensures that it will wait for three
* full lock cycles to crash before it gives up also.
* @return boolean success or failure
*/
function locked_memcache_update($memcache, $key, $updateFunction, $expiryTime=3, $waitUtime=101, $maxTries=100000)
{
$lock = 'lock:'.$key;

// get the lock {{{
if ($maxTries>0) {
for ($tries=0; $tries< $maxTries; ++$tries) {
if ($memcache->add($lock,1,0,$expiryTime)) { break; }
usleep($waitUtime);
}
if ($tries == $maxTries) {
// handle failure case (use exceptions and try-catch if you need to be nice)
trigger_error(sprintf('Lock failed for key: %s',$key), E_USER_NOTICE);
return false;
}
} else {
while (!$memcache->add($lock,1,0,$expiryTime)) {
usleep($waitUtime);
}
}
// }}}
// modify data in cache {{{
$data = $memcache->get($key, $flag);
call_user_func($updateFunction, $data); // update data
$memcache->set($key, $data, $flag);
// }}}
// clear the lock
$memcache->delete($lock,0);
return true;
}
// }}}
?>

最佳答案

一些事情。

  1. 您应该在 return value from get() 中使用 === 检查 false,而不是 '' . php 的类型转换使您无需在此处执行此操作,但恕我直言,最好明确说明您从缓存查找中查找的值
  2. 在空检查和 set() 数据库结果之间存在竞争条件。来自 http://code.google.com/p/memcached/wiki/FAQ#Race_conditions_and_stale_data :

    Remember that the process of checking memcached, fetching SQL, and storing into memcached, is not atomic at all!

    这种情况的症状是当 key 过期时数据库 CPU 出现峰值,并且(在高容量站点上)一堆请求同时尝试访问数据库并缓存值。

    您可以使用 add() 解决它而不是 get。查看更具体的示例 here .

关于php - 这是使用内存缓存的最佳方式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1210207/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com