| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\Calendars; |
| 4 |
|
| 5 |
/* |
| 6 |
* Caching class for string remote response data or any type of high intensive calculated data |
| 7 |
* This call will use fcal_meta table with object_type value is: performance_cache |
| 8 |
* the updated_at column will be used to set when the cache will be expired. (it's wierd but it's the optimal solution) |
| 9 |
* The created_at column will be used when the last time the data has been updated |
| 10 |
* the parent id meant to be the ID of the another row of the fcal_meta table. |
| 11 |
*/ |
| 12 |
|
| 13 |
class CalendarCache |
| 14 |
{ |
| 15 |
private static $objectType = 'performance_cache'; |
| 16 |
|
| 17 |
public static function getCache($parentId, $key, $callback, $cacheTime = 600, $renew = false) // 10 minutes check |
| 18 |
{ |
| 19 |
$db = self::db(); |
| 20 |
|
| 21 |
$row = $db->table('fcal_meta') |
| 22 |
->where('object_type', self::$objectType) |
| 23 |
->where('key', $key) |
| 24 |
->where('object_id', $parentId) |
| 25 |
->first(); |
| 26 |
|
| 27 |
if ($row && $row->value !== '' && !$renew) { |
| 28 |
if (strtotime($row->updated_at) > time()) { |
| 29 |
return maybe_unserialize($row->value); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
$value = $callback(); |
| 34 |
|
| 35 |
if (is_wp_error($value)) { |
| 36 |
return null; |
| 37 |
} |
| 38 |
|
| 39 |
// In the mean time it may got called and create the row |
| 40 |
$row = $db->table('fcal_meta') |
| 41 |
->where('object_type', self::$objectType) |
| 42 |
->where('key', $key) |
| 43 |
->where('object_id', $parentId) |
| 44 |
->first(); |
| 45 |
|
| 46 |
if ($row) { |
| 47 |
if ($value === null) { // value got nulled so let's return the previous data |
| 48 |
return maybe_unserialize($row->value); |
| 49 |
} |
| 50 |
|
| 51 |
$db->table('fcal_meta') |
| 52 |
->where('id', $row->id) |
| 53 |
->update([ |
| 54 |
'updated_at' => gmdate('Y-m-d H:i:s', time() + $cacheTime), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 55 |
'created_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 56 |
'value' => maybe_serialize($value) |
| 57 |
]); |
| 58 |
} else { |
| 59 |
if ($value === null) { |
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
$db->table('fcal_meta') |
| 64 |
->insert([ |
| 65 |
'updated_at' => gmdate('Y-m-d H:i:s', time() + $cacheTime), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 66 |
'created_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 67 |
'object_type' => self::$objectType, |
| 68 |
'key' => $key, |
| 69 |
'object_id' => $parentId, |
| 70 |
'value' => maybe_serialize($value) |
| 71 |
]); |
| 72 |
} |
| 73 |
|
| 74 |
return $value; |
| 75 |
} |
| 76 |
|
| 77 |
public static function deleteCache($parentId, $key) |
| 78 |
{ |
| 79 |
$db = self::db(); |
| 80 |
$db->table('fcal_meta') |
| 81 |
->where('object_type', self::$objectType) |
| 82 |
->where('key', $key) |
| 83 |
->where('object_id', $parentId) |
| 84 |
->delete(); |
| 85 |
} |
| 86 |
|
| 87 |
public static function deleteAllParentCache($parentId) |
| 88 |
{ |
| 89 |
$db = self::db(); |
| 90 |
$db->table('fcal_meta') |
| 91 |
->where('object_type', self::$objectType) |
| 92 |
->where('object_id', $parentId) |
| 93 |
->delete(); |
| 94 |
} |
| 95 |
|
| 96 |
public static function deleteAllOldExpiredCache() |
| 97 |
{ |
| 98 |
$db = self::db(); |
| 99 |
$db->table('fcal_meta') |
| 100 |
->where('object_type', self::$objectType) |
| 101 |
->where('updated_at', '<', gmdate('Y-m-d H:i:s', strtotime('-15 days'))) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 102 |
->delete(); |
| 103 |
} |
| 104 |
|
| 105 |
public static function db() |
| 106 |
{ |
| 107 |
return (\FluentBooking\App\App::getInstance())->db; |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|