php simplexml 对象缓存

发布于 2024-12-10 15:49:48 字数 1616 浏览 0 评论 0原文

考虑以下用于从缓存的 RSS(XML) 源缓存对象的函数原型:

function cacheObject($xml,$name,$age = 3600)
  { 
    // directory in which to store cached files
    $cacheDir = "cache/";
    // cache filename
    $filename = $cacheDir.$name;
    // default to fetch the file
    $cache = true;
    // but if the file exists, don't fetch if it is recent enough
    if (file_exists($filename))
    {
      $cache = (filemtime($filename) < (time()-$age));
    }
    // fetch the file if required
    if ($cache)
    {
      $item = $xml->channel->item;
      file_put_contents($filename,serialize($item));
      // update timestamp to now
      touch($filename);
    }
    // return the cache filename
    return unserialize(file_get_contents($filename));
  }   

函数调用如下:

$urlD = "http://somerss.php";
    $xmlD = simplexml_load_file(cacheFetch($urlD,'cachedfeedD.xml',3600));
    $itemD = '';
    if($xmlD === FALSE)
        {$itemD = '';}
    else
        {$itemD = cacheObject($xmlD,'cacheobjectD',3600);}
 $urlM = "somerss2.php";
    $xmlM = simplexml_load_file(cacheFetch($urlM,'cachedfeedM.xml',3600));
    $itemM = '';
    if($xmlM ===  FALSE) 
        {$itemM = '';}
    else
        {$itemM = cacheObject($xmlM,'cacheobjectM',3600);}

我收到以下错误:

    Fatal error: Uncaught exception 'Exception' 
with message 'Serialization of 'SimpleXMLElement' is not allowed' in C:\xampp\htdocs\sitefinal\cacheObject.php:20 Stack trace: #0 C:\xampp\htdocs\sitefinal\cacheObject.php(20): serialize(Object(SimpleXMLElement)) 

非常感谢使该程序正常工作的任何帮助。

Consider the following function prototype for caching object from cached RSS(XML) feed:

function cacheObject($xml,$name,$age = 3600)
  { 
    // directory in which to store cached files
    $cacheDir = "cache/";
    // cache filename
    $filename = $cacheDir.$name;
    // default to fetch the file
    $cache = true;
    // but if the file exists, don't fetch if it is recent enough
    if (file_exists($filename))
    {
      $cache = (filemtime($filename) < (time()-$age));
    }
    // fetch the file if required
    if ($cache)
    {
      $item = $xml->channel->item;
      file_put_contents($filename,serialize($item));
      // update timestamp to now
      touch($filename);
    }
    // return the cache filename
    return unserialize(file_get_contents($filename));
  }   

The function calls are as follows:

$urlD = "http://somerss.php";
    $xmlD = simplexml_load_file(cacheFetch($urlD,'cachedfeedD.xml',3600));
    $itemD = '';
    if($xmlD === FALSE)
        {$itemD = '';}
    else
        {$itemD = cacheObject($xmlD,'cacheobjectD',3600);}
 $urlM = "somerss2.php";
    $xmlM = simplexml_load_file(cacheFetch($urlM,'cachedfeedM.xml',3600));
    $itemM = '';
    if($xmlM ===  FALSE) 
        {$itemM = '';}
    else
        {$itemM = cacheObject($xmlM,'cacheobjectM',3600);}

I get the following error:

    Fatal error: Uncaught exception 'Exception' 
with message 'Serialization of 'SimpleXMLElement' is not allowed' in C:\xampp\htdocs\sitefinal\cacheObject.php:20 Stack trace: #0 C:\xampp\htdocs\sitefinal\cacheObject.php(20): serialize(Object(SimpleXMLElement)) 

Any help making this program to work is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杆小烟枪 2024-12-17 15:49:48

可能是 SimpleXMLElement 类,例如 许多内置 PHP 对象无法序列化

相反,您可以调用类方法 asXML (它返回一个有效的 XML字符串(如果不传递任何参数)并序列化它。然后,您可以通过调用 simplexml_load_string() 在此字符串上。

Probably, the SimpleXMLElement class, like many built-in PHP objects, cannot be serialized.

Instead, you could call the class method asXML (which returns a valid XML string if you pass no parameters) and serialize this. You can then recreate the SimpleXMLElement class by calling simplexml_load_string() on this string.

咋地 2024-12-17 15:49:48

Magpierss(免费开源)应该缓存外部 xml 文件。我几年前用过它。您设置软件再次提取 xml 文件的时间范围。效果很好。我看到的唯一问题是,无论是否有前端请求,它都会不断拉取 xml 文件,从而耗尽服务器。不过,我认为可能有解决办法。祝你好运。

Magpierss (free open source) is supposed to cache external xml files. I used it some years ago. You set the software a time frame to pull the xml file again. It worked well. Only problem I saw was that it kept pulling the xml file whether there was a frontend request for it or not which was using up the server. I think there might be a fix for that however. Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文