Skip to content

Instantly share code, notes, and snippets.

@alekssamos
Forked from erikeldridge/SqliteStore.php
Last active September 22, 2024 11:17
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
/* Added multithreading: https://habr.com/ru/articles/204438/ */
class SqliteStore {
protected $db;
public function __construct($tableName, $filePath = 'db.sqlite') {
$this->db = new SQLite3($filePath);
$this->db->busyTimeout(5000);
$this->db->exec('PRAGMA journal_mode=WAL;');
$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)
);
$this->db->exec('BEGIN IMMEDIATE;');
$result = $this->db->query($sql)->fetchArray(SQLITE3_ASSOC);
$this->db->exec('COMMIT;');
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('BEGIN IMMEDIATE;');
$this->db->exec($sql);
$this->db->exec('COMMIT;');
}
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('BEGIN IMMEDIATE;');
$this->db->exec($sql);
$this->db->exec('COMMIT;');
}
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('BEGIN IMMEDIATE;');
$this->db->exec($sql);
$this->db->exec('COMMIT;');
}
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.

@alekssamos
Copy link
Author

Added work in multithreaded mode.
https://habr.com/ru/articles/204438/

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