I'm working on some old(ish) software in PHP that maintains a $cache array to reduce the number of SQL queries. I was thinking of just putting memcached in its place and I'm wondering whether or not to get rid of the internal caching. Would there still be a worthwihle performance increase if I keep the internal caching, or would memcached suffice?
-
1Benchmark the different approaches within the scope of your application and see what happens. – salathe Jun 14 '10 at 14:08
-
How does it maintain the `$cache` array across page views? Is it serialized to a file? Is it stored in APC/Eaccelerator/Memcached/DB/etc? – ircmaxell Jun 14 '10 at 14:10
-
It seems current approach is useless and needs to be reviewed. What's the use of cache in one script runtime? It looks like more poor design than real need in cache. Does this app really need any caching? – Your Common Sense Jun 14 '10 at 14:32
-
ircmaxell, it's a global variable, no encapsulation so it's referenced thousands of times in the code... after taking a closer look it seems that this global $cache array isn't caching at all, but is used to avoid passing excessive arguments to functions. Argh! – bcoughlan Jun 14 '10 at 15:10
3 Answers
According to this blog post, the PHP internal array is way faster than any other method:
Cache Type Cache Gets/sec
Array Cache 365000
APC Cache 98000
File Cache 27000
Memcached Cache (TCP/IP) 12200
MySQL Query Cache (TCP/IP) 9900
MySQL Query Cache (Unix Socket) 13500
Selecting from table (TCP/IP) 5100
Selecting from table (Unix Socket) 7400

- 3,228
- 1
- 27
- 46
-
1I have a lot of those array-like cache. It is indeed faster. However, I'm introducing MemCache slowly for 2 reasons: 1) Some of this arrays are duplicated somehow across other scripts, using MemCache would unify this 2). Even though arrays are faster, they consume memory and the bigger the array the more memory. And MemCache is fast enough. ** In conclusion: For me I've decided to Use MemCache. Sometimes I keep both for a while, add a TODO comment and remove the array a couple of weeks later. Hope this helps – tanovellino Jan 03 '18 at 09:13
-
Redis is also very interesting, especially with the ability to store the data on disk, and with the possibility to easily sync the data between multiple servers. And if you want really high performance with php, then I recommend to look into [Swoole](https://github.com/swoole/swoole-src). You will get extremely low response times for any cached page (on my pc, redis cached html pages are loading in ~2ms with swoole, instead of ~10ms with traditional php 7 - the server is [h2o](https://h2o.examp1e.net/) in both cases, the pages were loaded in firefox). – István Ujj-Mészáros Jan 03 '18 at 13:22
It seems likely that memcache (which is implemented on the metal) would be faster than some php interpreted caching scheme.
However: if it's not broken, don't fix it.
If you remove the custom caching code, you might have to deal with other code that depends on the cache. I can't speak for the quality of the code you have to maintain but it seems like one of those "probably not worth it" things.
Let me put it this way: Do you trust the original developer(s) to have written code that will still work if you rip out the caching? (I probably wouldn't)
So unless the existing caching is giving you problems I would recommend against taking it out.

- 40,604
- 9
- 72
- 101
-
3How do you figure that Memcache would be faster than an interpreted system (In general)? It involves TCP communication... Memcache is faster than querying the DB since both hit the network, but it's not necessarily faster than reading a php file from disk (`$foo = include('myfoofile.php');` where myfoofile.php contains ` – ircmaxell Jun 14 '10 at 14:14
-
Great advice... the situation is that the lead developer has left and left me (the new guy!) a mess of undocumented code. The caching isn't encapsulated at all, it's a global variable referenced thousands of times in the code that really affects readability and reeks of bad design, so from an architecture point of view (in my opinion!) it is broken :-) – bcoughlan Jun 14 '10 at 15:01
-
@ircmaxell: I am assuming both hit the network because his question is about database access. – Kris Jun 14 '10 at 15:20
There's an advantage in using memcache vs local caching if:
1) you have mulitple webservers running off the same database, and have memcache set up to run across multiple nodes
2) the database does not implement query result caching or is very slow to access
Otherwise, unless the caching code is very poor, you shouldn't expect to see much performance benefit.
HTH
C.

- 47,736
- 6
- 59
- 94