-
Notifications
You must be signed in to change notification settings - Fork 198
/
Cache.php
150 lines (130 loc) · 3.91 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php declare(strict_types=1);
/**
* ownCloud - Music app
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Pauli Järvinen <[email protected]>
* @copyright Pauli Järvinen 2017 - 2021
*/
namespace OCA\Music\Db;
use OCP\IDBConnection;
use OCA\Music\AppFramework\Db\UniqueConstraintViolationException;
class Cache {
private $db;
public function __construct(IDBConnection $db) {
$this->db = $db;
}
/**
* @param string $userId
* @param string $key
* @param string $data
*/
public function add(string $userId, string $key, string $data) : void {
$sql = 'INSERT INTO `*PREFIX*music_cache`
(`user_id`, `key`, `data`) VALUES (?, ?, ?)';
$this->executeUpdate($sql, [$userId, $key, $data]);
}
/**
* @param string $userId
* @param string $key
* @param string $data
*/
public function update(string $userId, string $key, string $data) : void {
$sql = 'UPDATE `*PREFIX*music_cache` SET `data` = ?
WHERE `user_id` = ? AND `key` = ?';
$this->executeUpdate($sql, [$data, $userId, $key]);
}
/**
* @param string $userId
* @param string $key
* @param string $data
*/
public function set(string $userId, string $key, string $data) : void {
try {
$this->add($userId, $key, $data);
} catch (UniqueConstraintViolationException $e) {
$this->update($userId, $key, $data);
}
}
/**
* Remove one or several key-value pairs
*
* @param string $userId User to target, omit to target all users
* @param string $key Key to target, omit to target all keys
*/
public function remove(string $userId = null, string $key = null) : void {
$sql = 'DELETE FROM `*PREFIX*music_cache`';
$params = [];
if ($userId !== null) {
$sql .= ' WHERE `user_id` = ?';
$params[] = $userId;
if ($key !== null) {
$sql .= ' AND `key` = ?';
$params[] = $key;
}
} elseif ($key !== null) {
$sql .= ' WHERE `key` = ?';
$params[] = $key;
}
$this->executeUpdate($sql, $params);
}
/**
* @param string $userId
* @param string $key
* @return string|null
*/
public function get(string $userId, string $key) : ?string {
$sql = 'SELECT `data` FROM `*PREFIX*music_cache`
WHERE `user_id` = ? AND `key` = ?';
$result = $this->db->executeQuery($sql, [$userId, $key]);
$rows = $result->fetchAll();
$result->closeCursor();
return \count($rows) ? $rows[0]['data'] : null;
}
/**
* Get all key-value pairs of one user, optionally limiting to keys with a given prefix.
* @param string $userId
* @param string|null $prefix
* @return array of arrays with keys 'key', 'data'
*/
public function getAll(string $userId, string $prefix = null) : array {
$sql = 'SELECT `key`, `data` FROM `*PREFIX*music_cache`
WHERE `user_id` = ?';
$params = [$userId];
if (!empty($prefix)) {
$sql .= ' AND `key` LIKE ?';
$params[] = $prefix . '%';
}
$result = $this->db->executeQuery($sql, $params);
$rows = $result->fetchAll();
$result->closeCursor();
return $rows;
}
/**
* Given a cache key and its exact content, return the owning user
*/
public function getOwner(string $key, string $data) : ?string {
$sql = 'SELECT `user_id` FROM `*PREFIX*music_cache`
WHERE `key` = ? AND `data` = ?';
$result = $this->db->executeQuery($sql, [$key, $data]);
$rows = $result->fetchAll();
$result->closeCursor();
return \count($rows) ? $rows[0]['user_id'] : null;
}
private function executeUpdate(string $sql, array $params) {
try {
return $this->db->executeUpdate($sql, $params);
} catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
throw new UniqueConstraintViolationException($e->getMessage(), $e->getCode(), $e);
} catch (\OCP\DB\Exception $e) {
// Nextcloud 21
if ($e->getReason() == \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw new UniqueConstraintViolationException($e->getMessage(), $e->getCode(), $e);
} else {
throw $e;
}
}
}
}