Skip to content

Instantly share code, notes, and snippets.

@alekssamos
Forked from erikeldridge/SqliteStore.php
Last active October 3, 2020 18:36
Show Gist options
  • Save alekssamos/7cfafb993e3d195375cad8020e047701 to your computer and use it in GitHub Desktop.
Save alekssamos/7cfafb993e3d195375cad8020e047701 to your computer and use it in GitHub Desktop.
a simple key/val store using php & sqlite3
<?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';
<?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);
}
}
?>
@alekssamos
Copy link
Author

alekssamos commented Mar 10, 2020

  • updated from SQLite (PHP 5 < 5.4.0, PECL sqlite >= 1.0.0) to SQLite 3 (PHP 5 >= 5.3.0, PHP 7).
  • added delete and deleteOld functions
  • default value for get if not exists.

I like this class and this functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment