R
R
River00512019-05-17 15:51:59
PHP
River0051, 2019-05-17 15:51:59

How to cache mysql query in a variable?

Good afternoon, I'm trying to cache the result of a query in redis, but it doesn't work, the variable is empty and I don't understand why

function get60_v1($_60n) {
  global $wpdb;
  ob_start();
  $s_1 = $wpdb->get_row(
    "
    SELECT *
    FROM learn 
    WHERE s_name = '".$_60n->name."'
  " 
  );
  return ob_get_clean();
}

$key_60_sql = $pname.':'.get_field('user_version');
if ($redis->exists($key_60_sql)) {
  $s_1 = $redis->get($key_60_sql);
} else {
$sql_60 = get60_v1($_60n);
  // вычисляете свой кусок, затем
  $redis->set($key_60_sql, $sql_60);
  $redis->expire($key_60_sql, 604800); // 1 неделя
  $s_1 = $redis->get($key_60_sql);
}

Tell me what I'm doing wrong?
PS Caching of html and PHP goes without problems
PS If the function is executed without attempting caching, then the variable receives data
function get60_v1($_60n) {
  global $wpdb;
  $s_1 = $wpdb->get_row(
    "
    SELECT *
    FROM learn 
    WHERE s_name = '".$_60n->name."'
  " 
  );
  return $s_1;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
River0051, 2019-05-17
@River0051

Solved the problem like this

$key_60_sql = $pname.':'.get_field('user_version');
if ($redis->exists($key_60_sql)) {
  $_s_3 = $redis->hgetall($key_60_sql);
  $s_3 = (object)$_s_3;
} else {
$sql_60 = $wpdb->get_row (
    "
  SELECT *
  FROM learn 
  WHERE s_name = '".$_60n->name."'
  "
  );
  $array = (array)$sql_60;
  // вычисляете свой кусок, затем
  $redis->hMSet($key_60_sql, $array);
  $redis->expire($key_60_sql, 604800); // 1 неделя
  $_s_3 = $redis->hgetall($key_60_sql);
  $s_3 = (object)$_s_3;
}

M
Maxim Timofeev, 2019-05-17
@webinar

$60n- How is that? How about:
A valid variable name must begin with a letter or underscore and consist of any number of letters, numbers, and underscores.
https://www.php.net/manual/en/language.variables.b...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question