0

I am new to PHP. What I know is that PHP runs a separate process for each user/session. However, I also want some global process which can update some global information (I will get such information through REST APIs of a website) and cache it in the memory, so the PHP code can just grab the global information from there. Moreover, this global information has to be updated like every minute or so. I could of course get such global information for each session, but it would be inefficient. I would rather have such global information updated separately in a separate process.

AlexScalar
  • 1,729
  • 3
  • 10
  • 17
  • 1
    Something like that is usually done using a database as a "global process" and maybe a cron based script as actor. That depends on where that information comes from. You would need to tell us more. – arkascha May 21 '18 at 08:30
  • Can a cron based script be written in PHP, and can the results be stored in memory, rather than a file? – AlexScalar May 21 '18 at 08:32
  • I'd use Redis to share global information: https://redis.io – Stefan May 21 '18 at 08:33
  • It can be written in any language you want, sure. And the result _is_ in memory, more or less. I think you should read a bit about databases and efficiency. You won't be more efficient with shared memory or similar. – arkascha May 21 '18 at 08:33
  • 2
    Since every process has its own memory, "in memory" probably won't help much. You want a separate in-memory cache like memcache then. Of course, you could also have a persistent PHP worker which you communicate with over sockets or such which stores stuff just *in memory*. What's the best approach depends on a lot of details… – deceze May 21 '18 at 08:33
  • @Stefan Redis _is_ a database. – arkascha May 21 '18 at 08:34
  • @arkascha sounds like he wants to hold and regularly update data in memory, which is to be shared among multiple php processes. Some could be crons, others controlled by a web server. Redis seems a fit. Unless I got something wrong. – Stefan May 21 '18 at 08:39
  • 1
    @Stefan Any database can do that. I did not mean that you were wrong. You just mentioned _one_ of many alternatives. – arkascha May 21 '18 at 08:40
  • 1
    Redis, memcached, temp files, SQL database, MongoDB, ElasticSearch, building a robot to write postits and stick them on the server case. All these are solutions to this problem. Pick the easiest one and go with it. Also, there's probably tons more ways to solve this – apokryfos May 21 '18 at 08:41
  • @apokryfos you are right. – Stefan May 21 '18 at 08:44
  • Do you even need a daemon/service - perhaps you could use a caching layer? (Not that caching is easy.) – Progrock May 21 '18 at 11:33

0 Answers0