Skip to content

FOUR-20911: Caching Screens #7787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions ProcessMaker/Cache/Screens/LegacyScreenCacheAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace ProcessMaker\Cache\Screens;

use ProcessMaker\Managers\ScreenCompiledManager;

class LegacyScreenCacheAdapter implements ScreenCacheInterface
{
protected ScreenCompiledManager $compiledManager;

public function __construct(ScreenCompiledManager $compiledManager)
{
$this->compiledManager = $compiledManager;
}

/**
* Create a cache key for a screen
*/
public function createKey(int $processId, int $processVersionId, string $language, int $screenId, int $screenVersionId): string
{
return $this->compiledManager->createKey(
(string) $processId,
(string) $processVersionId,
$language,
(string) $screenId,
(string) $screenVersionId
);
}

/**
* Get a screen from cache
*/
public function get(string $key, mixed $default = null): mixed
{
$content = $this->compiledManager->getCompiledContent($key);

return $content ?? $default;
}

/**
* Store a screen in cache
*/
public function set(string $key, mixed $value): bool
{
$this->compiledManager->storeCompiledContent($key, $value);

return true;
}

/**
* Check if screen exists in cache
*/
public function has(string $key): bool
{
return $this->compiledManager->getCompiledContent($key) !== null;
}
}
18 changes: 18 additions & 0 deletions ProcessMaker/Cache/Screens/ScreenCacheFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ProcessMaker\Cache\Screens;

use Illuminate\Support\Facades\Facade;

/**
* Class ScreenCacheFacade
*
* @mixin \ProcessMaker\Cache\Settings\ScreenCacheManager
*/
class ScreenCacheFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'screen.cache';
}
}
45 changes: 45 additions & 0 deletions ProcessMaker/Cache/Screens/ScreenCacheFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace ProcessMaker\Cache\Screens;

use Illuminate\Support\Facades\Config;
use ProcessMaker\Cache\Screens\ScreenCacheManager;
use ProcessMaker\Managers\ScreenCompiledManager;

class ScreenCacheFactory
{
private static ?ScreenCacheInterface $testInstance = null;

/**
* Set a test instance for the factory
*
* @param ScreenCacheInterface|null $instance
*/
public static function setTestInstance(?ScreenCacheInterface $instance): void
{
self::$testInstance = $instance;
}

/**
* Create a screen cache handler based on configuration
*
* @return ScreenCacheInterface
*/
public static function create(): ScreenCacheInterface
{
if (self::$testInstance !== null) {
return self::$testInstance;
}

$manager = Config::get('screens.cache.manager', 'legacy');

if ($manager === 'new') {
return app(ScreenCacheManager::class);
}

// Get the concrete ScreenCompiledManager instance from the container
$compiledManager = app()->make(ScreenCompiledManager::class);

return new LegacyScreenCacheAdapter($compiledManager);
}
}
26 changes: 26 additions & 0 deletions ProcessMaker/Cache/Screens/ScreenCacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ProcessMaker\Cache\Screens;

interface ScreenCacheInterface
{
/**
* Create a cache key for a screen
*/
public function createKey(int $processId, int $processVersionId, string $language, int $screenId, int $screenVersionId): string;

/**
* Get a screen from cache
*/
public function get(string $key, mixed $default = null): mixed;

/**
* Store a screen in cache
*/
public function set(string $key, mixed $value): bool;

/**
* Check if screen exists in cache
*/
public function has(string $key): bool;
}
115 changes: 115 additions & 0 deletions ProcessMaker/Cache/Screens/ScreenCacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace ProcessMaker\Cache\Screens;

use Illuminate\Cache\CacheManager;
use ProcessMaker\Cache\CacheInterface;
use ProcessMaker\Managers\ScreenCompiledManager;

class ScreenCacheManager implements CacheInterface, ScreenCacheInterface
{
protected CacheManager $cacheManager;

protected ScreenCompiledManager $screenCompiler;

/**
* Default TTL for cached screens (24 hours)
*/
protected const DEFAULT_TTL = 86400;

public function __construct(CacheManager $cacheManager, ScreenCompiledManager $screenCompiler)
{
$this->cacheManager = $cacheManager;
$this->screenCompiler = $screenCompiler;
}

/**
* Create a cache key for a screen
*
* @param int $processId Process ID
* @param int $processVersionId Process version ID
* @param string $language Language code
* @param int $screenId Screen ID
* @param int $screenVersionId Screen version ID
* @return string Cache key
*/
public function createKey(int $processId, int $processVersionId, string $language, int $screenId, int $screenVersionId): string
{
return "pid_{$processId}_{$processVersionId}_{$language}_sid_{$screenId}_{$screenVersionId}";
}

/**
* Get a screen from cache
*
* @param string $key Screen cache key
* @param mixed $default Default value
* @return mixed
*/
public function get(string $key, mixed $default = null): mixed
{
$serializedContent = $this->cacheManager->get($key);
if ($serializedContent !== null) {
return unserialize($serializedContent);
}

return $default;
}

/**
* Store a screen in memory cache
*
* @param string $key Screen cache key
* @param mixed $value Compiled screen content
* @param null|int|\DateInterval $ttl Time to live
* @return bool
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
$serializedContent = serialize($value);

return $this->cacheManager->put($key, $serializedContent, $ttl ?? self::DEFAULT_TTL);
}

/**
* Delete a screen from cache
*
* @param string $key Screen cache key
* @return bool
*/
public function delete(string $key): bool
{
return $this->cacheManager->forget($key);
}

/**
* Clear all screen caches
*
* @return bool
*/
public function clear(): bool
{
return $this->cacheManager->flush();
}

/**
* Check if screen exists in cache
*
* @param string $key Screen cache key
* @return bool
*/
public function has(string $key): bool
{
return $this->cacheManager->has($key);
}

/**
* Check if screen is missing
*
* @param string $key Screen cache key
* @return bool
*/
public function missing(string $key): bool
{
return !$this->has($key);
}
}
36 changes: 24 additions & 12 deletions ProcessMaker/Http/Controllers/Api/V1_1/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use ProcessMaker\Facades\ScreenCompiledManager;
use ProcessMaker\Cache\Screens\ScreenCacheFactory;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Http\Resources\V1_1\TaskInterstitialResource;
use ProcessMaker\Http\Resources\V1_1\TaskResource;
Expand Down Expand Up @@ -102,31 +102,43 @@ public function showScreen($taskId)
$task = ProcessRequestToken::select(
array_merge($this->defaultFields, ['process_request_id', 'process_id'])
)
// eager loading process_request.process_version_id
->with([
'processRequest'=> function ($query) {
'processRequest' => function ($query) {
$query->select('id', 'process_version_id');
},
])->findOrFail($taskId);

// Prepare the key for the screen cache
// Get screen version and prepare cache key
$processId = $task->process_id;
$processVersionId = $task->processRequest->process_version_id;
$language = TranslationManager::getTargetLanguage();
$screenVersion = $task->getScreenVersion();
$key = ScreenCompiledManager::createKey($processId, $processVersionId, $language, $screenVersion->screen_id, $screenVersion->id);

// Get the screen content from the cache or compile it
$translatedScreen = ScreenCompiledManager::getCompiledContent($key);
if (!isset($translatedScreen)) {
// Get the appropriate cache handler based on configuration
$screenCache = ScreenCacheFactory::create();

// Create cache key
$key = $screenCache->createKey(
(int) $processId,
(int) $processVersionId,
$language,
(int) $screenVersion->screen_id,
(int) $screenVersion->id
);

// Try to get the screen from cache
$translatedScreen = $screenCache->get($key);

if ($translatedScreen === null) {
// If not in cache, create new response
$response = new TaskScreen($task);
$translatedScreen = $response->toArray(request())['screen'];
ScreenCompiledManager::storeCompiledContent($key, $translatedScreen);
}

$response = response($translatedScreen, 200);
// Store in cache
$screenCache->set($key, $translatedScreen);
}

return $response;
return response($translatedScreen, 200);
}

public function showInterstitial($taskId)
Expand Down
23 changes: 23 additions & 0 deletions config/screens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Screen Cache Configuration
|--------------------------------------------------------------------------
|
| Here you can configure the screen caching behavior.
|
*/

'cache' => [
// Cache manager to use: 'new' for ScreenCacheManager, 'legacy' for ScreenCompiledManager
'manager' => env('SCREEN_CACHE_MANAGER', 'legacy'),

// Cache driver to use (redis, file)
'driver' => env('SCREEN_CACHE_DRIVER', 'file'),

// Default TTL for cached screens (24 hours)
'ttl' => env('SCREEN_CACHE_TTL', 86400),
],
];
Loading
Loading