table('fcal_meta') ->where('object_type', self::$objectType) ->where('key', $key) ->where('object_id', $parentId) ->first(); if ($row && $row->value !== '' && !$renew) { if (strtotime($row->updated_at) > time()) { return maybe_unserialize($row->value); } } $value = $callback(); if (is_wp_error($value)) { return null; } // In the mean time it may got called and create the row $row = $db->table('fcal_meta') ->where('object_type', self::$objectType) ->where('key', $key) ->where('object_id', $parentId) ->first(); if ($row) { if ($value === null) { // value got nulled so let's return the previous data return maybe_unserialize($row->value); } $db->table('fcal_meta') ->where('id', $row->id) ->update([ 'updated_at' => gmdate('Y-m-d H:i:s', time() + $cacheTime), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date 'created_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date 'value' => maybe_serialize($value) ]); } else { if ($value === null) { return null; } $db->table('fcal_meta') ->insert([ 'updated_at' => gmdate('Y-m-d H:i:s', time() + $cacheTime), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date 'created_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date 'object_type' => self::$objectType, 'key' => $key, 'object_id' => $parentId, 'value' => maybe_serialize($value) ]); } return $value; } public static function deleteCache($parentId, $key) { $db = self::db(); $db->table('fcal_meta') ->where('object_type', self::$objectType) ->where('key', $key) ->where('object_id', $parentId) ->delete(); } public static function deleteAllParentCache($parentId) { $db = self::db(); $db->table('fcal_meta') ->where('object_type', self::$objectType) ->where('object_id', $parentId) ->delete(); } public static function deleteAllOldExpiredCache() { $db = self::db(); $db->table('fcal_meta') ->where('object_type', self::$objectType) ->where('updated_at', '<', gmdate('Y-m-d H:i:s', strtotime('-15 days'))) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date ->delete(); } public static function db() { return (\FluentBooking\App\App::getInstance())->db; } }