PHP变量缓存

发布于 2024-11-19 22:29:55 字数 298 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

说不完的你爱 2024-11-26 22:29:55

缓存是无处不在的时空权衡的一个很好的例子
编程。您可以通过使用空间来存储结果来节省时间。 [1]

有很多方法在网站上实施缓存的多个级别。让我们从前端开始,逐渐转向后端,而不涉及太多细节。

HTTP 缓存

请参阅如何使用 HTTP 缓存优化您的网站< /a>

应用程序级缓存

这是“查询成本高昂”的数据库对象的缓存。
例如:memcache、缓存文件中的页面等。

操作码缓存

例如:PHP 加速器、eAccelerator 等。

数据库级缓存

根据需要和机器硬件调整数据库参数来优化数据库。

对于你的情况,我建议使用 my.cnf 进行调整,因为只要有足够的 RAM,MySQL 就相当快。只是尽量不要预先优化。

Caching is a great example of the ubiquitous time-space tradeoff in
programming. You can save time by using space to store results. [1]

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.

噩梦成真你也成魔 2024-11-26 22:29:55

您可以使用共享内存扩展。

看这个基本示例: 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

狼性发作 2024-11-26 22:29:55

您只需将一个文件写入服务器来保存序列化的 php 变量数组。只需将所有变量插入关联数组,然后序列化并保存即可。虽然我真的不明白为什么你不将变量保存到数据库中的变量表中。这不是一个昂贵的操作。

$myvars = array(
  'how_high_do_i_jump' => 10,
  'which_tv_show_is_best' => 'Glee',
  'enable_caching' => true,
  'mathematical_solution' => 4534.234
);

$file_str = serialize($myvars);

//Save the file
file_put_contents('myvars.ser', $myvars);

//To reverse just do this
$file = file_get_contents('myvars.ser');

$myvars = unserialize($file);

如果这对你不起作用,如果你有 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.

$myvars = array(
  'how_high_do_i_jump' => 10,
  'which_tv_show_is_best' => 'Glee',
  'enable_caching' => true,
  'mathematical_solution' => 4534.234
);

$file_str = serialize($myvars);

//Save the file
file_put_contents('myvars.ser', $myvars);

//To reverse just do this
$file = file_get_contents('myvars.ser');

$myvars = unserialize($file);

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

静待花开 2024-11-26 22:29:55

您可以使用 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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文