Skip to content

Instantly share code, notes, and snippets.

@valkirilov
Last active June 4, 2020 11:23
Show Gist options
  • Save valkirilov/a5a7f3d32e7cbf3d19939a3bdd3b23f3 to your computer and use it in GitHub Desktop.
Save valkirilov/a5a7f3d32e7cbf3d19939a3bdd3b23f3 to your computer and use it in GitHub Desktop.
A Laravel cache wrapper of the Prismic API PHP client.
<?php
namespace App\Services;
use Illuminate\Support\Facades\Cache;
use Prismic\Api;
class PrismicApiService
{
/**
* The Prismic API instance used to communicate the API
*/
protected $api;
/**
* Establish a new connection to the Prismic API
* Skip if there is already initialized connection
*/
private function init(): void
{
if (is_null($this->api)) {
$this->api = Api::get(config('app.prismic'));
}
}
/**
* Generate a cache key according to the provided predicates
*
* @param string|array|\Prismic\Predicate $predicates the query, as a string, predicate or array of predicates
* @param array $options query options: pageSize, orderings, etc.
* @return string
*/
private function getCacheKey($predicates, array $options = []): string
{
$cacheKey = 'prismic:';
if (!is_array($predicates)) {
$predicates = [$predicates];
}
foreach($predicates as $predicate) {
$cacheKey .= $predicate->name . ':' . $predicate->fragment . ':' . implode(',', $predicate->args) . ';';
}
foreach($options as $key => $option) {
$cacheKey .= $key . ':' . $option . ';';
}
return $cacheKey;
}
/**
* A wrapper function of the standard Prismic::query method
*
* @param string|array|\Prismic\Predicate $predicates the query, as a string, predicate or array of predicates
* @param array $options query options: pageSize, orderings, etc.
*
* @return Prismic::Response the response, including documents and pagination information
*/
public function query($predicates, array $options = [])
{
$cacheKey = $this->getCacheKey($predicates, $options);
if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
$this->init();
$query = Cache::rememberForever($cacheKey, function () use ($predicates, $options) {
return $this->api->query($predicates, $options);
});
return $query;
}
/**
* A wrapper function of the standard Prismic::getResults method
* Uses the cached version of the query method
*
* @param string|array|\Prismic\Predicate $predicates the query, as a string, predicate or array of predicates
* @param array $options query options: pageSize, orderings, etc.
*
* @return array the list of returned documents
*/
public function getResults($predicates, array $options = [])
{
$cacheKey = $this->getCacheKey($predicates, $options);
if (Cache::has($cacheKey)) {
$query = Cache::get($cacheKey);
return $query->getResults();
}
$this->init();
$query = $this->query($predicates, $options);
return $query->getResults();
}
/**
* A wrapper function of the standard Prismic::getSingle method
*
* @param string $type the custom type of the requested document
* @param array $options query options: pageSize, orderings, etc.
* @return Prismic::Document the resulting document (null if no match)
*/
public function getSingle($predicates, array $options = [])
{
$cacheKey = 'prismic:' . $predicates;
if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
$this->init();
$single = Cache::rememberForever($cacheKey, function () use ($predicates, $options) {
return $this->api->getSingle($predicates, $options);
});
return $single;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment