If I store contents of a text file in a session variable in php, will it stay in memory for the period of that session?
I have a small project where I need to perform searches in that specific text file which is just over 1mb. I'm thinking of using a session variable if it stays in memory, so that I don't have to read this file again and again.
No, sessions will be stored default in files (e.g. in /tmp). Of course, you can use the memory with for example memcached. It's also possible to use a database for sessions.
But, if you have enough memory, store your sessions in memory (very fast). Memcached is a great distributed memory object caching system. See http://memcached.org for more information.
And here about the memcached extension for php: http://php.net/manual/en/book.memcached.php
Yes, It will stay in memory for the life of the session. Your 1mb is far from the max capacity it can hold (default I think is 128MB). However, test both ways if it would be more efficient to just read from the file.
It will be stored using a file in the server. You can see session files in the path defined by session_save_path. Depending on the file system, the file could stay in memory depending on usage, but basically is a file.
I forgot to mention that the info was obtained mainly from here: Where and How is data stored in a session?
session is stored either in file or in database, it does not consume PHP memory(server memory).
来源:https://stackoverflow.com/questions/32027037/does-a-session-variable-stay-in-memory-in-php