Question

PHP uses cookies, sessions or databases (and ORMs) in order to remember data (so they are not lost after single HTTP request). However, in Java (I mean servlets etc.) there is another solution: in brief you may choose for an object different scopes (how long it exists). Besides of session-scope or simple single HTTP-request "life" (scope), it can "live" during whole HTTP-server runtime and can be initialized at the startup of the HTTP-server.

Data can be therefore shared between different users / sessions, and no database requests are required (causing decrease of efficiency of the whole web-application). (I mean they're not required when HTTP-Server is already running - the object and its state is "remembered").

(And I do as much as I can to decrease SQL requests, using even PHP arrays for frequently read, but actually never modified DB data).

What I need in PHP is a way to:

  1. Remember (store somewhere) data that can be changed and shared between many users, but not into DB
  2. Without using sessions (nor cookies) I want to have multiple data-informations for many requests (etc. AJAX no single, but many requests to the same URL), which of course must be stored somewhere else for some time. For instance, I want to read all data (rows) with a single SQL request, remember them for a short period in PHP, and only then, one by one row, send responses with, say, each row in seperate response into appropriate AJAX function

Anyone can give me some hints how can I achieve this in PHP, preferably easiest possible way?

Was it helpful?

Solution

As a preface to this answer (which I'm sure you've already grasped), PHP's execution model essentially 'restarts' the process between requests and as such storage of anything cross-request in PHP alone is unachievable.

That leaves you with a few options, and they're all really 'strengths' of database:

  1. Use a simple key-value in-memory persistance layer, like memcached or Redis
  2. Use a noSQL solution with a bit more structure (and consistency should this be required) but that's still working in-memory and is comparably quicker than an RDB
  3. Use an RDBMS because it'll work great, and the quantity if traffic you'll need to topple a well designed schema on moderate hardware is probably much higher than you think

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top