I want to implement a cache system in PHP consisting of following features

1) It should be able to store any(large or small) amount of data as long as the memory serves.(key value pair i.e. get & set)

2) It should have TTL

3) It should have LRU

4) Complexity should be O(1)

Constraints: 1) I am not allowed to use any middleware i.e. memcache or redis or any other in memory DB - so essentially implement memcache

Now problem here is

  1. How can I interact with RAM using php?
  2. PHP variables can be stored in memory but as soon as thread ends all variables are garbage collected. So you loose data?
  3. May be use in memory mysql db engine? Can I?
  4. Is it even possible to build such system using PHP?

closed as too broad by GlenH7, James McLeod, Arseni Mourzenko, user40980, Kilian Foth Mar 23 '15 at 14:44

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • recommended reading: Why do interview questions make poor Programmers.SE questions? – gnat Mar 19 '15 at 14:18
  • @gnat ok. So do you have any suggestions on the problem in hand? – deepak Mar 19 '15 at 14:28
  • 1
    did you check meta guidance referred in prior comment? – gnat Mar 19 '15 at 14:32
  • @gnat Yes I did. And that is why I have laid down the problem to the point. Had it been a code related problem I would have posted it on stackoverflow instead. But because I wanted to hear out the suggestions I have come to programmers exchange. As far as scope of the problem is concerned it is very crystal clear to me and I have tried my best to provide those in my question. So it is neither an open ended question nor opinion based, answers ll be fairly 2 liners for each points. If any one needs any other info I am happy to provide. – deepak Mar 19 '15 at 14:47
  • 1
    I think this is a duplicate of this SO question: stackoverflow.com/questions/609150/… – dcorking Mar 19 '15 at 16:43

It's not a PHP specific problem. Whenever you want to share memory with multiple processes (PHP scripts terminate after they finish), use Shared Memory. In PHP, shm_attach() should get you started.

Implementing cache on top of shm should be straight forward.

  • Yeah, this is better then tmpfs. I didn't know php had shm bindings. – monocell Mar 19 '15 at 17:01

The easiest way I can think of to get access to a chunk of memory from php would be via tmpfs.

Then you write a hash table implementation (+your cache logic) in terms of operations on file handles. This might be fun as an exercise but if you do this on someone else's dime you are not doing your job properly imo.

Not the answer you're looking for? Browse other questions tagged or ask your own question.