Skip to content

Instantly share code, notes, and snippets.

@iegik
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save iegik/771e7c28cf93a6acd00a to your computer and use it in GitHub Desktop.

Select an option

Save iegik/771e7c28cf93a6acd00a to your computer and use it in GitHub Desktop.
PHP caching samples
<?php
// Return 304 when files are not changed
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime(__FILE__))
{
header('HTTP/1.0 304 Not Modified');
exit;
}
<?php
require_once('config.php');
require_once('model_tablename.php');
header('Content-Type: application/json');
header('Cache-Control: no-cache');
if(@$_GET['requests']){
updateTablenameRequests($_GET['id'],$_GET['requests']);
}
if(@$_GET['id']){
echo getTablenameRow($_GET['id']);
}else{
echo getTablenameRows();
}
<?php // Configuration
/* PDO
$provider = 'PDO';
$dbhost = dirname(__FILE__);
$dsn = "sqlite:".$dbhost;
/* Memory
$provider = 'SQLite3';
$dsn = "memory:";
*/
$provider = 'SQLite3';
$dbhost = '';
$dbname = "SQLite3.db";
$dsn = $dbname;
// Test the connection
try {
$db = new $provider($dsn);
} catch (Exception $e) {
header('HTTP/1.0 500 Internal Server Error');
echo $e->getMessage();
}
<?php
require_once('config.php');
require_once('model_tablename.php');
header('Content-Type: application/json');
header('Cache-Control: no-cache');
generateTablenameRows(@$_GET['from'],@$_GET['rows']);
echo getTablenameRows();
<?php
require_once('config.php');
require_once('model_tablename.php');
header('Content-Type: application/json');
header('Cache-Control: max-age=0');
if(@$_GET['requests']){
updateTablenameRequests($_GET['id'],$_GET['requests']);
}
if(@$_GET['id']){
echo getTablenameRow($_GET['id']);
}else{
echo getTablenameRows();
}
<?php
require_once('config.php');
require_once('model_tablename.php');
header('Content-Type: application/json');
header('Cache-Control: max-age=0, must-revalidate');
if(@$_GET['requests']){
updateTablenameRequests($_GET['id'],$_GET['requests']);
}
if(@$_GET['id']){
echo getTablenameRow($_GET['id']);
}else{
echo getTablenameRows();
}
<?php
// Install
@$db->query('CREATE TABLE IF NOT EXISTS tablename (id int, requests int, sometext TEXT, PRIMARY KEY (id))');
function json_cache($query, $data){
$new = json_encode($data);
$cached = join(DIRECTORY_SEPARATOR,array(sys_get_temp_dir(),md5($query)));
// Save query result as file
if(md5_file($cached) != md5($new))
file_put_contents($cached,$new);
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', filemtime($cached)));
// Check if the cached file ir modified
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($cached) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime(__FILE__))
{
// Not Modified:Return the 304
header('HTTP/1.0 304 Not Modified');
header('X-cached: '.$cached);
// Or ... open cached file
// return = file_get_contents($cached);
exit;
}
// Modified, return the result
return $new;
}
function generateTablenameRows($from, $rows) {
if(!@$to)$to = 100;
for($i=$from+1;$i<=$from+$to;$i++){
$id = $i;
createTablenameRow($i,$i, substr(str_shuffle(MD5(microtime())), 0, 10));
}
header('X-generated: '.$i);
}
function createTablenameRow($id,$requests,$sometext) {
global $db;
$provider = get_class($db);
$query = 'INSERT INTO tablename (id, requests, sometext) VALUES(:id, :requests, :sometext)';
$stmt = $db->prepare($query);
$stmt->bindValue(':id',$id,constant(strtoupper($provider).'_INTEGER'));
$stmt->bindValue(':requests',$requests,constant(strtoupper($provider).'_INTEGER'));
$stmt->bindValue(':sometext',$sometext,constant(strtoupper($provider).'_TEXT'));
if($res = $stmt->execute()){
$data = $res->fetchArray();
return json_cache($query, $data);
}
}
function getTablenameRow($id) {
global $db;
$provider = get_class($db);
$query = 'SELECT * FROM tablename WHERE id = :id';
$stmt = $db->prepare($query);
$stmt->bindValue(':id',$id,constant(strtoupper($provider).'_INTEGER'));
$res = $stmt->execute();
// res, query
$data = array();
$i = 0;
while($row = $res->fetchArray(constant(strtoupper($provider).'_ASSOC'))){
if(!isset($row['id'])) continue;
$data[$i]['id'] = $row['id'];
$data[$i]['requests'] = $row['requests'];
$i++;
}
header('X-resulted-rows: '.$i);
return json_cache($query, $data);
}
function updateTablenameRequests($id,$requests) {
global $db;
$provider = get_class($db);
$query = 'UPDATE tablename SET requests=:requests WHERE id = :id';
$stmt = $db->prepare($query);
$stmt->bindValue(':id',$id,constant(strtoupper($provider).'_INTEGER'));
$stmt->bindValue(':requests',$requests,constant(strtoupper($provider).'_INTEGER'));
$res = $stmt->execute();
$data = $res->fetchArray();
return json_cache($query, $data);
}
function deleteTablenameRow($id) {
global $db;
$provider = get_class($db);
$query = 'DELETE FROM tablename WHERE id = :id';
$stmt = $db->prepare($query);
$stmt->bindValue(':id',$id,constant(strtoupper($provider).'_INTEGER'));
$res = $stmt->execute();
$data = $res->fetchArray();
return json_cache($query, $data);
}
function getTablenameRows() {
global $db;
$provider = get_class($db);
$query = 'SELECT * FROM tablename';
$stmt = $db->prepare($query);
$res = $stmt->execute();
// res, query
$data = array();
$i = 0;
while($row = $res->fetchArray(constant(strtoupper($provider).'_ASSOC'))){
if(!isset($row['id'])) continue;
$data[$i]['id'] = $row['id'];
$data[$i]['requests'] = $row['requests'];
$i++;
}
header('X-resulted-rows: '.$i);
return json_cache($query, $data);
}
<?php
require_once('config.php');
require_once('model_tablename.php');
header('Content-Type: application/json');
header('Cache-Control: no-cache');
if(@$_GET['requests']){
updateTablenameRequests($_GET['id'],$_GET['requests']);
}
if(@$_GET['id']){
echo getTablenameRow($_GET['id']);
}else{
echo getTablenameRows();
}
<?php
require_once('config.php');
require_once('model_tablename.php');
header('Content-Type: application/json');
header('Cache-Control: no-store');
if(@$_GET['requests']){
updateTablenameRequests($_GET['id'],$_GET['requests']);
}
if(@$_GET['id']){
echo getTablenameRow($_GET['id']);
}else{
echo getTablenameRows();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment