PHP变量缓存
我的 MySQL 数据库中有一些数据大部分时间都是静态的。它们主要是几乎静态的价值观,如城市、州和种族。我想将它们缓存在一个变量中,最好是在内存中,这样我就不需要在每次加载页面时对 MySQL 执行另一个查询。
问题是,我的主机不支持 memcache 也不支持 APC。我能找到的唯一加速器是 eAccelerator,但我认为它无法实现我的想法。
有什么办法可以进行缓存吗?这是 http://www.k-disk.net
谢谢
I have some data in MySQL database that are static most of the time. They are mainly almost static values like cities, states, and ethnic. I want to cache them in a variable, preferebly in memory, so that I won't need to perform another query to MySQL every time a page loads.
The problem is, my hosting doesn't support memcache nor APC. The only accelerator I could find is eAccelerator, and I don't think it will do what I have in mind.
Is there any way I can do caching? It is http://www.k-disk.net
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有很多方法在网站上实施缓存的多个级别。让我们从前端开始,逐渐转向后端,而不涉及太多细节。
HTTP 缓存
请参阅如何使用 HTTP 缓存优化您的网站< /a>
应用程序级缓存
这是“查询成本高昂”的数据库对象的缓存。
例如:memcache、缓存文件中的页面等。
操作码缓存
例如:PHP 加速器、eAccelerator 等。
数据库级缓存
根据需要和机器硬件调整数据库参数来优化数据库。
对于你的情况,我建议使用 my.cnf 进行调整,因为只要有足够的 RAM,MySQL 就相当快。只是尽量不要预先优化。
There are many ways at many levels to implement cache on a website. Lets look at them starting form the front end and moving towards the backend without getting into too much details.
HTTP caching
See How To Optimize Your Site With HTTP Caching
Application level caching
This is caching of "expensive to query" database objects.
Eg: memcache, caching pages in files etc.
Op-code cache
Eg: PHP accelerator, eAccelerator etc.
Database level cache
Optimizing the database by tuning its parameters based on the need and machine hardware.
In your case, I would recommend tweaking around with my.cnf since given enough RAM, MySQL is quite fast. Just try not to pre-optimize.
您可以使用共享内存扩展。
看这个基本示例: http://www.php.net/manual/ en/shmop.examples-basic.php
You can use the Shared Memory extension.
Look this basic sample: http://www.php.net/manual/en/shmop.examples-basic.php
您只需将一个文件写入服务器来保存序列化的 php 变量数组。只需将所有变量插入关联数组,然后序列化并保存即可。虽然我真的不明白为什么你不将变量保存到数据库中的变量表中。这不是一个昂贵的操作。
如果这对你不起作用,如果你有 SSH 访问权限,可以通过一种方法在你的共享主机上获取 memcache。我实际上是在 hostmonster 上这样做的。这是一个演练(尽管这不是我最初使用的文章)。
http://andrewpeng.net/posts/2011 /06/271273-memcached-and-dreamhost-shared-tutorial.html
You could just write a file to your server that saves a serialized php array of variables. Just plug all of your variables into an associative array then serialize and save. Though I honestly don't see why you don't save the variables to a variable table in the database. Its not an expensive operation.
If that doesn't work for you there is a way to get memcache on your shared host if you have SSH access. I actually did this on hostmonster. Here is a walk through on it (though this is not the article I originally used).
http://andrewpeng.net/posts/2011/06/271273-memcached-and-dreamhost-shared-tutorial.html
您可以使用 file_put_contents() 和 file_get_contents() 创建自定义缓存类,从磁盘存储和读取数据。
You can create a custom cache class storing and reading data from disk, using file_put_contents() and file_get_contents().