Add libs
This commit is contained in:
parent
79ec05a7b7
commit
25a7610a93
21
index.php
21
index.php
@ -18,26 +18,13 @@ $SETTINGS = [
|
|||||||
"charset" => "utf8"
|
"charset" => "utf8"
|
||||||
],
|
],
|
||||||
"memcached" => [
|
"memcached" => [
|
||||||
"enable" => false,
|
"enable" => true,
|
||||||
"server" => "127.0.0.1",
|
"server" => "127.0.0.1",
|
||||||
"port" => 11211,
|
"port" => 11211,
|
||||||
"prefix" => "aa"
|
"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
|
ob_start(); // allow sending headers after content
|
||||||
// Unicode, solves almost all stupid encoding problems
|
// Unicode, solves almost all stupid encoding problems
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
@ -48,6 +35,12 @@ header("Access-Control-Allow-Origin: *");
|
|||||||
// Composer
|
// Composer
|
||||||
require __DIR__ . '/vendor/autoload.php';
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
$libs = glob(__DIR__ . "/lib/*.lib.php");
|
||||||
|
foreach ($libs as $lib) {
|
||||||
|
require_once $lib;
|
||||||
|
}
|
||||||
|
unset($libs, $lib);
|
||||||
|
|
||||||
use Medoo\Medoo;
|
use Medoo\Medoo;
|
||||||
|
|
||||||
$database;
|
$database;
|
||||||
|
20
lib/Env.lib.php
Normal file
20
lib/Env.lib.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
93
lib/MemcacheDriver.lib.php
Normal file
93
lib/MemcacheDriver.lib.php
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class MemcacheDriver {
|
||||||
|
|
||||||
|
public $memcache;
|
||||||
|
private bool $enabled;
|
||||||
|
private string $server;
|
||||||
|
private string $prefix;
|
||||||
|
private int $port;
|
||||||
|
private $dummycache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param bool $enabled true to enable memcached, false to use non-persistent dummy cache
|
||||||
|
* @param string $server
|
||||||
|
* @param int $port
|
||||||
|
* @param string $prefix Prefix to add to keys to avoid conflicts with other apps on same server
|
||||||
|
*/
|
||||||
|
function __construct(bool $enabled = false, string $server = "127.0.0.1", int $port = 11211, string $prefix = "") {
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user