Created
April 27, 2011 02:54
-
-
Save avdg/943638 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Phergie_Plugin_Cache_Backend_SqLite extends Phergie_Plugin_Cache_Backend | |
{ | |
protected $db; | |
protected $prepareIsset; | |
protected $prepareFetch; | |
protected $prepareStoreUpdate; | |
protected $prepareStoreInsert; | |
protected $prepareExpire; | |
protected $prepareGetExpiration; | |
public function __construct() | |
{ | |
if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) { | |
throw new Phergie_Exception('PDO and pdo_sqlite extensions must be installed'); | |
} | |
$defaultDir = dirname(__FILE__); | |
$defaultDbName = 'cache.db'; | |
$query = 'CREATE TABLE phergie_cache ( | |
id INT UNIQUE PRIMARY KEY, expiration TIME, key VARCHAR(120), value BLOB);'; | |
$this->db = new PDO('sqlite:' . $defaultDir . '/' . $defaultDbName); | |
$this->db->exec($query); | |
$query = 'SELECT count(id) FROM phergie_cache | |
WHERE key=:key AND expiration > :time | |
'; | |
$this->prepareIsset = $this->db->prepare($query); | |
$query = 'update phergie_simple_cache | |
set value=:value, expiration=:expire where key=:key | |
'; | |
$this->prepareStoreUpdate = $this->db->prepare($query); | |
$query = 'insert into phergie_simple_cache | |
(key, value, expiration) | |
VALUES (:key, :value, :expiration) | |
'; | |
$this->prepareStoreInsert = $this->db->prepare($query); | |
$query = 'SELECT value FROM phergie_cache | |
WHERE key=:key AND expiration > :time | |
'; | |
$this->prepareFetch = $this->db->prepare($query); | |
$query = 'DELETE FROM phergie_cache WHERE key=:key'; | |
$this->prepareExpire = $this->db->prepare($query); | |
$query = 'SELECT expiration FROM phergie_cache | |
WHERE key=:key AND expiration > :time | |
'; | |
$this->prepareGetExpiration = $this->db->prepare($query); | |
} | |
public function isset($key) | |
{ | |
$this->prepareIsset->execute(array( | |
':key' => $key, | |
':time' => time(), | |
)); | |
return $prepare->fetch() === 1; | |
} | |
public function store($key, $data, $ttl) | |
{ | |
$values = array( | |
':key' => $key, | |
':value' => $value, | |
':expiration' => $expiration, | |
); | |
if ($this->issetBackend($key)) { | |
$this->prepareStoreUpdate->execute($values); | |
} else { | |
$this->prepareStoreInsert->execute($values); | |
} | |
return true; | |
} | |
public function fetch($key) | |
{ | |
$this->prepareFetch->execute(array( | |
':key' => $key, | |
':time' => time(), | |
)); | |
if ($prepare->columnCount() === 1) { | |
$results = $prepare->fetch(); | |
return $results[0]; | |
} | |
return false; | |
} | |
public function expire($key) | |
{ | |
$this->prepareExpire->execute(array( | |
':key' => $key, | |
)); | |
} | |
public function getExpiration($key) | |
{ | |
$this->prepareGetExpiration->execute(array( | |
':key' => $key, | |
':time' => time(), | |
); | |
if ($prepare->columnCount() === 1) { | |
$results = $prepare->fetch(); | |
return $results[0]; | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Phergie_Plugin_Cache_Backend_SqLite extends Phergie_Plugin_Cache_Backend | |
{ | |
$db; | |
public function __construct() | |
{ | |
if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) { | |
throw new Phergie_Exception('PDO and pdo_sqlite extensions must be installed'); | |
} | |
$defaultDir = dirname(__FILE__); | |
$defaultDbName = 'cache.db'; | |
$query = ' | |
CREATE TABLE phergie_cache ( | |
id INT UNIQUE PRIMARY KEY, | |
expiration TIME, | |
key VARCHAR(120), | |
value BLOB | |
);'; | |
$this->db = new PDO('sqlite:' . $defaultDir . '/' . $defaultDbName); | |
$this->db->exec($query); | |
} | |
public function isset($key) | |
{ | |
$query = 'SELECT count(id) FROM phergie_cache | |
WHERE key=:key AND expiration > :time | |
'; | |
$prepare = $this->db->prepare($query); | |
$prepare->execute(array( | |
':key' => $key, | |
':time' => time(), | |
)); | |
return $prepare->fetch() === 1; | |
} | |
public function store($key, $data, $ttl) | |
{ | |
$queryUpdate = 'update phergie_simple_cache | |
set value=:value, expiration=:expire where key=:key | |
'; | |
$queryInsert = 'insert into phergie_simple_cache | |
(key, value, expiration) | |
VALUES (:key, :value, :expiration) | |
'; | |
$values = array( | |
':key' => $key, | |
':value' => $value, | |
':expiration' => $expiration, | |
); | |
if ($this->issetBackend($key)) { | |
if (!$overwrite) { | |
return false; | |
} | |
$prepare = $this->db->prepare($queryUpdate); | |
} else { | |
$prepare = $this->db->prepare($queryInsert); | |
} | |
$prepare->execute($values); | |
return true; | |
} | |
public function fetch($key) | |
{ | |
} | |
public function expire($key) | |
{ | |
} | |
public function getExpiration($key) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment