Caching data in PHP

2019-03-19 10:26发布

I'm developing a site in PHP which has some complex SQL queries and I would like to implement a caching feature to reduce the load on the database.

I'm just wondering would it be better to write and read HTML directly to a file or perhaps create something like a YAML file with a delimiter to separate records then wrap it in HTML with a function?

My thinking is that this would allow user options (for example, number of records to display) to be applied to the request.

Any advice / suggestions appreciated.

Thanks.

标签: php caching
4条回答
forever°为你锁心
2楼-- · 2019-03-19 10:41

Although I've never personally used it before, there is a PEAR package for caching. Consider it amongst other options like Zend_Cache.

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-19 10:46

You can implement caching on multiple places in your application:

  • you can implement conditional GET (ETag and Last-Modified HTTP headers)
  • do data caching with the mentioned solutions (Cache_Lite, Zend_Cache, APC) with multiple backends (file, memcached, shared memory)
  • you can cache the template files as you said (Smarty)
查看更多
叛逆
4楼-- · 2019-03-19 10:51

Use any of these for caching backends:

and use Zend_Cache for a unified interface to them

Whether to use full page caching or partial caching depends on your specific UseCases. Usually, your application will have a mix of both.

查看更多
【Aperson】
5楼-- · 2019-03-19 10:54

You might want to take a look at Zend Cache
http://framework.zend.com/manual/de/zend.cache.html

I usually store the result of the SQL queries somewhere (depending on what you are looking for, i.e. sometimes is caching it in $_SESSION ok (user related data), sometimes it's not (big resultsets for not user dependant queries) and then my class just checks if the cache exists and continues accordingly.

查看更多
登录 后发表回答