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?



83.4k

You can use apcstore/apcfetch to store/fetch data to/from memory with minimum overhead:

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

Thank you so much! For those who wants to do the same here is the code: ```php $now = time(); $onlinecount = 1; if (apcexists('online')) { $online = apcfetch('online'); foreach ($online as $key => $value) if ($now > $online[$key]) unset($online[$key]); $online[$this->session->getId()] = $now + 300; $onlinecount = count($online); } else { $online = array($this->session->getId() => $now + 300); } apc_store('online', $online);

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

```