如何在php中构建内存服务器端缓存?
我试图通过提供内存缓存来减少数据库访问量。 我知道我可以通过使用会话和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 MemCache(如果已安装)。
另一个选择是在 MySQL 中创建一个存储类型为 MEMORY< /a>.您仍将使用数据库,但速度会快得多。但 MemCache 确实会减少数据库的负载,因为您可以将数据缓存在数据库之外,甚至可以缓存在不同的服务器上。
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.
在共享托管空间上,您可能没有可用的内存缓存。在这种情况下,请检查 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).
如果您想要拥有多台服务器或能够使用多台计算机访问缓存,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.尝试 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.