From 25a7610a9328e74a3eff9e61b6d947a30e0f2861 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 25 Apr 2023 17:50:39 -0600 Subject: [PATCH] Add libs --- index.php | 21 +++------ lib/Env.lib.php | 20 ++++++++ lib/MemcacheDriver.lib.php | 93 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 lib/Env.lib.php create mode 100644 lib/MemcacheDriver.lib.php diff --git a/index.php b/index.php index 27eee8d..a91f19a 100644 --- a/index.php +++ b/index.php @@ -18,26 +18,13 @@ $SETTINGS = [ "charset" => "utf8" ], "memcached" => [ - "enable" => false, + "enable" => true, "server" => "127.0.0.1", "port" => 11211, "prefix" => "aa" ] ]; -function env(string $key, $defaultvalue = null) { - global $SETTINGS; - if (!empty($SETTINGS[$key])) { - return $SETTINGS[$key]; - } - return $defaultvalue; -} - -function envhas(string $key): bool { - global $SETTINGS; - return !empty($SETTINGS[$key]); -} - ob_start(); // allow sending headers after content // Unicode, solves almost all stupid encoding problems header('Content-Type: application/json; charset=utf-8'); @@ -48,6 +35,12 @@ header("Access-Control-Allow-Origin: *"); // Composer require __DIR__ . '/vendor/autoload.php'; +$libs = glob(__DIR__ . "/lib/*.lib.php"); +foreach ($libs as $lib) { + require_once $lib; +} +unset($libs, $lib); + use Medoo\Medoo; $database; diff --git a/lib/Env.lib.php b/lib/Env.lib.php new file mode 100644 index 0000000..d922c18 --- /dev/null +++ b/lib/Env.lib.php @@ -0,0 +1,20 @@ +enabled = $enabled; + $this->server = $server; + $this->port = $port; + $this->prefix = $prefix; + if ($enabled) { + $this->memcache = new Memcached(); + $this->memcache->addServer($this->server, $this->port); + } + $this->dummycache = []; + } + + /** + * Check if the memcache is actually setup or if it's a dummy shim. + * @return bool true if memcache is enabled, false if only dummy cache enabled + */ + function enabled(): bool { + return $this->enabled; + } + + /** + * Get the value for key. + * @param string $key + * @return value or false if not found + */ + function get(string $key) { + if ($this->enabled) { + $key = $this->prefix . $key; + return $this->memcache->get($key); + } else { + if (!empty($this->dummycache[$key])) { + return $this->dummycache[$key]; + } + return false; + } + } + + /** + * Set the value of $key to $val + * @param string $key + * @param type $val + * @param int $expires number of seconds (max 60*60*24*30) or UNIX timestamp for cache expiry + * Default expiration of 0 means no expiration + */ + function set(string $key, $val, int $expires = 0) { + if ($expires < 0) { + $expires = 0; + } + if ($this->enabled) { + $key = $this->prefix . $key; + $this->memcache->set($key, $val, $expires); + } else { + $this->dummycache[$key] = $val; + } + } + + /** + * Get the cache key prefix string + * @return string + */ + function getPrefix(): string { + return $this->prefix; + } + + function getMemcached() { + return $this->memcache; + } + +}