如何在php中构建内存服务器端缓存?

发布于 2024-12-09 11:09:42 字数 164 浏览 5 评论 0原文

我试图通过提供内存缓存来减少数据库访问量。 我知道我可以通过使用会话和 cookie 来获取缓存,但这仅适用于每个客户端。如果每个会话都进行一次相同的查询,那么缓存将毫无用处。但我只想访问数据库一次并将其缓存。

有没有办法在内存中创建一个可由服务器端脚本访问的缓存?我不想将缓存数据存储在文件中..

I am trying to reduce the amount of database access by providing a in memory cache.
I understand that I can get a cache by using session and cookies, however this only works on a per client basis. if the same query is made once in every session, then the cache will be useless. But I only want to access the database once and have it cached.

Is there a way to make create a cache in memory that can be accessed by the server side script? I don't want to store the cache data in a file ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

最美不过初阳 2024-12-16 11:09:42

You can use MemCache, if installed.

Another option is creating a table in MySQL with storage type MEMORY. You will still use the database, but it will be quite a lot faster. But MemCache will really cut the load on the database, because you can cache the data outside the database, even on a different server.

与他有关 2024-12-16 11:09:42

在共享托管空间上,您可能没有可用的内存缓存。在这种情况下,请检查 APC。如果您在多台机器上运行,Memcache 确实很强大。如果您在单个 Web 服务器上运行站点,APC 将完成相同的工作(开销较低)。

On a shared hosting space you may not have memcache available. Check APC in that case. Memcache really rocks if you're running on multiple machines. If you're running your site on a single web server APC will do the same job (with lower overhead).

淡看悲欢离合 2024-12-16 11:09:42

如果您想要拥有多台服务器或能够使用多台计算机访问缓存,Memecached/Memcache 是使用最广泛的解决方案。

但是,如果您只希望缓存位于本地(在运行 PHP 代码的同一台机器上),您还有其他更简单的选择:

APC 模块 还可以使用 < code>apc_add、apc_fetch 等...

或者您可以在 tmpfs 中使用标准文件。这样做的优点是完全可移植,如果任何缓存系统使用文件,您可以非常快速地设置它。

Memecached/Memcache are the most widely used solutions if you want to have multiple servers or be able to access the cache with several machines.

However, if you only want your cache to be local (on the same machine that runs the PHP code), you have other, somewhat easier choices :

The APC module can also store stuff in the memory with apc_add, apc_fetch etc…

Or you can use standard files in a tmpfs. This has the advantage of being completely portable, and you can set it up very quickly if any caching system uses files.

掩耳倾听 2024-12-16 11:09:42

尝试 MemCache

Memcache 模块为 memcached 提供方便的过程和面向对象接口,高效的缓存守护进程,专门为减少动态 Web 应用程序中的数据库负载而设计。

Try MemCache

Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

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