flextype icon indicating copy to clipboard operation
flextype copied to clipboard

Add CMS Updated API

Open volomike opened this issue 4 years ago • 3 comments

Most people using a headless CMS will want to connect with the FlexType API and then handle caching on the website, rather than the admin system. Imagine if you will the admin system is on a subdomain, while the main website is on the main domain. In building that cache code for the website, one needs to know when that cache is invalid and needs to be retrieved again. It would be great if, every time some Entry is added, changed, or removed, no matter the entry, it stores a "cms-last-modified" timestamp value somewhere, and then make that available on the API. This API should respond as fast as possible and then disconnect. So, when someone loads a website, it does this API call to the admin system to ask, "Is my cache invalid and I need to re-download everything again?" If the cache is still valid on the website side, then it could pull from cache instead of pulling from the CMS system. And, you could build a failsafe where the website will mark the cache invalid on either an interval like once a day or once a week.

What I'm having to do now, without this solution, is to just make live calls to the CMS every time the website loads, which can make a site slow.

volomike avatar Nov 18 '20 02:11 volomike

Is there an API call, SQLite query, or file folder check that I can do to determine if the CMS has been updated? That way, I can do a quick check with that (building my own wrapper if necessary) on the loading of a website, rather than pulling from the CMS API each time for all content.

volomike avatar Nov 22 '20 07:11 volomike

Okay, I get it. It's yaml. I thought this content was being stored in SQLite. However, it's not. Okay, so basically, projects/entries and projects/uploads/.meta would be modified if the content changed. I can use the following code below to detect changes there. So, the client (the website) that connects to a changed.php (which one can store in the root of this system) can run the following script. If the hash they stored last does not match what is returned from changed.php, then it can say the cache is stale and can refresh the cache:

<?php

function get_m_time_dir($path)
{
  $directory = new RecursiveDirectoryIterator(
    $path,
    FilesystemIterator::KEY_AS_PATHNAME | 
    FilesystemIterator::CURRENT_AS_FILEINFO | 
    FilesystemIterator::SKIP_DOTS
  );
  $iterator = new RecursiveIteratorIterator(
    $directory,
    RecursiveIteratorIterator::SELF_FIRST 
  );
  $resultFile = $iterator->current();
  foreach($iterator as $file) {
    if ($file->getMtime() > $resultFile->getMtime()) {
      $resultFile = $file;
    }
  }
  return $resultFile->getMtime();
}

$sEntriesCode = get_m_time_dir('project/entries');
$sUploadsCode = get_m_time_dir('project/uploads/.meta');

$sHash = md5($sEntriesCode . $sUploadsCode);

$a = (object) array(
  'last_modified_hash' => $sHash
);
die(json_encode($a));

volomike avatar Nov 22 '20 08:11 volomike

@volomike I think there should be some kind of Client SDK (php,js,python and etc...) that will fetch data from API and will store it in the locale cache for some time

Awilum avatar Nov 23 '20 12:11 Awilum