0

I started a few months ago with Laravel and I want to implement Redis.

How and what is the best way to update or delete 1 or more records from $test.

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

Output:

    [{
        "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}

Answer:

$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);
}
Bas
  • 2,330
  • 4
  • 29
  • 68

1 Answers1

1

Instead of storing the whole json as a string, store them in a hashmap using hmset command, id is the member and rest of the json is the value.

http://redis.io/commands#hash

To set the 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

To retrieve the whole json:

hgetall articles

To update one or more values use HMSET

To delete one or more values use HDEL

Hope this helps.

Karthikeyan Gopall
  • 5,469
  • 2
  • 19
  • 35
  • How do i do that, do i have to make a loop? – Bas Sep 11 '16 at 11:11
  • You need to get a key Value pair for example a hashmap from your Json. That is up to your app logic then a single call of Hmset will do the job. Similarly hgetall and heel are single call. – Karthikeyan Gopall Sep 11 '16 at 17:28
  • I hope you can give me an example :) – Bas Sep 13 '16 at 05:49
  • I made the following: $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); Is that the correct way? – Bas Sep 13 '16 at 06:25