how to build a in-memory server side cache in php?
Asked Answered
B

4

8

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

Brodench answered 13/10, 2011 at 17:54 Comment(1)
memcached is the standard way. php.net/manual/en/book.memcached.phpChrisman
A
4

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.

Admissible answered 13/10, 2011 at 17:56 Comment(0)
S
2

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

Sternwheeler answered 13/10, 2011 at 18:17 Comment(1)
Thanks, I will take a look at this as well, could be more suitable in my caseBrodench
F
1

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.

Fernandes answered 13/10, 2011 at 19:4 Comment(0)
M
0

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.

Misbegotten answered 13/10, 2011 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.