Home  >  Q&A  >  body text

php - About redis read and write escape

I only came into contact with redis and related things because of work needs. The result is as follows:
Part of the code is as follows
$newsRedis = Redis::get('news_'.$id);

    if ($newsRedis) return $newsRedis;
    $re = NewNotice::select('community', 'title', 'created_at', 'content', 'initiator', 'img')->where('id', '=', $id)->get();

    $change = NewNotice::where('id', '=', $id)->update([
        'state' => '1'
    ]);
    if (!$change) return 'NOTICE_ERROR';

// dd($change);

    Redis::set('news_'.$id,$re);
    Redis::expire('news_'.$id,10);
    return $re;
}

It turned out that there was an escape slash before each symbol

The following is the result of my direct return $re

Why does this happen? How can I avoid escaping?

迷茫迷茫2686 days ago1482

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-07-03 11:42:42

    You take it out, first json_decode and then encapsulate the data and output it to the front end.
    Your escaping is because it was json_encode twice.

    What you store in Redis is a JSON string. You take it out directly, put it into the data, and perform json_encode again, so it is normal for this to happen.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-07-03 11:42:42

    This escaping is normal. It’s because of json. JSON requires that all key values ​​must be enclosed in double quotes ". Values ​​containing double quotes naturally need to be escaped. You dd($change->datas) will not have slashes If you really don’t want to see the double quotes, you can url_encode first and then url_decode when using it, but it’s not necessary

    reply
    0
  • Cancelreply