16

I'm looking for the fastest in-memory cache/hashtable available for PHP.

I will be storing some system-configuration values in it and I'm trying to get the least possible overhead.

The data will be small and granular.

What would you recommend and why?

thwd
  • 23,956
  • 8
  • 74
  • 108
  • Are you in multiple server environment? (more than one server) – ajreal Sep 05 '11 at 14:22
  • 5
    Hello and welcome to StackOverflow. Note that as it stands at revision 1, your question is little more than "which X is best?" which doesn't serve any useful purpose beyond inciting flamewars (see the answers for some fine examples). Various caching systems have different strengths and weaknesses - what is your specific situation? – Piskvor left the building Sep 05 '11 at 14:38
  • 2
    Fastest is subjective. Do you mean fastest as in returns a value as fast as possible? Supports as much concurent load as possible? Least network usage? Most queries per second? And why are you worried about speed? Never [prematurely optimize](http://blog.ircmaxell.com/2011/08/on-optimization-in-php.html) – ircmaxell Sep 05 '11 at 14:59
  • I've benchmarked (probably poorly and or wrong) apc, memcached, ramdisk and shm some month ago. Maybe the scripts can help you out a little but it's not enough for an answer as i don't have any good numbers to share: http://dl.dropbox.com/u/3615626/code.zip – edorian Sep 05 '11 at 15:24
  • Haxe/mod_tora allows to store variable instances in-memory WITHOUT serializing. I'm looking for similar feature for PHP, however, looks like that complex data structures can't be stored without serializing step. Is that true and PHP does not have this feature ? – MT4000 Feb 02 '14 at 02:42

5 Answers5

14

If you dont have APC or Memcached installed already (or dont want to use them for this) you can also create a RAM disk. Then use file_get_contents() and file_put_contents() where filename is your key and the file content is your value. I dont have numbers for that, but it should be fast.

William Desportes
  • 1,412
  • 1
  • 22
  • 31
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Looks like a mega overhead. **How** could RAM disk be faster than direct APC and Shmop? – Pacerier Jan 19 '15 at 08:55
  • 5
    @Pacerier not sure why you think it cannot be faster or why it's a "mega" overhead. When in doubt, benchmark. – Gordon Jan 20 '15 at 09:16
6
  • chdb is a read-only hashtable shared across PHP processes: Probably the fastest and less memory-angry one.

  • Hidef allows to define constants using a .ini file. The constants are defined once, when the php module is started.

  • APC can store variables in shared memory, so that they are available to other PHP processes. It has the overhead of serializing and de-serializing variables each time you store and fetch them.

See others: http://pecl.php.net/packages.php?catpid=3&catname=Caching

NikiC
  • 100,734
  • 37
  • 191
  • 225
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • It's useless if you can't write to it............. – Pacerier Jan 19 '15 at 08:56
  • @Pacerier: what is that you can't write to? I just tried chdb and is pretty good. I'm caching parts of the website and it is awesome. – machineaddict May 25 '16 at 12:35
  • @Pacerier: now I see what you meant, you cannot change the stored data once you save it. that is not a problem if you have build your webapp in a way to use it like this. for example, i cache parts of the website there are doing heavy mysql queries and are used by a lot of users. – machineaddict May 30 '16 at 07:48
3

APC http://php.net/manual/en/book.apc.php

You can use it for optimizing and caching (intermediate) PHP-code

Read this: http://blog.digitalstruct.com/2008/02/27/php-performance-series-caching-techniques/

v3rt1g0
  • 43
  • 4
0

If your don't intend to modify data (probably true for your configuration files) then use chdb.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • It's also worth pointing out that chdb is NOT distributed, so it's single server only. If you need multiple servers to have access to the info, you're options are limited... – ircmaxell Sep 05 '11 at 15:26