forked from RSS-Bridge/rss-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SQLiteCache] Implement cache based on SQLite 3 (RSS-Bridge#1035)
- Loading branch information
1 parent
0f985f0
commit ac0fe26
Showing
2 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* Cache based on SQLite 3 <https://www.sqlite.org> | ||
*/ | ||
class SQLiteCache implements CacheInterface { | ||
protected $path; | ||
protected $param; | ||
|
||
private $db = null; | ||
|
||
public function __construct() { | ||
$file = PATH_CACHE . 'cache.sqlite'; | ||
|
||
if (!is_file($file)) { | ||
$this->db = new SQLite3($file); | ||
$this->db->enableExceptions(true); | ||
$this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)"); | ||
} else { | ||
$this->db = new SQLite3($file); | ||
$this->db->enableExceptions(true); | ||
} | ||
$this->db->busyTimeout(5000); | ||
} | ||
|
||
public function loadData(){ | ||
$Qselect = $this->db->prepare('SELECT value FROM storage WHERE key = :key'); | ||
$Qselect->bindValue(':key', $this->getCacheKey()); | ||
$result = $Qselect->execute(); | ||
if ($result instanceof SQLite3Result) { | ||
$data = $result->fetchArray(SQLITE3_ASSOC); | ||
if (isset($data['value'])) { | ||
return unserialize($data['value']); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function saveData($datas){ | ||
$Qupdate = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)'); | ||
$Qupdate->bindValue(':key', $this->getCacheKey()); | ||
$Qupdate->bindValue(':value', serialize($datas)); | ||
$Qupdate->bindValue(':updated', time()); | ||
$Qupdate->execute(); | ||
|
||
return $this; | ||
} | ||
|
||
public function getTime(){ | ||
$Qselect = $this->db->prepare('SELECT updated FROM storage WHERE key = :key'); | ||
$Qselect->bindValue(':key', $this->getCacheKey()); | ||
$result = $Qselect->execute(); | ||
if ($result instanceof SQLite3Result) { | ||
$data = $result->fetchArray(SQLITE3_ASSOC); | ||
if (isset($data['updated'])) { | ||
return $data['updated']; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function purgeCache($duration){ | ||
$Qdelete = $this->db->prepare('DELETE FROM storage WHERE updated < :expired'); | ||
$Qdelete->bindValue(':expired', time() - $duration); | ||
$Qdelete->execute(); | ||
} | ||
|
||
/** | ||
* Set cache path | ||
* @return self | ||
*/ | ||
public function setPath($path){ | ||
$this->path = $path; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set HTTP GET parameters | ||
* @return self | ||
*/ | ||
public function setParameters(array $param){ | ||
$this->param = array_map('strtolower', $param); | ||
return $this; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
protected function getCacheKey(){ | ||
if(is_null($this->param)) { | ||
throw new \Exception('Call "setParameters" first!'); | ||
} | ||
|
||
return hash('sha1', $this->path . http_build_query($this->param), true); | ||
} | ||
} |
This file contains 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