-
-
Save alekssamos/7cfafb993e3d195375cad8020e047701 to your computer and use it in GitHub Desktop.
a simple key/val store using php & sqlite3
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
<?php | |
require_once 'SqliteStore.php'; | |
$ss = new SqliteStore('example1'); | |
echo 'create: '; | |
$ss->set('myname', 'Alex'); | |
if($ss->get('myname') == 'Alex') | |
echo 'ok'; | |
else | |
echo 'error'; | |
echo "\n"; | |
echo 'delete and test default value: '; | |
$ss->delete('myname'); | |
if($ss->get('myname', 'deleted') == 'deleted') | |
echo 'ok'; | |
else | |
echo 'error'; | |
echo "\n"; | |
$seconds = 2; | |
echo "test delete old records after {$seconds} seconds: "; | |
$uniqid = 'u' . uniqid(); | |
$ss->set($uniqid, 'timecheck'); | |
sleep($seconds + 1); | |
// $ss->deleteOld($seconds); | |
$ss->delete_old($seconds); | |
if($ss->get($uniqid, 'expired') == 'expired') | |
echo 'ok'; | |
else | |
echo 'error'; |
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
<?php | |
// a simple key/val store using php & sqlite3 | |
// license: http://gist.github.com/375593 | |
// edited by alekssamos | |
class SqliteStore { | |
protected $db; | |
public function __construct($tableName, $filePath = 'db.sqlite') { | |
$this->db = new SQLite3($filePath); | |
$this->tableName = $this->db->escapeString($tableName); | |
if (is_numeric($tableName[0])) { | |
$details = sprintf( | |
"sqlite will choke on table names that start w/ a number. yours starts w/ '%s'", | |
$tableName[0] | |
); | |
throw new Exception($details); | |
} | |
//wrap in try/catch & ignore warnings as workaround to lack of 'if not exists' in sqlite version | |
try { | |
$sql = "create table $tableName ( key text primary key, value text, tm INT )"; | |
@$this->db->query( $sql ); | |
} catch ( Exception $e ) { | |
// var_dump($e); | |
} | |
} | |
public function get($key, $defvalue = '') { | |
$sql = sprintf( | |
"SELECT value FROM %s WHERE key = '%s';", | |
$this->tableName, $this->db->escapeString($key) | |
); | |
$result = $this->db->query($sql)->fetchArray(SQLITE3_ASSOC); | |
if ($result) { | |
$result = $result['value']; | |
} else { $result = $defvalue; } | |
return $result; | |
} | |
public function set($key, $value){ | |
$time = time(); | |
$sql = sprintf( | |
"REPLACE INTO %s (key, value, tm) VALUES ('%s', '%s', %d);", | |
$this->tableName, $this->db->escapeString($key), $this->db->escapeString($value), $time | |
); | |
//allow exceptions to bubble up | |
$this->db->exec($sql); | |
} | |
public function delete($key){ | |
$sql = sprintf( | |
"DELETE FROM %s WHERE key = '%s';", | |
$this->tableName, $this->db->escapeString($key) | |
); | |
//allow exceptions to bubble up | |
$this->db->exec($sql); | |
} | |
public function deleteOld($seconds){ | |
$time = time(); | |
$sql = sprintf( | |
"DELETE FROM %s WHERE (%d - tm > %d);", | |
$this->tableName, $time, $seconds | |
); | |
//allow exceptions to bubble up | |
$this->db->exec($sql); | |
} | |
public function delete_old($seconds){ | |
return $this->deleteOld($seconds); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like this class and this functions.