Alex
Alex

Reputation: 1486

How to fetch first 100 records from Redis

I am working on small application where I am using redis to hold my intermediate data. After inserting data, I need to reload my data in same order in which i have inserted.

I am using keys method to get all keys but the order of returned keys is not same as they were inserted.

Upvotes: 1

Views: 4521

Answers (2)

anvoz
anvoz

Reputation: 722

For indexing URL and getting the list by inserted order, you should use sorted set:

zadd <your_url_list_key> <inserted_time> <url>

Detail data for a single url should be stored in a different place. For example, use hash:

hset <your_url_data_key> <url> <url_data>

It's better if you don't store detail data on redis, so instead of using redis hash, you should save url detail data on mysql.

You can also md5(url) before indexing to reduce the size (then the full url value will be stored in url_data).

In my project, sorted set still works ok with about 3mil records (read & write frequently). But you should watch the hash size often, it will grow really fast.

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230441

You have to maintain order yourself, by keeping a separate list for inserted keys. So, instead of

SET foo, bar

you may do something like this:

SET foo, bar
RPUSH insert_order, foo

Then you can do

LRANGE insert_order, 0, 100

to get first 100 set fields.

If you want to track actual insertion (and not updates), you can use SETNX, for example. Also, you can use a sorted set instead of a list (as mentioned by @Leonid) Additionally, you can wrap the whole thing in Lua, so that the bookkeeping is hidden from the client code.

Upvotes: 2

Related Questions