PDO和缓存,如何在数据库类中实现它? [英] PDO and caching, how to implement it in a database class?

查看:210
本文介绍了PDO和缓存,如何在数据库类中实现它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PDO和MySQL时,缓存结果有什么好处,我知道我将在同一页面上多次使用?或者PDO / MySQL是否自动处理这种事情?

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?

如果我应该自己做,我应该存储查询的实际结果,在缓存中的PDOS条目并重用它们?

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?

当然,我可以存储任何结果,我知道我要在一个页面上多次使用变量,但它只是似乎更清洁,让我的数据库类透明地处理这种事情。例如,我想能够调用$ DB-> get_username_by_id($ id),而不必担心它是直接从数据库还是从缓存。

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.

或者我可能会放弃建立自己的数据库类的想法,如果你有任何一个人可以指向一个超轻量级,易于使用,有效,多才多艺的一个将使用PDO MySQL共享主机...

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...

推荐答案

这可以帮助您 PHP PDO缓存

如果您使用PDO,那么PDO :: Prepare是您的朋友:

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

为使用不同参数值多次发出的语句调用PDO :: prepare()和PDOStatement :: execute()通过允许驱动程序协商客户端和/或服务器端缓存查询计划和元信息,并通过消除手动引用参数来帮助防止SQL注入攻击。

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

这篇关于PDO和缓存,如何在数据库类中实现它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆