Skip to content

Commit

Permalink
Merge pull request #297 from christianlupus/dev/rework-db
Browse files Browse the repository at this point in the history
Rework the database requests for performance
  • Loading branch information
christianlupus authored Oct 6, 2020
2 parents f4fc213 + e2983e5 commit 2c747f9
Show file tree
Hide file tree
Showing 7 changed files with 770 additions and 186 deletions.
19 changes: 15 additions & 4 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use OCA\Cookbook\Service\RecipeService;
use OCP\IURLGenerator;
use OCA\Cookbook\Service\DbCacheService;

class ConfigController extends Controller
{
Expand All @@ -25,23 +26,31 @@ class ConfigController extends Controller
* @var IURLGenerator
*/
private $urlGenerator;

/**
* @var DbCacheService
*/
private $dbCacheService;

public function __construct($AppName, IRequest $request, IURLGenerator $urlGenerator, RecipeService $recipeService)
public function __construct($AppName, IRequest $request, IURLGenerator $urlGenerator, RecipeService $recipeService, DbCacheService $dbCacheService)
{
parent::__construct($AppName, $request);

$this->service = $recipeService;
$this->urlGenerator = $urlGenerator;
$this->dbCacheService = $dbCacheService;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function list() {
$this->dbCacheService->triggerCheck();

return new DataResponse([
'folder' => $this->service->getUserFolderPath(),
'update_interval' => $this->service->getSearchIndexUpdateInterval(),
'update_interval' => $this->dbCacheService->getSearchIndexUpdateInterval(),
'print_image' => $this->service->getPrintImage(),
], Http::STATUS_OK);
}
Expand All @@ -52,9 +61,11 @@ public function list() {
*/
public function config()
{
$this->dbCacheService->triggerCheck();

if (isset($_POST['folder'])) {
$this->service->setUserFolderPath($_POST['folder']);
$this->service->rebuildSearchIndex();
$this->dbCacheService->updateCache();
}

if (isset($_POST['update_interval'])) {
Expand All @@ -74,7 +85,7 @@ public function config()
*/
public function reindex()
{
$this->service->rebuildSearchIndex();
$this->dbCacheService->updateCache();

return new DataResponse('Search index rebuilt successfully', Http::STATUS_OK);
}
Expand Down
51 changes: 46 additions & 5 deletions lib/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,33 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\Cookbook\Service\RecipeService;
use OCA\Cookbook\Service\DbCacheService;

class MainController extends Controller
{
protected $appName;

/**
* @var RecipeService
*/
private $service;
/**
* @var DbCacheService
*/
private $dbCacheService;
/**
* @var IURLGenerator
*/
private $urlGenerator;

public function __construct(string $AppName, IRequest $request, RecipeService $recipeService, IURLGenerator $urlGenerator)
public function __construct(string $AppName, IRequest $request, RecipeService $recipeService, DbCacheService $dbCacheService, IURLGenerator $urlGenerator)
{
parent::__construct($AppName, $request);

$this->service = $recipeService;
$this->urlGenerator = $urlGenerator;
$this->appName = $AppName;
$this->dbCacheService = $dbCacheService;
}

/**
Expand All @@ -36,11 +48,13 @@ public function __construct(string $AppName, IRequest $request, RecipeService $r
*/
public function index(): TemplateResponse
{
$this->dbCacheService->triggerCheck();

$view_data = [
'all_keywords' => $this->service->getAllKeywordsInSearchIndex(),
'folder' => $this->service->getUserFolderPath(),
'update_interval' => $this->service->getSearchIndexUpdateInterval(),
'last_update' => $this->service->getSearchIndexLastUpdateTime(),
'update_interval' => $this->dbCacheService->getSearchIndexUpdateInterval(),
'last_update' => $this->dbCacheService->getSearchIndexLastUpdateTime(),
'print_image' => $this->service->getPrintImage(),
];

Expand All @@ -53,6 +67,8 @@ public function index(): TemplateResponse
*/
public function categories()
{
$this->dbCacheService->triggerCheck();

$categories = $this->service->getAllCategoriesInSearchIndex();
return new DataResponse($categories, 200, ['Content-Type' => 'application/json']);
}
Expand All @@ -63,6 +79,8 @@ public function categories()
*/
public function keywords()
{
$this->dbCacheService->triggerCheck();

$keywords = $this->service->getAllKeywordsInSearchIndex();
return new DataResponse($keywords, 200, ['Content-Type' => 'application/json']);
}
Expand All @@ -73,6 +91,8 @@ public function keywords()
*/
public function home()
{
$this->dbCacheService->triggerCheck();

try {
$recipes = $this->service->getAllRecipesInSearchIndex();

Expand Down Expand Up @@ -102,6 +122,8 @@ public function home()
*/
public function error()
{
$this->dbCacheService->triggerCheck();

$response = new TemplateResponse($this->appName, 'navigation/error');
$response->renderAs('blank');

Expand All @@ -114,7 +136,9 @@ public function error()
*/
public function search($query)
{
$query = urldecode($query);
$this->dbCacheService->triggerCheck();

$query = urldecode($query);
try {
$recipes = $this->service->findRecipesInSearchIndex($query);

Expand Down Expand Up @@ -144,6 +168,8 @@ public function search($query)
*/
public function category($category)
{
$this->dbCacheService->triggerCheck();

$category = urldecode($category);
try {
$recipes = $this->service->getRecipesByCategory($category);
Expand All @@ -170,6 +196,8 @@ public function category($category)
*/
public function recipe($id)
{
$this->dbCacheService->triggerCheck();

try {
$recipe = $this->service->getRecipeById($id);
$recipe['image_url'] = $this->urlGenerator->linkToRoute(
Expand Down Expand Up @@ -197,6 +225,8 @@ public function recipe($id)
*/
public function create()
{
$this->dbCacheService->triggerCheck();

try {
$recipe = [];

Expand All @@ -215,13 +245,16 @@ public function create()
*/
public function import()
{
$this->dbCacheService->triggerCheck();

if (!isset($_POST['url'])) {
return new DataResponse('Field "url" is required', 400);
}

try {
$recipe_file = $this->service->downloadRecipe($_POST['url']);
$recipe_json = $this->service->parseRecipeFile($recipe_file);
$this->dbCacheService->addRecipe($recipe_file);

return new DataResponse($recipe_json, Http::STATUS_OK, ['Content-Type' => 'application/json']);
} catch (\Exception $e) {
Expand All @@ -235,9 +268,12 @@ public function import()
*/
public function new()
{
try {
$this->dbCacheService->triggerCheck();

try {
$recipe_data = $_POST;
$file = $this->service->addRecipe($recipe_data);
$this->dbCacheService->addRecipe($file);

return new DataResponse($file->getParent()->getId());
} catch (\Exception $e) {
Expand All @@ -251,6 +287,8 @@ public function new()
*/
public function edit($id)
{
$this->dbCacheService->triggerCheck();

try {
$recipe = [];

Expand All @@ -277,6 +315,8 @@ public function edit($id)
*/
public function update($id)
{
$this->dbCacheService->triggerCheck();

try {
$recipe_data = [];

Expand All @@ -285,6 +325,7 @@ public function update($id)
$recipe_data['id'] = $id;

$file = $this->service->addRecipe($recipe_data);
$this->dbCacheService->addRecipe($file);

return new DataResponse($id);

Expand Down
23 changes: 22 additions & 1 deletion lib/Controller/RecipeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use OCA\Cookbook\Service\RecipeService;
use OCP\IURLGenerator;
use OCA\Cookbook\Service\DbCacheService;

class RecipeController extends Controller
{
Expand All @@ -25,13 +26,19 @@ class RecipeController extends Controller
* @var IURLGenerator
*/
private $urlGenerator;

/**
* @var DbCacheService
*/
private $dbCacheService;

public function __construct($AppName, IRequest $request, IURLGenerator $urlGenerator, RecipeService $recipeService)
public function __construct($AppName, IRequest $request, IURLGenerator $urlGenerator, RecipeService $recipeService, DbCacheService $dbCacheService)
{
parent::__construct($AppName, $request);

$this->service = $recipeService;
$this->urlGenerator = $urlGenerator;
$this->dbCacheService = $dbCacheService;
}

/**
Expand All @@ -40,6 +47,8 @@ public function __construct($AppName, IRequest $request, IURLGenerator $urlGener
*/
public function index()
{
$this->dbCacheService->triggerCheck();

if (empty($_GET['keywords'])) {
$recipes = $this->service->getAllRecipesInSearchIndex();
} else {
Expand All @@ -59,6 +68,8 @@ public function index()
*/
public function show($id)
{
$this->dbCacheService->triggerCheck();

$json = $this->service->getRecipeById($id);

if (null === $json) {
Expand All @@ -83,9 +94,12 @@ public function show($id)
*/
public function update($id)
{
$this->dbCacheService->triggerCheck();

$recipeData = [];
parse_str(file_get_contents("php://input"), $recipeData);
$file = $this->service->addRecipe($recipeData);
$this->dbCacheService->addRecipe($file);

return new DataResponse($file->getParent()->getId(), Http::STATUS_OK, ['Content-Type' => 'application/json']);
}
Expand All @@ -102,8 +116,11 @@ public function update($id)
*/
public function create()
{
$this->dbCacheService->triggerCheck();

$recipeData = $_POST;
$file = $this->service->addRecipe($recipeData);
$this->dbCacheService->addRecipe($file);

return new DataResponse($file->getParent()->getId(), Http::STATUS_OK, ['Content-Type' => 'application/json']);
}
Expand All @@ -116,6 +133,8 @@ public function create()
*/
public function destroy($id)
{
$this->dbCacheService->triggerCheck();

try {
$this->service->deleteRecipe($id);
return new DataResponse('Recipe ' . $_GET['id'] . ' deleted successfully', Http::STATUS_OK);
Expand All @@ -132,6 +151,8 @@ public function destroy($id)
*/
public function image($id)
{
$this->dbCacheService->triggerCheck();

$size = isset($_GET['size']) ? $_GET['size'] : null;

try {
Expand Down
Loading

0 comments on commit 2c747f9

Please sign in to comment.