Does a session variable stay in memory in php

浪尽此生 提交于 2019-12-10 11:18:19

问题


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.


回答1:


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




回答2:


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.




回答3:


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?




回答4:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!