Skip to content

Commit

Permalink
Implemented a caching mechanism for getting the lastmod of a translator
Browse files Browse the repository at this point in the history
  • Loading branch information
LionelLaffineur committed Sep 29, 2023
1 parent aec37e0 commit adc1318
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
22 changes: 22 additions & 0 deletions lib/Skeleton/I18n/Translator/Storage/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Config class
* Configuration for Skeleton\I18n\Translator\Storage
*
* @author Lionel Laffineur <[email protected]>
*/

namespace Skeleton\I18n\Translator\Storage;

class Config {

/**
* Tmp path
*
* This folder will be used to create a cache for resized pictures
*
* @access public
* @var string $tmp_path
*/
public static $tmp_path = '/tmp';
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,33 @@ public static function get_by_source_language(Source $source, \Skeleton\I18n\Lan
return $target;
}

/**
* get last modified
*
* @access public
* @param string $translator_name
* @return DateTime
*/
public static function get_last_modified($translator_name) {
$db = Database::get();
$updated = $db->get_one('
SELECT ifnull(translation_target.updated, translation_target.created) as last_modified
FROM translation_source, translation_target
WHERE translation_target.translation_source_id=translation_source.id and name= ?
ORDER BY last_modified DESC LIMIT 1', [ $translator_name ]);

if (file_exists(\Skeleton\I18n\Translator\Storage\Config::$tmp_path) === false) {
mkdir(\Skeleton\I18n\Translator\Storage\Config::$tmp_path, 0755, true);
}

if (file_exists(\Skeleton\I18n\Translator\Storage\Config::$tmp_path . '/' . $translator_name) && filemtime(\Skeleton\I18n\Translator\Storage\Config::$tmp_path . '/' . $translator_name) + 60 > time()) {
return new \DateTime(file_get_contents(\Skeleton\I18n\Translator\Storage\Config::$tmp_path . '/' . $translator_name));
}

$updated = $db->get_one(' SELECT ifnull(translation_target.updated, translation_target.created) as last_modified
FROM translation_source, translation_target
WHERE translation_target.translation_source_id=translation_source.id and name= ?
ORDER BY last_modified DESC LIMIT 1', [ $translator_name ]);
if ($updated === null) {
return null;
}

file_put_contents(\Skeleton\I18n\Translator\Storage\Config::$tmp_path . '/' . $translator_name, $updated);
return new \DateTime($updated);
}
}

0 comments on commit adc1318

Please sign in to comment.