Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Approach For Cache System: Step 1: Create A Table To Save Last Bug Fix Update Time

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Approach for Cache system

Step 1: Create a Table to save Last Bug fix update time:

-- phpMyAdmin SQL Dump


-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 07, 2010 at 06:39 AM
-- Server version: 5.1.36
-- PHP Version: 5.3.0

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;


/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `0210_01`
--

-- --------------------------------------------------------

--
-- Table structure for table `app_bug_fix`
--

CREATE TABLE IF NOT EXISTS `app_bug_fix` (


`id` bigint(20) NOT NULL AUTO_INCREMENT,
`last_fix_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `app_bug_fix`
--

Step 2. Create a stored procedure for Select for new table:


-- Name: sp_selectAppBugFixes

DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_selectAppBugFixes`$$
CREATE PROCEDURE `sp_selectAppBugFixes`(
_id BIGINT(20)
)
BEGIN
select
ads.id as id,
ads.last_fix_time as last_fix_time
from
app_bug_fix as ads
where
ads.id = _id
;
END$$
DELIMITER ;

-- End sp_selectAppBugFixes--------------

Step 3. Create a Business class for new table:

class AppBugFixes {

public $id;
public $last_fix_time;
private $db;

public function load(){


if(!Utility::isNumeric($this->id)) return false;
$this->db = DB::getInstance('Database');
$parameters = array(
"id"=>$this->id
);
$r = $this->db->callProcedure('sp_selectAppBugFixes',$parameters);
$row = $this->db->rownext($r);
$data = array(
"id" => $row['id'],
"last_fix_time" => $row['last_fix_time']
);
return $data;
}
}

This class will basically contain only the load function as Save or Update function will take place when some change is pushed to production. That can be thinked of later.

Step 4. Make a folder bug_fixes in app/tab and make 2 subfolders in the same: 1 for config.ini and other for install.sql
according to this new class:

[development]
class = AppBugFixes

Step 5. Make a new folder cache in project root and add a new file Cache.php. This will be the base class for caching
implementation:

class Cache{
// location and prefix for cache files
//const CACHE_PATH = "/wamp/www/time/cache/cache_";
//define('CACHE_PATH','/wamp/www/time/cache/cache_');

// how long to keep the cache files (hours)


//const CACHE_TIME;
//define('CACHE_TIME', 6);

var $_cache_path;
var $_cache_time;
var $_fanpageid;

function __construct($cache_path, $cahce_time = 1,$fanpageid = 0){


$this->_cache_path = $cache_path;
$this->_cache_time = $cahce_time;
$this->_fanpageid = $fanpageid;
}

// return location and name for cache file


function cache_file(){
return $this->_cache_path . md5($this->_fanpageid);
}

// display cached file if present and not expired


function cache_display(){

$file = $this->cache_file();
// check that cache file exists and is not too old
if(!file_exists($file)) return;
if(filemtime($file) < time() - $this->_cache_time * 3600) return;
// if so, display cache file and stop processing
readfile($file);
exit;
}

// write to cache file


function cache_page($content){
$content=preg_replace('/(\r\n|\r|\n)/s',"\n",$content);
if(false !== ($f = @fopen($this->cache_file(), 'w'))){
fwrite($f, $content); fclose($f);
}
return $content;
}

// load data from cache file


function cache_load(){
$handle = fopen($this->_cache_path . md5($this->_fanpageid), "rb");
$contents = stream_get_contents($handle);
fclose($handle);
$temp = explode ('<br/>',$contents);
foreach ($temp as $pair){
list($key,$value) = explode('=',$pair);
$pairs[$key] = $value;
}
return $pairs;
}

// execution stops here if valid cache file found


}
// enable output buffering and create cache file

Step 6. Make following changes in Business class of the app:


Changes to load function:
public function load(){
if(!Utility::isNumeric($this->pages_id)) return false;
$this->db = DB::getInstance('Database');
$parameters = array(
"pages_id"=>$this->pages_id
);
$r = $this->db->callProcedure('sp_selectAppDealShares',$parameters);
$row = $this->db->rownext($r);
$data = array(
"pages_id" => $row['pages_id'],
"require_fan" => $row['require_fan'],
"list_id" => $row['list_id'],
"contest_rules_href" => $row['contest_rules_href'],
"min_num_registrants" => $row['min_num_registrants'],
"max_num_registrants" => $row['max_num_registrants'],
"current_num_registrants" => $row['current_num_registrants'],
"deal_end_date" => $row['deal_end_date'],
"share_title" => $row['share_title'],
"share_text" => $row['share_text'],
"authorized" => $row['authorized'],
"display_footer" => $row['display_footer'],
"last_update_time" => $row['last_update_time'],
);
return $data;
}
Changes to save function:
public function save(){
$this->db = DB::getInstance('Database');
$parameters = array(
"pages_id"=>$this->pages_id,
"require_fan"=>$this->require_fan,
"list_id"=>$this->list_id,
"contest_rules_href"=>$this->contest_rules_href,
"min_num_registrants"=>$this->min_num_registrants,
"max_num_registrants"=>$this->max_num_registrants,
"current_num_registrants"=>$this->current_num_registrants,
"deal_end_date"=>$this->deal_end_date,
"share_title"=>$this->share_title,
"share_image"=>$this->share_image,
"share_text"=>$this->share_text,
"last_update_time"=>time()
);
$obj = new Cache('/wamp/www/0210-01/docroot/app/tab/deal_share/cache/',6,$this->pages_id);
$QueryString = '';
$first = true;
foreach ($parameters as $Key => $Value){
if(!$first){
$QueryString .= "<br/>".$Key.'='.$Value ;
}else{
$QueryString .= $Key.'='.$Value ;
$first = false;
}
}
$obj->cache_page($QueryString);
//$r = $this->db->callProcedure('sp_saveAppDealShares',$parameters);
//$row = $this->db->rownext($r);
//return $row['last_update_time'];
return true;

}
Step 7. Add support for Bug Fix class in FaceBookApp.

Step 8. Make following changes in view page of app:

$fb_app = FacebookApp::loadFacebookApp(FacebookApp::APP_DEAL_SHARE, true);


$fb_app_bugfix = FacebookApp::loadFacebookApp(FacebookApp::APP_BUG_FIX, true);

$page_id = "119460368064206"; //change later


$id = 1; //change later

$deal_share = $fb_app->getBusinessInstance();
$bug_fix = $fb_app_bugfix->getBusinessInstance();

$deal_share->pages_id = $page_id;
$bug_fix->id = $id;
$parameters = $deal_share->load();
$parameters_bugfix = $bug_fix->load();

$obj = new Cache('/wamp/www/0210-01/docroot/app/tab/deal_share/cache/',6,$page_id);//change later

$file = $obj->cache_file();
$array = $obj->cache_load();

//converting array to string

$QueryString = '';
$first = true;
foreach ($parameters as $Key => $Value){
if(!$first){
$QueryString .= "<br/>".$Key.'='.$Value ;
}else{
$QueryString .= $Key.'='.$Value ;
$first = false;
}
}

if(!file_exists($file)){ // check that cache file exists

$obj->cache_page($QueryString);

}elseif($parameters['last_update_time'] < $parameters_bugfix['last_fix_time']){ // check that cache file is older then last bug fix update

unlink($file);
$file = $obj->cache_file();
$obj->cache_page($QueryString);
$array = $obj->cache_load();

}else{

print_r($array);

You might also like