Browse Source

Add ubiquity memory cache (#5879)

* Fix cache pb with pgsql

* add CachedWorld model

* add controller and warmup for cache

* fix set cache omited

* Fix warmup Cache controller omited

* fix typo

* update to Ubuntu 20.04 + add preloading

* Remove swoole pecl version

* remove swoole pecl version

* update to Ubuntu 20.04

* Fix mysql version in docker file
jcheron 5 years ago
parent
commit
88fc80b154

+ 2 - 0
frameworks/PHP/ubiquity/app/cache/models/CachedWorld.cache.php

@@ -0,0 +1,2 @@
+<?php
+return array("#tableName"=>"World","#primaryKeys"=>array("id"=>"id"),"#manyToOne"=>array(),"#fieldNames"=>array("id"=>"id","randomNumber"=>"randomnumber"),"#fieldTypes"=>array("id"=>"int(11)","randomNumber"=>"int(11)"),"#nullable"=>array(),"#notSerializable"=>array(),"#transformers"=>array(),"#accessors"=>array("id"=>"setId","randomNumber"=>"setRandomNumber"));

+ 1 - 1
frameworks/PHP/ubiquity/app/cache/models/World.cache.php

@@ -1,2 +1,2 @@
 <?php
-return array("#tableName"=>"World","#primaryKeys"=>array("id"=>"id"),"#manyToOne"=>array(),"#fieldNames"=>array("id"=>"id","randomNumber"=>"randomNumber"),"#fieldTypes"=>array("id"=>"int(11)","randomNumber"=>"int(11)"),"#nullable"=>array(),"#notSerializable"=>array(),"#transformers"=>array(),"#accessors"=>array("id"=>"setId","randomNumber"=>"setRandomNumber"));
+return array("#tableName"=>"World","#primaryKeys"=>array("id"=>"id"),"#manyToOne"=>array(),"#fieldNames"=>array("id"=>"id","randomNumber"=>"randomnumber"),"#fieldTypes"=>array("id"=>"int(11)","randomNumber"=>"int(11)"),"#nullable"=>array(),"#notSerializable"=>array(),"#transformers"=>array(),"#accessors"=>array("id"=>"setId","randomNumber"=>"setRandomNumber"));

+ 13 - 0
frameworks/PHP/ubiquity/app/config/config.php

@@ -27,6 +27,19 @@ return array(
 			],
 			"cache" => false
 		],
+		'pgsql-cache' => [
+			"wrapper" => "\\Ubiquity\\db\\providers\\pdo\\PDOWrapper",
+			"type" => "pgsql",
+			"dbName" => "hello_world",
+			"serverName" => "tfb-database", // tfb-database
+			"port" => 5432,
+			"user" => "benchmarkdbuser", // benchmarkdbuser
+			"password" => "benchmarkdbpass", // benchmarkdbpass
+			"options" => [
+				\PDO::ATTR_PERSISTENT => false
+			],
+			"cache" => false
+		],
 		'async' => [
 			"wrapper" => "\\Ubiquity\\db\\providers\\swoole\SwooleWrapper",
 			"type" => "mysql",

+ 11 - 2
frameworks/PHP/ubiquity/app/config/workerServices.php

@@ -2,13 +2,22 @@
 \Ubiquity\cache\CacheManager::startProd($config);
 \Ubiquity\orm\DAO::setModelsDatabases([
 	'models\\Fortune' => 'pgsql',
-	'models\\World' => 'pgsql'
+	'models\\World' => 'pgsql',
+	'models\\CachedWorld'=>'pgsql-cache'
 ]);
+
+\Ubiquity\orm\DAO::setCache(new \Ubiquity\cache\dao\DAOMemoryCache());
+
+echo "Loading worlds\n";
+\Ubiquity\orm\DAO::warmupCache('models\\CachedWorld','',false);
+echo "End Loading worlds\n";
+
 \Ubiquity\cache\CacheManager::warmUpControllers([
 	'controllers\\Plaintext_',
 	'controllers\\Json_',
 	'controllers\\DbPg',
-	'controllers\\Fortunes_'
+	'controllers\\Fortunes_',
+	'controllers\\Cache'
 ]);
 $workerServer->onWorkerStart = function () use ($config) {
 	\Ubiquity\orm\DAO::startDatabase($config, 'pgsql');

+ 30 - 0
frameworks/PHP/ubiquity/app/controllers/Cache.php

@@ -0,0 +1,30 @@
+<?php
+namespace controllers;
+
+
+/**
+ * Bench controller.
+ */
+class Cache extends \Ubiquity\controllers\Controller {
+
+	protected $cache;
+
+	public function __construct() {
+		$this->cache=\Ubiquity\orm\DAO::getCache();
+	}
+
+	public function initialize() {
+		\Ubiquity\utils\http\UResponse::setContentType('application/json');
+	}
+
+	public function index(){}
+
+	public function cachedquery($queries = 1) {
+		$worlds = [];
+		$queries = \min(\max($queries, 1), 500);
+		for ($i = 0; $i < $queries; ++ $i) {
+			$worlds[] = ($this->cache->fetch('models\\CachedWorld', \mt_rand(1, 10000)))->_rest;
+		}
+		echo \json_encode($worlds);
+	}
+}

+ 9 - 0
frameworks/PHP/ubiquity/app/models/CachedWorld.php

@@ -0,0 +1,9 @@
+<?php
+namespace models;
+
+/**
+ * @table("World")
+ */
+class CachedWorld extends World{
+}
+

+ 1 - 0
frameworks/PHP/ubiquity/benchmark_config.json

@@ -32,6 +32,7 @@
         "query_url": "/DbPg/query/",
         "fortune_url": "/Fortunes_",
         "update_url": "/DbPg/update/",
+        "cached_query_url": "/Cache/cachedquery/",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Fullstack",

+ 1 - 1
frameworks/PHP/ubiquity/ubiquity-roadrunner-mysql.dockerfile

@@ -1,4 +1,4 @@
-FROM ubuntu:19.10
+FROM ubuntu:20.04
 
 ARG DEBIAN_FRONTEND=noninteractive
 

+ 1 - 1
frameworks/PHP/ubiquity/ubiquity-roadrunner.dockerfile

@@ -1,5 +1,5 @@
   
-FROM ubuntu:19.10
+FROM ubuntu:20.04
 
 ARG DEBIAN_FRONTEND=noninteractive
 

+ 1 - 1
frameworks/PHP/ubiquity/ubiquity-swoole-mysql-async.dockerfile

@@ -2,7 +2,7 @@ FROM php:7.4
 
 RUN apt-get update > /dev/null
 
-RUN pecl install swoole-4.4.14 > /dev/null && \
+RUN pecl install swoole > /dev/null && \
     docker-php-ext-enable swoole
 
 RUN docker-php-ext-install pdo_mysql pcntl > /dev/null

+ 1 - 1
frameworks/PHP/ubiquity/ubiquity-swoole-mysql.dockerfile

@@ -2,7 +2,7 @@ FROM php:7.4
 
 RUN apt-get update > /dev/null
 
-RUN pecl install swoole-4.4.14 > /dev/null && \
+RUN pecl install swoole > /dev/null && \
     docker-php-ext-enable swoole
 
 RUN docker-php-ext-install pdo_mysql > /dev/null

+ 1 - 1
frameworks/PHP/ubiquity/ubiquity-swoole.dockerfile

@@ -2,7 +2,7 @@ FROM php:7.4
 
 RUN apt-get update > /dev/null
 
-RUN pecl install swoole-4.4.14 > /dev/null && \
+RUN pecl install swoole > /dev/null && \
     docker-php-ext-enable swoole
 
 RUN apt-get install -y libpq-dev \

+ 2 - 4
frameworks/PHP/ubiquity/ubiquity-workerman.dockerfile

@@ -1,5 +1,5 @@
   
-FROM ubuntu:19.10
+FROM ubuntu:20.04
 
 ARG DEBIAN_FRONTEND=noninteractive
 
@@ -33,8 +33,6 @@ RUN php composer.phar install --optimize-autoloader --classmap-authoritative --n
 
 RUN chmod 777 -R /ubiquity/.ubiquity/*
 
-RUN sed -i "s|'worker'|'pgsql'|g" /ubiquity/app/config/workerServices.php
-
-#RUN echo "opcache.preload=/ubiquity/app/config/preloader.script.php" >> /etc/php/7.4/cli/php.ini
+RUN echo "opcache.preload=/ubiquity/app/config/preloader.script.php" >> /etc/php/7.4/cli/php.ini
 
 CMD /ubiquity/vendor/bin/Ubiquity serve -t=workerman -p=8080 -h=0.0.0.0

+ 1 - 1
frameworks/PHP/ubiquity/ubiquity.dockerfile

@@ -1,4 +1,4 @@
-FROM ubuntu:19.10
+FROM ubuntu:20.04
 
 ARG DEBIAN_FRONTEND=noninteractive
 

+ 1 - 1
toolset/databases/mysql/mysql.dockerfile

@@ -15,7 +15,7 @@ ENV LANG en_US.UTF-8
 ENV LANGUAGE en_US:en
 ENV LC_ALL en_US.UTF-8
 
-ENV MYSQL_VERSION 8.0.20-1ubuntu18.04
+ENV MYSQL_VERSION 8.0.21-1ubuntu18.04
 
 # https://bugs.mysql.com/bug.php?id=90695
 RUN ["/bin/bash", "-c", "debconf-set-selections <<< \"mysql-server mysql-server/lowercase-table-names select Enabled\""]