We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

"read_and_close" option in Session Adapter Files

While troubleshooting slow loading times in a project that uses file-based session storage I realized that php was locking the session file for each process that needed to read the session. My front-end logic was firing off multiple ajax calls to my Phalcon back end, but my Phalcon back end was forcing the ajax calls to queue up and wait turns to read the session file.

A quick fix for this would be to apply the "read_and_close" option that php allows in the session_start() command. Given that session_start() is encapsulated within Phalcon's $session->start() command, I would expect this syntax to work:

$session = new \Phalcon\Session\Adapter\Files;

$session->setOptions([ "read_and_close" => true ]);

$session->start();

Unfortunately, I could not find any documentation to indicate whether "read_and_close" is implemented as a viable option within the \Phalcon\Session\Adapter\Files class. Has anyone successfully used this option to remove session file blocking while using the \Phalcon\Session\Adapter\Files class? If so, how did you go about making it work?

If the session files are being hit so much they're slowing the page down, then I'd move away from file based sessions.

The simplest solution would be to move to database-based sessions, as the locking should be less restrictive. This would only require a modification of the Session service - the rest of your code shouldn't need to be touched.

This may be a lateral move - but could some of the information stored in the session be stored in a cache like memcache instead?