Redis how to store associative array? Set or Hash or List?

后端 未结 3 1867
孤独总比滥情好
孤独总比滥情好 2021-01-30 04:24

I\'m a bit confused with all the available storing options of Redis. I want to do something simple and I don\'t want to over engineer it. I\'m working with phpredis

3条回答
  •  自闭症患者
    2021-01-30 05:06

    Just for the folks looking for the PHP code, here is what I've ended up using:

    // Create a post hash
    $key = 'post:'.$post->getId();
    $this->redis->hSet($key, 'data', serialize($post->toArray()));
    
    // Add a post in the account posts SET
    $this->redis->sAdd($account->getId().':posts', $post->getId());
    
    // You can execute the above code as many time as you need 
    // to add an object in a SET
    
    // Fetch the first $limit posts for this account
    // SORT :posts BY nosort GET :post:*->data
    $key = $account->getId().':posts';
    $keys = $this->redis->sort($key, array(
        'by' => 'nosort',
        'limit' => array($offset, $limit),
        'get' => 'post:*->data'
    ));
    
    // All Good !
    var_dump($keys);
    

    I hope this will help some of you ;)

提交回复
热议问题