I';我试图将php脚本中的数据保存在memcached中,以便我的网站可以直接使用这些数据
脚本工作正常,正在设置数据,但网站代码无法使用它,而是在设置自己的memcached值。我的网站代码是在codeIgniter框架中编写的。我不知道为什么会这样 我的脚本代码:-I';我试图将php脚本中的数据保存在memcached中,以便我的网站可以直接使用这些数据,php,codeigniter,memcached,Php,Codeigniter,Memcached,脚本工作正常,正在设置数据,但网站代码无法使用它,而是在设置自己的memcached值。我的网站代码是在codeIgniter框架中编写的。我不知道为什么会这样 我的脚本代码:- function getFromMemcached($string) { $memcached_library = new Memcached(); $memcached_library->addServer('localhost', 11211); $result = $memcach
function getFromMemcached($string) {
$memcached_library = new Memcached();
$memcached_library->addServer('localhost', 11211);
$result = $memcached_library->get(md5($string));
return $result;
}
function setInMemcached($string,$result,$TTL = 1800) {
$memcached_library = new Memcached();
$memcached_library->addServer('localhost', 11211);
$memcached_library->set(md5($string),$result, $TTL);
}
/*---------- Function stores complete product page as one function call cache -----------------*/
function getCachedCompleteProduct($productId,$brand)
{
$result = array();
$result = getFromMemcached($productId." product page");
if(true==empty($result))
{
//------- REST CODE storing data in $result------
setInMemcached($productId." product page",$result,1800);
}
return $result;
}
网站代码:-
private function getFromMemcached($string) {
$result = $this->memcached_library->get(md5($string));
return $result;
}
private function setInMemcached($string,$result,$TTL = 1800) {
$this->memcached_library->add(md5($string),$result, $TTL);
}
/*---------- Function stores complete product page as one function call cache -----------------*/
public function getCachedCompleteProduct($productId,$brand)
{
$result = array();
$result = $this->getFromMemcached($productId." product page");
if(true==empty($result))
{
// ----------- Rest Code storing data in $result
$this->setInMemcached($productId." product page",$result,1800);
}
return $result;
}
这是在memcached中保存数据。我通过在if条件内打印并根据CodeIgniter文档检查最终结果进行了检查,您可以使用:
class YourController extends CI_Controller() {
function __construct() {
$this->load->driver('cache');
}
private function getFromMemcached($key) {
$result = $this->cache->memcached->get(md5($key));
return $result;
}
private function setInMemcached($key, $value, $TTL = 1800) {
$this->cache->memcached->save(md5($key), $value, $TTL);
}
public function getCachedCompleteProduct($productId,$brand) {
$result = array();
$result = $this->getFromMemcached($productId." product page");
if( empty($result) ) {
// ----------- Rest Code storing data in $result
$this->setInMemcached($productId." product page",$result,1800);
}
return $result;
}
}
如果第三方库已经存在于核心框架中,那么个人应尽量避免使用它。我已经对此进行了测试,它工作得非常好,所以应该可以为您解决这个问题:)
请记住按照中的说明为memcache服务器设置所需的配置请提供有关此问题的更多信息。可能包括如何从脚本以及网站中设置和读取memcache密钥的代码段。还可能包括您如何验证脚本成功设置数据。请确保您正在写入正确的memcache服务器,该服务器与网站代码可访问的服务器相同。