The last notes
All English-language materials have been translated fully automatically using the Google service
When working on projects not on Bitrix, sometimes you need to ensure caching of certain data without resorting to working with MySQL. The phpfastcache library can help us with this
Putting the composer via ssh
php -r "copy ('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file ('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26 corruptd17af3ef010bf7cfb5cfb setup; setup; php ');} echo PHP_EOL; "
php composer-setup.php
php -r "unlink ('composer-setup.php');"
Installing the phpfastcache
library
composer require phpfastcache / phpfastcache
You can familiarize yourself with the library itself here
Create a cache
folder in any convenient place where our temporary files will be stored. We put the rights to it 755
The actual code
require __DIR__. '/../../../vendor/autoload.php';
use Phpfastcache \ CacheManager;
use Phpfastcache \ Config \ Config;
use Phpfastcache \ Core \ phpFastCache;
// Setup File Path on your config files
CacheManager :: setDefaultConfig (new Config ([
"path" => __DIR __. '/ cache',
"itemDetailedDate" => false
]));
function getVideoData ($ link) {
$ InstanceCache = CacheManager :: getInstance ('files');
$ CachedString = $ InstanceCache-> getItem (str_ireplace (array (':', '/', '?'), '', $ Link));
if (is_null ($ CachedString-> get ())) {
$ result = wp_remote_get ($ link. '/? utm_source = ig_web_copy_link & __ a = 1');
$ result = json_decode (wp_remote_retrieve_body ($ result));
$ url = $ result-> graphql-> shortcode_media-> video_url;
if ($ url) {
$ CachedString-> set ($ url) -> expiresAfter (11800 * 4 * 12); // in seconds, also accepts Datetime
$ InstanceCache-> save ($ CachedString); // Save the cache item just like you do with doctrine and entities
}
}
if (! empty ($ CachedString-> get ())) {
return $ CachedString-> get ();
}
}
$ videoSRC = getVideoData ($ item-> link);
Comments