PHP 中的初学者数据缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2279316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:50:49  来源:igfitidea点击:

Beginner data caching in PHP

phpcachingtwitter

提问by Joshua Cody

I'm using simpleXML to go through the XML results of a Twitter XML file, but I'm completely lost as to caching the results with PHP. This articleseems to be of some help, but I've come across memcache (and memcached. C'mon, namers.) as well, and I have no idea what to do.

我正在使用 simpleXML 浏览 Twitter XML 文件的 XML 结果,但我完全不知道使用 PHP 缓存结果。这篇文章似乎有些帮助,但我也遇到了 memcache(和 memcached。来吧,命名者。),我不知道该怎么做。

I'm using this:

我正在使用这个:

$sxml = simplexml_load_file(
    'http://api.twitter.com/1/qworky/lists/qworkyteam/statuses.xml');

foreach($sxml->status as $status){
    $name = $status->user->name;
    $image = $status->user->profile_image_url;
    $update = $status->text;
    $url = "http://twitter.com/" . $status->user->screen_name;
}

to simply store the XML data of a Twitter list into usable variables. But what's the right thing to do? Create a cache file and only run this block of PHP if the cache file is older than ten minutes, otherwise serve up the cached variables? How do I pass the cached variables back-and-forth between the cached file and the DOM? Heck, what kind of extension and filename does a cache file have?

简单地将 Twitter 列表的 XML 数据存储到可用变量中。但正确的做法是什么?创建一个缓存文件,如果缓存文件早于十分钟,则只运行这块 PHP,否则提供缓存的变量?如何在缓存文件和 DOM 之间来回传递缓存变量?哎呀,缓存文件有什么样的扩展名和文件名?

Thanks so much for any way you can point me in a healthy direction here.

非常感谢您可以在这里为我指明一个健康的方向。

回答by fire

Joshua take a look at this articleabout simple PHP caching, you can do exactly as you've described with relative ease (and yes is probably the most sensible way to go).

Joshua 看看这篇关于简单 PHP 缓存的文章,您可以相对轻松地完全按照您的描述进行操作(是的,这可能是最明智的方法)。

回答by casperOne

As a general concept, cachingdoesn't imply a strategy for implementation. The prevalent idea is that you store the information somewhere that provides you more efficent access than where you obtained the data from originally.

作为一般概念,缓存并不意味着实施策略。普遍的想法是,将信息存储在比最初获取数据的位置提供更有效访问的位置。

So in this case, it's more efficient to get the data from the disk than it is to requery Twitter (generally, network latencyis greater than disk IO latency).

所以在这种情况下,从磁盘获取数据比重新查询 Twitter 更有效(通常,网络延迟大于磁盘 IO 延迟)。

Also, getting data from memory is more efficient than getting the information from disk (because memory latency is less than disk IO latency).

此外,从内存获取数据比从磁盘获取信息更有效(因为内存延迟小于磁盘 IO 延迟)。

That being said, you can store the values from Twitter in memory, if you wish, or to a file on disk, if you need the values to persist beyond say, a shutdown. How you do it is up to you (disk or memory, extensions, format, etc, etc). It's your cache.

话虽如此,如果您愿意,您可以将 Twitter 中的值存储在内存中,或者存储到磁盘上的文件中,如果您需要这些值在关机后保持不变。如何操作取决于您(磁盘或内存、扩展名、格式等)。这是你的缓存。

The thing you have to be careful of is the cache growing stale. This is when the information that you have in your cache is out of sync with the original data source. You have to make the determination for your application how acceptable stale data is, and requery as appropriate, replacing your cache values.

您必须小心的是缓存越来越陈旧。这是当您在缓存中拥有的信息与原始数据源不同步时。您必须为您的应用程序确定陈旧数据的可接受程度,并根据需要重新查询,替换您的缓存值。