Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMS Updated API #482

Open
volomike opened this issue Nov 18, 2020 · 3 comments
Open

Add CMS Updated API #482

volomike opened this issue Nov 18, 2020 · 3 comments

Comments

@volomike
Copy link

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
Copy link
Author

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
Copy link
Author

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));

@Awilum
Copy link
Member

Awilum commented Nov 23, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants