aboutsummaryrefslogtreecommitdiff
path: root/caches
diff options
context:
space:
mode:
authorGravatar Dag <me@dvikan.no> 2022-07-01 15:10:30 +0200
committerGravatar GitHub <noreply@github.com> 2022-07-01 15:10:30 +0200
commit4f75591060d95208a301bc6bf460d875631b29cc (patch)
tree4e37d86840e8d990a563ba75d3de6f84a53cc2de /caches
parent66568e3a39c61546c09a47a5688914a0bdf3c60c (diff)
downloadrss-bridge-4f75591060d95208a301bc6bf460d875631b29cc.tar.gz
rss-bridge-4f75591060d95208a301bc6bf460d875631b29cc.tar.zst
rss-bridge-4f75591060d95208a301bc6bf460d875631b29cc.zip
Reformat codebase v4 (#2872)
Reformat code base to PSR12 Co-authored-by: rssbridge <noreply@github.com>
Diffstat (limited to 'caches')
-rw-r--r--caches/FileCache.php277
-rw-r--r--caches/MemcachedCache.php237
-rw-r--r--caches/SQLiteCache.php256
3 files changed, 402 insertions, 368 deletions
diff --git a/caches/FileCache.php b/caches/FileCache.php
index 1b8ae6cd..29f4d78b 100644
--- a/caches/FileCache.php
+++ b/caches/FileCache.php
@@ -1,137 +1,150 @@
<?php
+
/**
* Cache with file system
*/
-class FileCache implements CacheInterface {
- protected $path;
- protected $key;
-
- public function __construct() {
- if (!is_writable(PATH_CACHE)) {
- returnServerError(
- 'RSS-Bridge does not have write permissions for '
- . PATH_CACHE . '!'
- );
- }
- }
-
- public function loadData(){
- if(file_exists($this->getCacheFile())) {
- return unserialize(file_get_contents($this->getCacheFile()));
- }
-
- return null;
- }
-
- public function saveData($data){
- // Notice: We use plain serialize() here to reduce memory footprint on
- // large input data.
- $writeStream = file_put_contents($this->getCacheFile(), serialize($data));
-
- if($writeStream === false) {
- throw new \Exception('Cannot write the cache... Do you have the right permissions ?');
- }
-
- return $this;
- }
-
- public function getTime(){
- $cacheFile = $this->getCacheFile();
- clearstatcache(false, $cacheFile);
- if(file_exists($cacheFile)) {
- $time = filemtime($cacheFile);
- return ($time !== false) ? $time : null;
- }
-
- return null;
- }
-
- public function purgeCache($seconds){
- $cachePath = $this->getPath();
- if(file_exists($cachePath)) {
- $cacheIterator = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($cachePath),
- RecursiveIteratorIterator::CHILD_FIRST
- );
-
- foreach($cacheIterator as $cacheFile) {
- if(in_array($cacheFile->getBasename(), array('.', '..', '.gitkeep')))
- continue;
- elseif($cacheFile->isFile()) {
- if(filemtime($cacheFile->getPathname()) < time() - $seconds)
- unlink($cacheFile->getPathname());
- }
- }
- }
- }
-
- /**
- * Set scope
- * @return self
- */
- public function setScope($scope){
- if(is_null($scope) || !is_string($scope)) {
- throw new \Exception('The given scope is invalid!');
- }
-
- $this->path = PATH_CACHE . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
-
- return $this;
- }
-
- /**
- * Set key
- * @return self
- */
- public function setKey($key){
- if (!empty($key) && is_array($key)) {
- $key = array_map('strtolower', $key);
- }
- $key = json_encode($key);
-
- if (!is_string($key)) {
- throw new \Exception('The given key is invalid!');
- }
-
- $this->key = $key;
- return $this;
- }
-
- /**
- * Return cache path (and create if not exist)
- * @return string Cache path
- */
- private function getPath(){
- if(is_null($this->path)) {
- throw new \Exception('Call "setScope" first!');
- }
-
- if(!is_dir($this->path)) {
- if (mkdir($this->path, 0755, true) !== true) {
- throw new \Exception('Unable to create ' . $this->path);
- }
- }
-
- return $this->path;
- }
-
- /**
- * Get the file name use for cache store
- * @return string Path to the file cache
- */
- private function getCacheFile(){
- return $this->getPath() . $this->getCacheName();
- }
-
- /**
- * Determines file name for store the cache
- * return string
- */
- private function getCacheName(){
- if(is_null($this->key)) {
- throw new \Exception('Call "setKey" first!');
- }
-
- return hash('md5', $this->key) . '.cache';
- }
+class FileCache implements CacheInterface
+{
+ protected $path;
+ protected $key;
+
+ public function __construct()
+ {
+ if (!is_writable(PATH_CACHE)) {
+ returnServerError(
+ 'RSS-Bridge does not have write permissions for '
+ . PATH_CACHE . '!'
+ );
+ }
+ }
+
+ public function loadData()
+ {
+ if (file_exists($this->getCacheFile())) {
+ return unserialize(file_get_contents($this->getCacheFile()));
+ }
+
+ return null;
+ }
+
+ public function saveData($data)
+ {
+ // Notice: We use plain serialize() here to reduce memory footprint on
+ // large input data.
+ $writeStream = file_put_contents($this->getCacheFile(), serialize($data));
+
+ if ($writeStream === false) {
+ throw new \Exception('Cannot write the cache... Do you have the right permissions ?');
+ }
+
+ return $this;
+ }
+
+ public function getTime()
+ {
+ $cacheFile = $this->getCacheFile();
+ clearstatcache(false, $cacheFile);
+ if (file_exists($cacheFile)) {
+ $time = filemtime($cacheFile);
+ return ($time !== false) ? $time : null;
+ }
+
+ return null;
+ }
+
+ public function purgeCache($seconds)
+ {
+ $cachePath = $this->getPath();
+ if (file_exists($cachePath)) {
+ $cacheIterator = new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($cachePath),
+ RecursiveIteratorIterator::CHILD_FIRST
+ );
+
+ foreach ($cacheIterator as $cacheFile) {
+ if (in_array($cacheFile->getBasename(), ['.', '..', '.gitkeep'])) {
+ continue;
+ } elseif ($cacheFile->isFile()) {
+ if (filemtime($cacheFile->getPathname()) < time() - $seconds) {
+ unlink($cacheFile->getPathname());
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Set scope
+ * @return self
+ */
+ public function setScope($scope)
+ {
+ if (is_null($scope) || !is_string($scope)) {
+ throw new \Exception('The given scope is invalid!');
+ }
+
+ $this->path = PATH_CACHE . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
+
+ return $this;
+ }
+
+ /**
+ * Set key
+ * @return self
+ */
+ public function setKey($key)
+ {
+ if (!empty($key) && is_array($key)) {
+ $key = array_map('strtolower', $key);
+ }
+ $key = json_encode($key);
+
+ if (!is_string($key)) {
+ throw new \Exception('The given key is invalid!');
+ }
+
+ $this->key = $key;
+ return $this;
+ }
+
+ /**
+ * Return cache path (and create if not exist)
+ * @return string Cache path
+ */
+ private function getPath()
+ {
+ if (is_null($this->path)) {
+ throw new \Exception('Call "setScope" first!');
+ }
+
+ if (!is_dir($this->path)) {
+ if (mkdir($this->path, 0755, true) !== true) {
+ throw new \Exception('Unable to create ' . $this->path);
+ }
+ }
+
+ return $this->path;
+ }
+
+ /**
+ * Get the file name use for cache store
+ * @return string Path to the file cache
+ */
+ private function getCacheFile()
+ {
+ return $this->getPath() . $this->getCacheName();
+ }
+
+ /**
+ * Determines file name for store the cache
+ * return string
+ */
+ private function getCacheName()
+ {
+ if (is_null($this->key)) {
+ throw new \Exception('Call "setKey" first!');
+ }
+
+ return hash('md5', $this->key) . '.cache';
+ }
}
diff --git a/caches/MemcachedCache.php b/caches/MemcachedCache.php
index b431279a..8619c255 100644
--- a/caches/MemcachedCache.php
+++ b/caches/MemcachedCache.php
@@ -1,115 +1,126 @@
<?php
-class MemcachedCache implements CacheInterface {
-
- private $scope;
- private $key;
- private $conn;
- private $expiration = 0;
- private $time = false;
- private $data = null;
-
- public function __construct() {
- if (!extension_loaded('memcached')) {
- returnServerError('"memcached" extension not loaded. Please check "php.ini"');
- }
-
- $host = Configuration::getConfig(get_called_class(), 'host');
- $port = Configuration::getConfig(get_called_class(), 'port');
- if (empty($host) && empty($port)) {
- returnServerError('Configuration for ' . get_called_class() . ' missing. Please check your ' . FILE_CONFIG);
- } else if (empty($host)) {
- returnServerError('"host" param is not set for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
- } else if (empty($port)) {
- returnServerError('"port" param is not set for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
- } else if (!ctype_digit($port)) {
- returnServerError('"port" param is invalid for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
- }
-
- $port = intval($port);
-
- if ($port < 1 || $port > 65535) {
- returnServerError('"port" param is invalid for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
- }
-
- $conn = new Memcached();
- $conn->addServer($host, $port) or returnServerError('Could not connect to memcached server');
- $this->conn = $conn;
- }
-
- public function loadData(){
- if ($this->data) return $this->data;
- $result = $this->conn->get($this->getCacheKey());
- if ($result === false) {
- return null;
- }
-
- $this->time = $result['time'];
- $this->data = $result['data'];
- return $result['data'];
- }
-
- public function saveData($datas){
- $time = time();
- $object_to_save = array(
- 'data' => $datas,
- 'time' => $time,
- );
- $result = $this->conn->set($this->getCacheKey(), $object_to_save, $this->expiration);
-
- if($result === false) {
- returnServerError('Cannot write the cache to memcached server');
- }
-
- $this->time = $time;
-
- return $this;
- }
-
- public function getTime(){
- if ($this->time === false) {
- $this->loadData();
- }
- return $this->time;
- }
-
- public function purgeCache($duration){
- // Note: does not purges cache right now
- // Just sets cache expiration and leave cache purging for memcached itself
- $this->expiration = $duration;
- }
-
- /**
- * Set scope
- * @return self
- */
- public function setScope($scope){
- $this->scope = $scope;
- return $this;
- }
-
- /**
- * Set key
- * @return self
- */
- public function setKey($key){
- if (!empty($key) && is_array($key)) {
- $key = array_map('strtolower', $key);
- }
- $key = json_encode($key);
-
- if (!is_string($key)) {
- throw new \Exception('The given key is invalid!');
- }
-
- $this->key = $key;
- return $this;
- }
-
- private function getCacheKey(){
- if(is_null($this->key)) {
- returnServerError('Call "setKey" first!');
- }
-
- return 'rss_bridge_cache_' . hash('md5', $this->scope . $this->key . 'A');
- }
+
+class MemcachedCache implements CacheInterface
+{
+ private $scope;
+ private $key;
+ private $conn;
+ private $expiration = 0;
+ private $time = false;
+ private $data = null;
+
+ public function __construct()
+ {
+ if (!extension_loaded('memcached')) {
+ returnServerError('"memcached" extension not loaded. Please check "php.ini"');
+ }
+
+ $host = Configuration::getConfig(get_called_class(), 'host');
+ $port = Configuration::getConfig(get_called_class(), 'port');
+ if (empty($host) && empty($port)) {
+ returnServerError('Configuration for ' . get_called_class() . ' missing. Please check your ' . FILE_CONFIG);
+ } elseif (empty($host)) {
+ returnServerError('"host" param is not set for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
+ } elseif (empty($port)) {
+ returnServerError('"port" param is not set for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
+ } elseif (!ctype_digit($port)) {
+ returnServerError('"port" param is invalid for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
+ }
+
+ $port = intval($port);
+
+ if ($port < 1 || $port > 65535) {
+ returnServerError('"port" param is invalid for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
+ }
+
+ $conn = new Memcached();
+ $conn->addServer($host, $port) or returnServerError('Could not connect to memcached server');
+ $this->conn = $conn;
+ }
+
+ public function loadData()
+ {
+ if ($this->data) {
+ return $this->data;
+ }
+ $result = $this->conn->get($this->getCacheKey());
+ if ($result === false) {
+ return null;
+ }
+
+ $this->time = $result['time'];
+ $this->data = $result['data'];
+ return $result['data'];
+ }
+
+ public function saveData($datas)
+ {
+ $time = time();
+ $object_to_save = [
+ 'data' => $datas,
+ 'time' => $time,
+ ];
+ $result = $this->conn->set($this->getCacheKey(), $object_to_save, $this->expiration);
+
+ if ($result === false) {
+ returnServerError('Cannot write the cache to memcached server');
+ }
+
+ $this->time = $time;
+
+ return $this;
+ }
+
+ public function getTime()
+ {
+ if ($this->time === false) {
+ $this->loadData();
+ }
+ return $this->time;
+ }
+
+ public function purgeCache($duration)
+ {
+ // Note: does not purges cache right now
+ // Just sets cache expiration and leave cache purging for memcached itself
+ $this->expiration = $duration;
+ }
+
+ /**
+ * Set scope
+ * @return self
+ */
+ public function setScope($scope)
+ {
+ $this->scope = $scope;
+ return $this;
+ }
+
+ /**
+ * Set key
+ * @return self
+ */
+ public function setKey($key)
+ {
+ if (!empty($key) && is_array($key)) {
+ $key = array_map('strtolower', $key);
+ }
+ $key = json_encode($key);
+
+ if (!is_string($key)) {
+ throw new \Exception('The given key is invalid!');
+ }
+
+ $this->key = $key;
+ return $this;
+ }
+
+ private function getCacheKey()
+ {
+ if (is_null($this->key)) {
+ returnServerError('Call "setKey" first!');
+ }
+
+ return 'rss_bridge_cache_' . hash('md5', $this->scope . $this->key . 'A');
+ }
}
diff --git a/caches/SQLiteCache.php b/caches/SQLiteCache.php
index 5ec69417..e8d020a5 100644
--- a/caches/SQLiteCache.php
+++ b/caches/SQLiteCache.php
@@ -1,128 +1,138 @@
<?php
+
/**
* Cache based on SQLite 3 <https://www.sqlite.org>
*/
-class SQLiteCache implements CacheInterface {
- protected $scope;
- protected $key;
-
- private $db = null;
-
- public function __construct() {
- if (!extension_loaded('sqlite3')) {
- die('"sqlite3" extension not loaded. Please check "php.ini"');
- }
-
- if (!is_writable(PATH_CACHE)) {
- returnServerError(
- 'RSS-Bridge does not have write permissions for '
- . PATH_CACHE . '!'
- );
- }
-
- $file = Configuration::getConfig(get_called_class(), 'file');
- if (empty($file)) {
- die('Configuration for ' . get_called_class() . ' missing. Please check your ' . FILE_CONFIG);
- }
- if (dirname($file) == '.') {
- $file = PATH_CACHE . $file;
- } elseif (!is_dir(dirname($file))) {
- die('Invalid configuration for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
- }
-
- if (!is_file($file)) {
- $this->db = new SQLite3($file);
- $this->db->enableExceptions(true);
- $this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
- } else {
- $this->db = new SQLite3($file);
- $this->db->enableExceptions(true);
- }
- $this->db->busyTimeout(5000);
- }
-
- public function loadData(){
- $Qselect = $this->db->prepare('SELECT value FROM storage WHERE key = :key');
- $Qselect->bindValue(':key', $this->getCacheKey());
- $result = $Qselect->execute();
- if ($result instanceof SQLite3Result) {
- $data = $result->fetchArray(SQLITE3_ASSOC);
- if (isset($data['value'])) {
- return unserialize($data['value']);
- }
- }
-
- return null;
- }
-
- public function saveData($data){
- $Qupdate = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
- $Qupdate->bindValue(':key', $this->getCacheKey());
- $Qupdate->bindValue(':value', serialize($data));
- $Qupdate->bindValue(':updated', time());
- $Qupdate->execute();
-
- return $this;
- }
-
- public function getTime(){
- $Qselect = $this->db->prepare('SELECT updated FROM storage WHERE key = :key');
- $Qselect->bindValue(':key', $this->getCacheKey());
- $result = $Qselect->execute();
- if ($result instanceof SQLite3Result) {
- $data = $result->fetchArray(SQLITE3_ASSOC);
- if (isset($data['updated'])) {
- return $data['updated'];
- }
- }
-
- return null;
- }
-
- public function purgeCache($seconds){
- $Qdelete = $this->db->prepare('DELETE FROM storage WHERE updated < :expired');
- $Qdelete->bindValue(':expired', time() - $seconds);
- $Qdelete->execute();
- }
-
- /**
- * Set scope
- * @return self
- */
- public function setScope($scope){
- if(is_null($scope) || !is_string($scope)) {
- throw new \Exception('The given scope is invalid!');
- }
-
- $this->scope = $scope;
- return $this;
- }
-
- /**
- * Set key
- * @return self
- */
- public function setKey($key){
- if (!empty($key) && is_array($key)) {
- $key = array_map('strtolower', $key);
- }
- $key = json_encode($key);
-
- if (!is_string($key)) {
- throw new \Exception('The given key is invalid!');
- }
-
- $this->key = $key;
- return $this;
- }
-
- ////////////////////////////////////////////////////////////////////////////
-
- private function getCacheKey(){
- if(is_null($this->key)) {
- throw new \Exception('Call "setKey" first!');
- }
-
- return hash('sha1', $this->scope . $this->key, true);
- }
+class SQLiteCache implements CacheInterface
+{
+ protected $scope;
+ protected $key;
+
+ private $db = null;
+
+ public function __construct()
+ {
+ if (!extension_loaded('sqlite3')) {
+ die('"sqlite3" extension not loaded. Please check "php.ini"');
+ }
+
+ if (!is_writable(PATH_CACHE)) {
+ returnServerError(
+ 'RSS-Bridge does not have write permissions for '
+ . PATH_CACHE . '!'
+ );
+ }
+
+ $file = Configuration::getConfig(get_called_class(), 'file');
+ if (empty($file)) {
+ die('Configuration for ' . get_called_class() . ' missing. Please check your ' . FILE_CONFIG);
+ }
+ if (dirname($file) == '.') {
+ $file = PATH_CACHE . $file;
+ } elseif (!is_dir(dirname($file))) {
+ die('Invalid configuration for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
+ }
+
+ if (!is_file($file)) {
+ $this->db = new SQLite3($file);
+ $this->db->enableExceptions(true);
+ $this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
+ } else {
+ $this->db = new SQLite3($file);
+ $this->db->enableExceptions(true);
+ }
+ $this->db->busyTimeout(5000);
+ }
+
+ public function loadData()
+ {
+ $Qselect = $this->db->prepare('SELECT value FROM storage WHERE key = :key');
+ $Qselect->bindValue(':key', $this->getCacheKey());
+ $result = $Qselect->execute();
+ if ($result instanceof SQLite3Result) {
+ $data = $result->fetchArray(SQLITE3_ASSOC);
+ if (isset($data['value'])) {
+ return unserialize($data['value']);
+ }
+ }
+
+ return null;
+ }
+
+ public function saveData($data)
+ {
+ $Qupdate = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
+ $Qupdate->bindValue(':key', $this->getCacheKey());
+ $Qupdate->bindValue(':value', serialize($data));
+ $Qupdate->bindValue(':updated', time());
+ $Qupdate->execute();
+
+ return $this;
+ }
+
+ public function getTime()
+ {
+ $Qselect = $this->db->prepare('SELECT updated FROM storage WHERE key = :key');
+ $Qselect->bindValue(':key', $this->getCacheKey());
+ $result = $Qselect->execute();
+ if ($result instanceof SQLite3Result) {
+ $data = $result->fetchArray(SQLITE3_ASSOC);
+ if (isset($data['updated'])) {
+ return $data['updated'];
+ }
+ }
+
+ return null;
+ }
+
+ public function purgeCache($seconds)
+ {
+ $Qdelete = $this->db->prepare('DELETE FROM storage WHERE updated < :expired');
+ $Qdelete->bindValue(':expired', time() - $seconds);
+ $Qdelete->execute();
+ }
+
+ /**
+ * Set scope
+ * @return self
+ */
+ public function setScope($scope)
+ {
+ if (is_null($scope) || !is_string($scope)) {
+ throw new \Exception('The given scope is invalid!');
+ }
+
+ $this->scope = $scope;
+ return $this;
+ }
+
+ /**
+ * Set key
+ * @return self
+ */
+ public function setKey($key)
+ {
+ if (!empty($key) && is_array($key)) {
+ $key = array_map('strtolower', $key);
+ }
+ $key = json_encode($key);
+
+ if (!is_string($key)) {
+ throw new \Exception('The given key is invalid!');
+ }
+
+ $this->key = $key;
+ return $this;
+ }
+
+ ////////////////////////////////////////////////////////////////////////////
+
+ private function getCacheKey()
+ {
+ if (is_null($this->key)) {
+ throw new \Exception('Call "setKey" first!');
+ }
+
+ return hash('sha1', $this->scope . $this->key, true);
+ }
}