Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Heres a solution ive just put together, instead of using a file based cache use a database, PDO sqlite (This way its easy just to delete the cache file database to clear all cache).</p> <p>Look towards the bottom you can see how it works, it will delete the row after 50 hits and redirect so it can generate a new copy. Hope it helps</p> <p><strong>sqlite.cache.class.php</strong></p> <pre><code>&lt;?php /** * PDO sqlite cache class * You can include('sqlite.cache.class.php'); this class */ class sqlite_cache{ private $db; function __construct($dsn){ $this-&gt;dsn = $dsn; $this-&gt;chkSetup(); } /*Singleton Connect*/ private function connect(){ if (!$this-&gt;db instanceof PDO){ $this-&gt;db = new PDO($this-&gt;dsn); $this-&gt;db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } } /*Raw Select*/ public function rawSelect($sql){ $this-&gt;connect(); return $this-&gt;db-&gt;query($sql)-&gt;fetchAll(PDO::FETCH_ASSOC); } public function get($fieldname=null, $id=null){ $this-&gt;connect(); $sql = "SELECT * FROM cache WHERE $fieldname = :id"; $statement = $this-&gt;db-&gt;prepare($sql); $statement-&gt;bindParam(':id', $id, PDO::PARAM_STR); $statement-&gt;execute(); return $statement-&gt;fetchAll(PDO::FETCH_ASSOC); } /*Insert*/ public function put($values){ $this-&gt;connect(); $fieldnames = array_keys($values[0]); $sql = "INSERT INTO cache "; $fields = '('.implode(' ,', $fieldnames).')'; $bound = '(:'.implode(', :', $fieldnames).')'; $sql .= $fields.' VALUES '.$bound; $statement = $this-&gt;db-&gt;prepare($sql); foreach($values as $vals){ $statement-&gt;execute($vals); } } /*Update*/ public function update($fieldname, $value, $pk, $id){ $this-&gt;connect(); $sql = "UPDATE cache SET $fieldname = :value WHERE $pk = :id"; $statement = $this-&gt;db-&gt;prepare($sql); $statement-&gt;bindParam(':id', $id, PDO::PARAM_STR); $statement-&gt;bindParam(':value', $value, PDO::PARAM_STR); $statement-&gt;execute(); } /*Update Hits*/ public function add_hit($id){ $this-&gt;connect(); $sql = "UPDATE cache SET hits = hits + 1 WHERE url = :id"; $statement = $this-&gt;db-&gt;prepare($sql); $statement-&gt;bindParam(':id', $id, PDO::PARAM_STR); $statement-&gt;execute(); } /*Delete*/ public function delete($id){ $this-&gt;connect(); $sql = "DELETE FROM cache WHERE url = :id"; $statement = $this-&gt;db-&gt;prepare($sql); $statement-&gt;bindParam(':id', $id, PDO::PARAM_STR); $statement-&gt;execute(); } /*Database Setup*/ private function chkSetup(){ $dso = explode(':',$this-&gt;dsn); if(file_exists($dso[1])){ return; }else{ $this-&gt;connect(); //Create Table $sql ="CREATE TABLE cache (id INTEGER PRIMARY KEY, title TEXT, url TEXT, hits INTEGER, date INTEGER, contents TEXT)"; $this-&gt;db-&gt;query($sql); header("refresh:0;url=./"); die; } } } ?&gt; </code></pre> <p><strong>index.php</strong></p> <pre><code>&lt;?php include('sqlite.cache.class.php'); $cache = new sqlite_cache('sqlite:./cache.db'); //Check if cache exists $cache_result = $cache-&gt;get('url',$_SERVER['REQUEST_URI']); //Exists if(!empty($cache_result)){ //Add Hit $cache-&gt;add_hit($_SERVER['REQUEST_URI']); //Delete If over 50 hits if($cache_result[0]['hits']&gt;=50){ $cache-&gt;delete($_SERVER['REQUEST_URI']); header('Location: '.$_SERVER['REQUEST_URI']); die; } echo $cache_result[0]['contents']; }else{ //Generate your page contents ect ob_start(); /////////////////////////////// //Your script code goes here /////////////////////////////// echo 'Your content'; //End your script code///////// /////////////////////////////// $return = ob_get_contents(); ob_end_clean(); //Before output build values to put in cache $cache_contents = array(array('id'=&gt;NULL, 'title'=&gt;'Page Title', 'url'=&gt;$_SERVER['REQUEST_URI'], 'hits'=&gt;'0', 'date'=&gt;time(), 'contents'=&gt;$return)); //Store cache $cache-&gt;put($cache_contents); echo $return; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload