@webkul Hi. I tried caching the API through redis. And the error came out: Class ‘Predis\Client’ not found
url lesson: https://webkul.com/blog/how-to-use-redis-for-api-caching-in-cs-cart/
Helper.php:
<?php
namespace Tygh\Addons;
use Tygh\Registry;
use Predis\Client;
class Helper
{
protected $redis;
public function __construct()
{
$config = Registry::get("config");
$this->redis = new Client([
'scheme' => 'tcp',
'host' => $config['cache_redis_server'], // Adjust based on your Redis server
'port' => $config['cache_redis_port'], // Adjust based on your Redis server
]);
}
public function get($key)
{
$data = $this->redis->get($key);
return $data !== null ? unserialize($data) : false;
}
public function set($key, $value, $expiration = 3600)
{
$this->redis->setex($key, $expiration, serialize($value));
}
}
way helper: Tygh\Addons\Helper
Api products:
public function index($id = 0, $params = array())
{
$redisCache = new Helper();
// Check if data is cached in Redis
$cacheKey = 'product_list';
$cachedData = $redisCache->get($cacheKey);
if ($cachedData !== false) {
return array(
'status' => Response::STATUS_OK,
'data' => $cachedData
);
}
$status = Response::STATUS_OK;
$lang_code = $this->getLanguageCode($params);
$params['extend'][] = 'categories';
if (fn_allowed_for('MULTIVENDOR')) {
$vendor_id = $this->safeGet($params, 'company_id', null);
if ($vendor_id) {
Registry::set('runtime.vendor_id', $vendor_id);
}
}
if ($this->getParentName() == 'categories') {
$parent_category = $this->getParentData();
$params['cid'] = $parent_category['category_id'];
}
if (!empty($id)) {
$data = fn_get_product_data($id, $this->auth, $lang_code, '', true, true, true, true, false, false, false);
if (empty($data)) {
$status = Response::STATUS_NOT_FOUND;
} else {
$data['selected_options'] = $this->safeGet($params, 'selected_options', []);
$products = $this->getProductsAdditionalData(array($data), $params);
$data = reset($products);
}
} else {
if (
isset($params['pshort']) && YesNo::toBool($params['pshort'])
|| isset($params['pfull']) && YesNo::toBool($params['pfull'])
|| isset($params['pkeywords']) && YesNo::toBool($params['pkeywords'])
) {
$params['extend'][] = 'description';
}
// Set default values to input params
$default_params = [
'match' => 'all',
'subcats' => YesNo::YES,
'pcode_from_q' => YesNo::YES,
'pshort' => YesNo::YES,
'pfull' => YesNo::YES,
'pname' => YesNo::YES,
'pkeywords' => YesNo::YES,
'search_performed' => YesNo::YES
];
$params = array_merge($default_params, $params);
$items_per_page = $this->safeGet($params, 'items_per_page', Registry::get('settings.Appearance.admin_elements_per_page'));
list($products, $search) = fn_get_products($params, $items_per_page, $lang_code);
$products = $this->getProductsAdditionalData($products, $search);
$data = array(
'products' => array_values($products),
'params' => $search,
);
}
// Cache the product data
$redisCache->set($cacheKey, $data, 3600);
return array(
'status' => $status,
'data' => $data,
);
}
What did I do wrong, please tell me.