Hello, Im beginner in PHP dev. I want to reduce a DB select request on my application, by implementing a temp variable for every user, every x min that variable must be updated from the DB using a function getonline, note that every user have its own unique usercode and query result, so every user run the same function with same usercode during the next x min will get the result from the temp variable without accessing the DB. something like: If ($temp[$user_code] == null) { $temp[$user_code] = $this->getOnlineNow($user_code); return $temp[$user_code]; } but that of course will never work. please dont respond with "go learn how memcached work" or "use read this", please help I want a simple solution Im a beginner ! thanks
$_SESSION? for example $_SESSION['online'] = array('time' => timestamp, 'users' => 121); check if time > time + ?? secs and then refresh or don't do anything? But if you want multiple users to make use of this? create a cache function that writes the value to a file if the file doesn't exists or when the time has expired. If you like i can add a snippet from my very easy (and understandable) class.
$_SESSION['online'] = array('time' => timestamp, 'users' => 121); check if time > time + ?? secs and then refresh or don't do anything? But if you want multiple users to make use of this? create a cache function that writes the value to a file if the file doesn't exists or when the time has expired. If you like i can add a snippet from my very easy (and understandable) class.[/quote][/quote] Hi Eric, thanks for reply, and thats my first idea and its not going to work with my case, because of multiple users with different IDs using the same function, waiting for your class Also I just installed memcached as last resort, and it seems good and easy after I watch this easy tutorial : Any one can help and write the code for multiple users ? $Im too tired now to try it, going to sleep
Can you tell me what the onlineUsers function returns? Link to my class: http://www.bruggema.nl/phps/class.cache.phps
Got it to work using memcached something like : $test = $memcache->get($code); if ($test == null) { $tempp = $this->getOnlineNow($code); $memcache->set($user_code, $tempp, false, 60); //###time in second } $onlinenow = $memcache->get($code); return $onlinenow; Code (markup): Wish that help future reader
Good start, but there is no need to make a cache call if you already have the data: $results = $memcache->get($code); if ($results == null) { $results = $this->getOnlineNow($code); $memcache->set($code, $results, false, 60); //###time in second } return $results; Code (markup):