Domanda

Voglio creare un'applicazione Web con un modello che persiste tra richieste HTTP.Da ciò che capisco le lingue come PHP trattare ogni richiesta HTTP come una nuova connessione ad eccezione di alcune variabili globali come la sessione;Quindi ogni volta che l'utente cambia pagine Tutte le mie classi PHP vengono caricate di nuovo in memoria (e ogni richiesta AJAX lo fa anche questo) - che richiede di costruire dal database ogni volta.

Sono sbagliato o sto cercando di fare un cerchio in forma in una piazza?Memcached sembra essere una buona soluzione per mantenere il mio modello in memoria tra le richieste di pagina, ma deve ancora caricare la cache.PHP CLI sembrava promettenti ma dopo averlo esaminato più che sembrava che sarebbe più difficile di quanto ne valesse la pena.Qualche suggerimento?

È stato utile?

Soluzione

You should avoid requiring a persistent state in your web application; HTTP is stateless and you need to design your business logic around that. Also, PHP isn't very memory-leak safe since it isn't meant to act as a daemon or run for extended periods. You shouldn't be maintaining a database of information in PHP variables either. You can cache expensive query results in memcache and retrieve them with very little latency.

You can serialize your model, store it in a session (or memcache), and deserialize it on the next request in order to keep relevant variables within scope between requests. If you share a bit more about the specifics of your application, perhaps we can help you figure out the best way to handle this.

Altri suggerimenti

I do agree that one should try to avoid sharing states between requests. But in rare cases, such as to implement a simple message queue over HTTP, It's desirable to have this feature in hand.

For php, one could use IPC via the php_shmop extension.

For nodejs, I found this

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top