Skip to content

Commit

Permalink
added: a cache layer to the bynder image action
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijzer committed Sep 19, 2023
1 parent 54d8c9c commit 222e31a
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 20 deletions.
56 changes: 36 additions & 20 deletions src/Component/Action/GetImageFromBynderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Misery\Component\Action;

use Misery\Component\Common\Cache\CacheSettings;
use Misery\Component\Common\Cache\Local\LocalFilesystemCache;
use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use function _PHPStan_76800bfb5\regex;

class GetImageFromBynderAction implements OptionsInterface
{
Expand All @@ -19,29 +20,44 @@ class GetImageFromBynderAction implements OptionsInterface
'bynder_cookieid' => 'xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx',
'array_location' => [ 'original' ],
];
private ?LocalFilesystemCache $cachePool;

public function apply(array $item): array
public function init(): void
{
$fields = $this->options['fields'];
$bynder_url = $this->options['bynder_url'];
$bynder_token = $this->options['bynder_token'];
$bynder_cookieid = $this->options['bynder_cookieid'];
$array_location = $this->options['array_location'];

$bynder = [
'url' => $bynder_url,
'token' => $bynder_token,
'cookieid' => $bynder_cookieid,
];

// validation
if (!isset($fields) || $fields === []) {
return $item;
if (null === $this->cachePool) {
$this->cachePool = new LocalFilesystemCache(
sys_get_temp_dir() .
DIRECTORY_SEPARATOR .
'bynder_cache' .
DIRECTORY_SEPARATOR .
md5($this->getOption('bynder_url'))
);
}
}

$item = $this->sendRequest($bynder, $item, $fields, $array_location);

return $item;
public function apply(array $item): array
{
$this->init();

// Generate a unique cache key based on your data
$cacheKey = md5(json_encode($item)); // You can modify this based on your data structure

return $this->cachePool->retrieve($cacheKey, function (CacheSettings $settings) use ($item) {
$settings->setTtl(259200); # 3 day
$fields = $this->options['fields'];
$bynder_url = $this->options['bynder_url'];
$bynder_token = $this->options['bynder_token'];
$bynder_cookieid = $this->options['bynder_cookieid'];
$array_location = $this->options['array_location'];

$bynder = [
'url' => $bynder_url,
'token' => $bynder_token,
'cookieid' => $bynder_cookieid,
];

return $this->sendRequest($bynder, $item, $fields, $array_location);
});
}

public function sendRequest(array $bynder, array $item, array $fields, array $array_location)
Expand Down
24 changes: 24 additions & 0 deletions src/Component/Common/Cache/CacheSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Misery\Component\Common\Cache;

class CacheSettings
{
private int $ttl = 3600;

/**
* @param int $ttl
*/
public function setTtl(int $ttl): void
{
$this->ttl = $ttl;
}

/**
* @return int
*/
public function getTtl(): int
{
return $this->ttl;
}
}
145 changes: 145 additions & 0 deletions src/Component/Common/Cache/Local/LocalFilesystemCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Misery\Component\Common\Cache\Local;

use Misery\Component\Common\Cache\CacheSettings;
use Misery\Component\Common\Cache\SimpleCacheInterface;

class LocalFilesystemCache implements SimpleCacheInterface
{
private $cacheDirectory;

public function __construct(string $cacheDirectory)
{
$this->cacheDirectory = $cacheDirectory;
if (!is_dir($this->cacheDirectory)) {
mkdir($this->cacheDirectory, 0777, true);
}
}

public function retrieve($key, callable $process)
{
$cachedData = $this->get($key);
if ($cachedData !== null) {
return $cachedData;
}

$item = $process($setting = new CacheSettings());

// Cache the API response
$this->set($key, $item, $setting->getTtl());

return $item;
}

public function get($key, $default = null)
{
$filename = $this->getCacheFilename($key);

if (!file_exists($filename)) {
return $default;
}

$data = file_get_contents($filename);
$cachedItem = unserialize($data);

if ($cachedItem['ttl'] !== null && $cachedItem['ttl'] < time()) {
$this->delete($key);
return $default;
}

return $cachedItem['value'];
}

public function set($key, $value, $ttl = null)
{
$filename = $this->getCacheFilename($key);
$cachedItem = [
'value' => $value,
'ttl' => $ttl !== null ? time() + $ttl : null,
];

$data = serialize($cachedItem);
return file_put_contents($filename, $data) !== false;
}

public function delete($key)
{
$filename = $this->getCacheFilename($key);

if (file_exists($filename)) {
return unlink($filename);
}

return false;
}

public function clear()
{
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->cacheDirectory, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($iterator as $path) {
if ($path->isDir()) {
rmdir($path->getPathname());
} else {
unlink($path->getPathname());
}
}

return true;
}

public function getMultiple($keys, $default = null)
{
$values = [];
foreach ($keys as $key) {
$values[$key] = $this->get($key, $default);
}
return $values;
}

public function setMultiple($values, $ttl = null)
{
$success = true;
foreach ($values as $key => $value) {
$success = $success && $this->set($key, $value, $ttl);
}
return $success;
}

public function deleteMultiple($keys)
{
$success = true;
foreach ($keys as $key) {
$success = $success && $this->delete($key);
}
return $success;
}

public function has($key)
{
$filename = $this->getCacheFilename($key);

if (!file_exists($filename)) {
return false;
}

$cachedItem = unserialize(file_get_contents($filename));

if ($cachedItem['ttl'] !== null && $cachedItem['ttl'] < time()) {
$this->delete($key);
return false;
}

return true;
}

private function getCacheFilename($key)
{
$key = preg_replace('/[^a-z0-9_\-]/i', '', $key); // Sanitize key
return $this->cacheDirectory . DIRECTORY_SEPARATOR . $key;
}
}
Loading

0 comments on commit 222e31a

Please sign in to comment.