Search code examples
phpredispredis

CRUD & Relationships in Redis: How to


So Redis (Predis Library for PHP) is incredibly useful, as this SO answer clearly shows the many use cases: What is Redis and what do I use it for? And this answer looks at handling many to many relationships with Redis: how to have relations many to many in redis

QUESTION: CRUD & Pagination in Redis. How do we achieve this? Do we need to pre-load the entire table (all rows) into Redis?

Environment: LAMP Stack, OSX


Solution

  • How do we achieve this?

    For pagination, if your backend data-structure is:

    • a list use lrange
    • a set use sscan
    • a hash use hscan

    Note that *scan function can yield multiple times the same value so you will have to do ensure client-side that the data your received at each iteration is unique.

    Do we need to pre-load the entire table (all rows) into Redis?

    It clearly depends on your use-case. But yes, it will be easier to talk directly to redis than to talk to redis and mysql at the time (however that can be done sucessfully without issue if you shard your data in a proper way).