Skip to content

Commit

Permalink
Merge pull request #132 from wrav/feature/caching-issue
Browse files Browse the repository at this point in the history
Added default caching keys to resolve missing field data while cached
  • Loading branch information
reganlawton authored Feb 2, 2023
2 parents e5def20 + 5aa34ab commit b696728
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# oEmbed Changelog

## 2.3.0 - 2023-02-02

### Update
- Added default caching keys to resolve missing field data while cached

## 2.2.2 - 2022-08-26

### Update
Expand Down
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ To use simply call one of the following methods on your field type

We also provide option to use as a Twig variable

{{ craft.oembed.valid(url, options) }}
{{ craft.oembed.render(url, options) }}
{% set embed = craft.oembed.embed(url, options) %}
{% set media = craft.oembed.media(url, options) %}
{{ craft.oembed.valid(url, options, cacheFields) }}
{{ craft.oembed.render(url, options, cacheFields) }}
{% set embed = craft.oembed.embed(url, options, cacheFields) %}
{% set media = craft.oembed.media(url, options, cacheFields) %}

Updating the embed URL, such as autoplay, rel, mute paramaters. This allows for you to support features the provider might not yet support

Expand Down Expand Up @@ -127,6 +127,48 @@ You can access additional media details using the examples below.

Additional Embed information can be found [here](https://github.com/oscarotero/Embed)

## Cache

By default, the plugin will cache the following keys on the oembed object. The plugin can cache additional missing fields using the cache prop parameter which will take an array of strings.

{{
entry.oembed_field.render(
{
width: 640,
height: 480,
},
[
'cacheable_key'
]
)
}}

### Default Keys

- title
- description
- url
- type
- tags
- images
- image
- imageWidth
- imageHeight
- code
- width
- height
- aspectRatio
- authorName
- authorUrl
- providerName
- providerUrl
- providerIcons
- providerIcon
- publishedDate
- license
- linkedData
- feeds

## GraphQL

I recommend enabling caching in the plugin settings menu to speed up the API resolve timing.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "wrav/oembed",
"description": "A simple plugin to extract media information from websites, like youtube videos, twitter statuses or blog articles.",
"type": "craft-plugin",
"version": "2.2.2",
"version": "2.3.0",
"keywords": [
"craft",
"cms",
Expand Down
16 changes: 8 additions & 8 deletions src/models/OembedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ public function getUrl()
* @param array $options
* @return string
*/
public function render(array $options = [])
public function render(array $options = [], array $cacheProps = [])
{
if (empty($this->getUrl())) {
return null;
}

return Oembed::getInstance()->oembedService->render($this->getUrl(), $options);
return Oembed::getInstance()->oembedService->render($this->getUrl(), $options, $cacheProps);
}

/**
Expand All @@ -147,38 +147,38 @@ public function render(array $options = [])
* @param array $options
* @return string
*/
public function embed(array $options = [])
public function embed(array $options = [], array $cacheProps = [])
{
if (empty($this->getUrl())) {
return null;
}

return Oembed::getInstance()->oembedService->embed($this->getUrl(), $options);
return Oembed::getInstance()->oembedService->embed($this->getUrl(), $options, $cacheProps);
}

/**
* @param array $options
* @return string
*/
public function media(array $options = [])
public function media(array $options = [], array $cacheProps = [])
{
if (empty($this->getUrl())) {
return null;
}

return $this->embed($options);
return $this->embed($options, $cacheProps);
}

/**
* @return boolean
*/
public function valid(array $options = [])
public function valid(array $options = [], array $cacheProps = [])
{
if (empty($this->getUrl())) {
return false;
}

$media = $this->embed($options);
$media = $this->embed($options, $cacheProps);
return (!empty($media) && isset($media->code));
}
}
44 changes: 40 additions & 4 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class OembedService extends Component
* @param array $options
* @return string
*/
public function render($url, array $options = [])
public function render($url, array $options = [], array $cacheProps = [])
{
/** @var Media $media */
$media = $this->embed($url, $options);
$media = $this->embed($url, $options, $cacheProps);

if (!empty($media)) {
return Template::raw(isset($media->code) ? $media->code : '');
Expand All @@ -56,10 +56,10 @@ public function render($url, array $options = [])
* @param array $options
* @return Media|string
*/
public function embed($url, array $options = [])
public function embed($url, array $options = [], array $cacheProps = [])
{
try {
$hash = md5(json_encode($options));
$hash = md5(json_encode($options)).md5(json_encode($cacheProps));
} catch (\Exception $exception) {
$hash = '';
}
Expand Down Expand Up @@ -167,6 +167,42 @@ public function embed($url, array $options = [])
// Cache failed requests only for 15 minutes
$duration = $media instanceof FallbackAdapter ? 15 * 60 : 60 * 60;

$defaultCacheKeys = [
'title',
'description',
'url',
'type',
'tags',
'images',
'image',
'imageWidth',
'imageHeight',
'code',
'width',
'height',
'aspectRatio',
'authorName',
'authorUrl',
'providerName',
'providerUrl',
'providerIcons',
'providerIcon',
'publishedDate',
'license',
'linkedData',
'feeds',
];

$cacheKeys = array_unique(array_merge($defaultCacheKeys, $cacheProps));

foreach ($cacheKeys as $key) {
try {
$media->{$key} = $media->{$key};
} catch (\Exception $e) {
$media->{$key} = null;
}
}

Craft::$app->cache->set($cacheKey, json_decode(json_encode($media)), $duration);
}
return $media;
Expand Down
12 changes: 6 additions & 6 deletions src/variables/OembedVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class OembedVariable
* @param array $options
* @return string
*/
public function render($url, array $options = [])
public function render($url, array $options = [], array $cacheProps = [])
{
if (empty($url)) {
return null;
}

return Oembed::getInstance()->oembedService->render($url, $options);
return Oembed::getInstance()->oembedService->render($url, $options, $cacheProps);
}

/**
Expand All @@ -48,13 +48,13 @@ public function render($url, array $options = [])
* @param array $options
* @return string
*/
public function embed($url, array $options = [])
public function embed($url, array $options = [], array $cacheProps = [])
{
if (empty($url)) {
return null;
}

return Oembed::getInstance()->oembedService->embed($url, $options);
return Oembed::getInstance()->oembedService->embed($url, $options, $cacheProps);
}

/**
Expand All @@ -66,13 +66,13 @@ public function embed($url, array $options = [])
* @param array $options
* @return bool
*/
public function valid($url, array $options = [])
public function valid($url, array $options = [], array $cacheProps = [])
{
if (empty($url)) {
return false;
}

$media = $this->embed($url, $options);
$media = $this->embed($url, $options, $cacheProps);
return (!empty($media) && isset($media->code));
}
}

0 comments on commit b696728

Please sign in to comment.