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

Storing data in memory

I'm rewriting my old project to Phalcon, and in that project I have online counter which use 1 MySQL table to store all IP's for past 5 min. and on every request it updates timeout for current IP and delete all rows that already > 5 min. and after that it shows actual users online.

It's absolutely bad solution, because it can produce "SQL request flood" to DB table when every user updates the table at every request. I have one thought, things like this should be in memory. So I would like to ask does Phalcon can give me access to some storage in memory?

Or maybe somebody knows a superbest solution for this?


You can use apc_store/apc_fetch to store/fetch data to/from memory with minimum overhead:

https://php.net/manual/en/function.apc-fetch.php https://php.net/manual/en/function.apc-store.php

Thank you so much! For those who wants to do the same here is the code:

        $now = time();
        $online_count = 1;
        if (apc_exists('online')) {
            $online = apc_fetch('online');
            foreach ($online as $key => $value)
                if ($now > $online[$key])
            $online[$this->session->getId()] = $now + 300;
            $online_count = count($online);
        } else {
            $online = array($this->session->getId() => $now + 300);
        apc_store('online', $online);

        $this->view->online = $online_count;