gpt4 book ai didi

json - 如何使用 Laravel 和 Predis 从 JSON 输出中更新或删除记录

转载 作者:可可西里 更新时间:2023-11-01 11:20:03 25 4
gpt4 key购买 nike

几个月前我开始使用 Laravel,我想实现 Redis。

$test 更新或删除 1 条或多条记录的最佳方式和最佳方式是什么。

    $allarticles = Article::all();
$client = Redis::connection();
$client->set('articles', $allarticles->toJson() );
$test = $client->set('articles');

输出:

    [{
"id":1,"title":"xQeMKGefAW","content":"44cuxAqVDS@gmail.com","created_at":null,"updated_at":null},{
"id":2,"title":"a5wpRVRBNZ","content":"SsH9U5kF32@gmail.com","created_at":null,"updated_at":null},{
"id":3,"title":"QF5xhsMh7d","content":"8erXnIojAM@gmail.com","created_at":null,"updated_at":null},{
"id":4,"title":"gQVbDNbcmD","content":"27feouH6vc@gmail.com","created_at":null,"updated_at":null},{
"id":5,"title":"FsOnoABBTg","content":"2qNutidwKZ@gmail.com","created_at":null,"updated_at":null},{
"id":6,"title":"89sS4UASJl","content":"cQku7DBKSB@gmail.com","created_at":null,"updated_at":null},{
"id":7,"title":"gpT3hO43V1","content":"EhzyEylbgw@gmail.com","created_at":null,"updated_at":null},{
"id":8,"title":"1DKvbBn7yV","content":"0cSAxi9if3@gmail.com","created_at":null,"updated_at":null},{
"id":9,"title":"pRr2LgzezC","content":"Aam0uuWLlF@gmail.com","created_at":null,"updated_at":null}

回答:

$allarticles = Article::all()->keyBy('id'); $client = Redis::connection();
$newarray = array();
foreach ( $allarticles->toArray() as $key => $value ){
$newarray[$key] = json_encode($value); } $client->hmset('testtest', $newarray);
$qwerty = $client->HGETALL('testtest');
print_r($qwerty);
}

最佳答案

不是将整个 json 存储为字符串,而是使用 hmset 命令将它们存储在 hashmap 中,id 是成员,json 的其余部分是值。

http://redis.io/commands#hash

设置json:

hmset articles 1 {"title":"xQeMKGefAW","content":"44cuxAqVDS@gmail.com","created_at":null,"updated_at":null} 2 {"title":"xQeMKGefAW","content":"44cuxAqVDS@gmail.com","created_at":null,"updated_at":null} and so on

要检索整个 json:

hgetall articles

要更新一个或多个值,请使用 HMSET

要删除一个或多个值,请使用 HDEL

希望这对您有所帮助。

关于json - 如何使用 Laravel 和 Predis 从 JSON 输出中更新或删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39425790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com