PDO and caching, how to implement it in a database class?

痴心易碎 提交于 2020-01-06 04:36:14

问题


When using PDO and MySQL, is there any benefit in caching results that I know I am going to be using multiple times on the same page? Or does PDO / MySQL automatically handle this sort of thing?

And if I should do it myself, should I store the actual results from a query, or could I just store the PDOStatements in a cache and reuse them?

Of course I could store any result I know I'm going to use multiple times on a page in a variable, but it just seems cleaner to let my database class handle this sort of thing transparently. For example, I want to be able to call $DB->get_username_by_id($id) and don't have to worry about whether it comes straight from the DB or from a cache.

Or maybe I'll abandon the idea of making my own database class, if any of you guys can point me towards a super lightweight, easy to use, effective, versatile one that'll work with PDO an MySQL on a shared host...


回答1:


this can help you PHP PDO Caching

If you use PDO then PDO::Prepare is your friend:

Calling PDO::prepare() and PDOStatement::execute() for statements that will be issued multiple times with different parameter values optimizes the performance of your application by allowing the driver to negotiate client and/or server side caching of the query plan and meta information, and helps to prevent SQL injection attacks by eliminating the need to manually quote the parameters.

http://php.net/manual/en/pdo.prepare.php




回答2:


Well id always recommend using an existing library over something custom unless you have a really good excuse not to. In this case id say Doctrine DBAL since you dont seem to be using objects to hold your data. If you want to go the full OOP route and map data to particular objects then go with the full Doctrine ORM.



来源:https://stackoverflow.com/questions/10194223/pdo-and-caching-how-to-implement-it-in-a-database-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!