PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / currency / helper / cacheable.php
vikappointments / site / helpers / libraries / currency / helper Last commit date
cacheable.php 6 days ago enum.php 6 days ago
cacheable.php
77 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Currency helper cacheable trait.
16 *
17 * @since 1.7.6
18 */
19 trait VAPCurrencyHelperCacheable
20 {
21 /**
22 * Helper method used to internally cache a resource.
23 * The buffer will be always cached within the following folder:
24 * .../site/assets/cache/currency/
25 *
26 * @param string $name The name of the file to cache.
27 * @param int $lifetime How long the cache file is considered valid (in seconds).
28 * @param callable $proxy The callback to invoke when the cache cannot be pulled.
29 *
30 * @return string The cached buffer.
31 */
32 public function getCached(string $name, int $lifetime, $proxy)
33 {
34 // set up base path for cached files
35 $path = VAPBASE . '/assets/cache/currency/';
36
37 try
38 {
39 // make sure the folder exists, otherwise attempt to create it
40 if (!JFolder::exists($path) && !JFolder::create($path))
41 {
42 // cache folder must be created manually...
43 throw new RuntimeException('Unable to create the cache folder: ' . $path);
44 }
45
46 if (!JFile::exists($path . '/' . $name))
47 {
48 // response not cached yet
49 throw new RuntimeException('The cached file does not exist');
50 }
51
52 // fetch last-modify timestamp
53 $lastmodify = filemtime(JPath::clean($path . '/' . $name));
54
55 if (time() - $lastmodify > $lifetime)
56 {
57 // the cache file is expired
58 throw new RuntimeException('Cache file expired');
59 }
60
61 // read the contents from the cached file
62 return file_get_contents(JPath::clean($path . '/' . $name));
63 }
64 catch (Exception $error)
65 {
66 // cache not found or expired, execute callback
67 $response = call_user_func($proxy);
68
69 // internally cache the response (do not care about the result)
70 JFile::write($path . '/' . $name, (string) $response);
71
72 // return the response to the caller
73 return $response;
74 }
75 }
76 }
77