From 292358b9e57ca35539cd547747ac32a55dbaabdb Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Thu, 11 Jul 2019 18:29:48 +0200 Subject: [PATCH] add cache helper function in config.inc, needed for https://github.com/opnsense/core/issues/3567 get_cached_json_content() deserialize json data when the file is found and not yet expired, returns null otherwise. Since there might be some spots in the code where this can be convenient, it seemed like a good idea to wrap it in a function within the legacy code base. Maybe config.inc is not enterily the right spot for this, but util.inc is quite diverse already. --- src/etc/inc/config.inc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/etc/inc/config.inc b/src/etc/inc/config.inc index f7a298388..49cca531d 100644 --- a/src/etc/inc/config.inc +++ b/src/etc/inc/config.inc @@ -301,6 +301,22 @@ function legacy_config_get_interfaces($filters = array()) return $interfaces; } +/** + * parse stored json content, return empty when not found or expired + */ +function get_cached_json_content($filename, $ttl=3600) +{ + $result = null; + if (file_exists($filename)) { + $fstat = stat($filename); + if ((time() - $fstat['mtime']) < $ttl) { + $result = json_decode(file_get_contents($filename), true); + } + } + return $result; +} + + $config = parse_config(); /* set timezone */