aboutsummaryrefslogtreecommitdiff
path: root/caches/SQLiteCache.php
diff options
context:
space:
mode:
authorGravatar Dag <me@dvikan.no> 2023-07-15 22:12:16 +0200
committerGravatar GitHub <noreply@github.com> 2023-07-15 22:12:16 +0200
commitef8181478d62b7a558dcf9821076882b13443b0f (patch)
tree8320332c6609b0ef2a8b286f674f18dd3b50ff34 /caches/SQLiteCache.php
parenteaea8e664078db55dd32dd227bad34acb6c46229 (diff)
downloadrss-bridge-ef8181478d62b7a558dcf9821076882b13443b0f.tar.gz
rss-bridge-ef8181478d62b7a558dcf9821076882b13443b0f.tar.zst
rss-bridge-ef8181478d62b7a558dcf9821076882b13443b0f.zip
perf(SqliteCache): add index to updated (#3515)
* refactor(SqliteCache) * perf(SqliteCache): add index to updated
Diffstat (limited to 'caches/SQLiteCache.php')
-rw-r--r--caches/SQLiteCache.php23
1 files changed, 10 insertions, 13 deletions
diff --git a/caches/SQLiteCache.php b/caches/SQLiteCache.php
index 309b86d1..cb9f33b1 100644
--- a/caches/SQLiteCache.php
+++ b/caches/SQLiteCache.php
@@ -1,8 +1,5 @@
<?php
-/**
- * Cache based on SQLite 3 <https://www.sqlite.org>
- */
class SQLiteCache implements CacheInterface
{
private \SQLite3 $db;
@@ -32,6 +29,7 @@ class SQLiteCache implements CacheInterface
$this->db = new \SQLite3($config['file']);
$this->db->enableExceptions(true);
$this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
+ $this->db->exec('CREATE INDEX idx_storage_updated ON storage (updated)');
}
$this->db->busyTimeout($config['timeout']);
}
@@ -42,20 +40,22 @@ class SQLiteCache implements CacheInterface
$stmt->bindValue(':key', $this->getCacheKey());
$result = $stmt->execute();
if ($result) {
- $data = $result->fetchArray(\SQLITE3_ASSOC);
- if (isset($data['value'])) {
- return unserialize($data['value']);
+ $row = $result->fetchArray(\SQLITE3_ASSOC);
+ $data = unserialize($row['value']);
+ if ($data !== false) {
+ return $data;
}
}
-
return null;
}
public function saveData($data): void
{
+ $blob = serialize($data);
+
$stmt = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
$stmt->bindValue(':key', $this->getCacheKey());
- $stmt->bindValue(':value', serialize($data));
+ $stmt->bindValue(':value', $blob);
$stmt->bindValue(':updated', time());
$stmt->execute();
}
@@ -66,12 +66,9 @@ class SQLiteCache implements CacheInterface
$stmt->bindValue(':key', $this->getCacheKey());
$result = $stmt->execute();
if ($result) {
- $data = $result->fetchArray(\SQLITE3_ASSOC);
- if (isset($data['updated'])) {
- return $data['updated'];
- }
+ $row = $result->fetchArray(\SQLITE3_ASSOC);
+ return $row['updated'];
}
-
return null;
}