Web Developer On1x.com Игры как увлечение Видео–игры как хобби и увлечение. Новости игровой индустрии. Блог про видео–игры.
Характеристики персонажа в игре Dark SoulsPAX East 2013, отчетО повторении одних и тех же квестов в играх

PHP cache on mysql. SQL table like memcached. Class

Hello! Every time i working with big project i use in my code memcached. It's like file cache, but taking place on server memory. It's fast... very fast. One what you need - save output html code and put it on memcached. Next time you may just look it like attribute on memcached. Each link(like "name" eq "value") in memcache have expire time. For example - create "comment_168" with value "JohnX, score: 9.6 comments: 19...bla bla bla" and set expire time in 6 minutes. Each client, who trying real comment line - will not make on server additionals queries, like count of comments and calculation score of this comment. Server just take it from memcached. When timer goes over 6 min - this variable and it's value just destroying. So...

So memcached used on Dedicated servers on linux(dunno, working highly loaded server on win or not) for fast access to variables from memory. It's faster, that file cache. Very important thing in this system - expiry time of variable.

I searching in web some class for system like this on mysql, but found nothing. Anyway, it's idea - with expire timer, get me a reason to coding php class by myself.


How it's work:
Try get $buf from $mycache. Then take a look, exist $buf or not. If exist, just output it. If not - generate content and put it in $mycache. Expire timer i make with summ current unixtime with $expire in seconds. When we select row from MySQL, we use condition - that expire attribute more that current unixtime.

Class code(you must rewrite lines with $db class for yourself):
cache.php, use it, like memcached
<?php
class mycache {
  function 
get($name){
    global 
$db,$now;
    
$c_q=$db->sql("SELECT `value` FROM `cache` WHERE `name`='".$name."' AND `expire`>'".$now."'");
    
$c_m=$db->row($c_q);
    if(
$c_m['value']){
      return 
$c_m['value'];
    }
    else{
      return 
false;
    }
  }
  function 
set($name,$value,$expire=1800){
    global 
$db,$now;
    
$db->sql("DELETE FROM `cache` WHERE `name`='".$name."'");
    
$db->sql("INSERT INTO `cache` (`name`,`value`,`expire`) VALUES ('".mysql_real_escape_string($name)."','".mysql_real_escape_string($value)."','".($now+$expire)."')");
  }
  function 
stats(){
    global 
$db;
    
$c_q=$db->sql("SHOW TABLE STATUS FROM `".$db->basename."` WHERE `Name`='cache'");
    
$c_m=$db->row($c_q);
    return 
"mycache: table ".$c_m[0].", rows ".$c_m[4].", size ".round($c_m[6]/1024,2)."Kb;";
  }
}

Example code:
<?php
require("class/cache.php");
$mycache=new mycache;
$buf=$mycache->get("index");
if(
$buf){
    print 
$buf;
}
else{
    
$result=date('d.m.Y H:i:s').'<table width="100%">';
    for(
$i=1;$i<100;$i=$i+3){
        
$result.='<tr><td style="background:'.(($i%2==0)?'#fff':'#eee').';">'.$i.'</td></tr>';<br>     }
    
$result.='</table>';
    
$mycache->set("index",$result,30);
    print 
$result;
}

Yeah, i so crazy :D
and forgot mysql table:
CREATE TABLE IF NOT EXISTS `cache` (
`name` varchar(200) NOT NULL,
`value` blob NOT NULL,
`expire` int(11) NOT NULL,
PRIMARY KEY (`name`),
KEY `expire` (`expire`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Maybe would be better use Memory engine, not MyISAM =)
Дата записи: 25.01.2011 16:51
On1x blog
my ip