<?php
namespace Softech_Addons_for_Elementor\Includes;

if (!defined('ABSPATH')) {
    exit;
}

class Cache_Manager {
    const CACHE_GROUP = 'sitsel_addons';
    const CACHE_TTL = 3600; // 1 hour

    /**
     * Get cached value
     */
    public static function get($key, $default = false) {
        return wp_cache_get($key, self::CACHE_GROUP, false, $found);
    }

    /**
     * Set cached value
     */
    public static function set($key, $value, $ttl = self::CACHE_TTL) {
        return wp_cache_set($key, $value, self::CACHE_GROUP, $ttl);
    }

    /**
     * Delete cached value
     */
    public static function delete($key) {
        return wp_cache_delete($key, self::CACHE_GROUP);
    }

    /**
     * Flush all cache
     */
    public static function flush() {
        wp_cache_flush();
    }

    /**
 * Cache-aware database query
 *
 * @param string $query  SQL query with placeholders e.g. "SELECT * FROM {$wpdb->posts} WHERE ID = %d"
 * @param array  $args   Values to bind into placeholders e.g. [42]
 * @param string $key    Cache key
 * @param int    $ttl    Cache TTL in seconds
 */
public static function query_cached( $query, $args, $key, $ttl = self::CACHE_TTL ) {
    $cached = self::get( $key );
    if ( false !== $cached ) {
        return $cached;
    }
    global $wpdb;
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $result = $wpdb->get_results(
        $wpdb->prepare( $query, $args ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    );
    self::set( $key, $result, $ttl );
    return $result;
}
}