Hunrik
Hunrik

Reputation: 483

How to use redis with hash storage in laravel?

I used the laravel's default Redis::set() function to store items, but I have 13k records and the memory usage is around 400mb. Now I'm looking for ways to reduce it. I noticed laravel stores data as string not as hash which is wasting resources. How can I use Redis with hash through laravel?

Upvotes: 1

Views: 9613

Answers (1)

LFI
LFI

Reputation: 654

In the official Laravel Redis Facade, it's said that

The Redis facade supports dynamic methods, meaning you may call any Redis command on the facade and the command will be passed directly to Redis

So I guess you can use

Redis::hSet('h', 'key1', 'hello');
$value = Redis::hGet('h', 'key1');

But I really doubt that you will reduce drastically your memory usage, unless you have very long key name. Instead you can :

  • If it's possible think about executing EXPIRE on your keys
  • You can also compress your key value : for example, if it's encoded in json, you can use msgpack which claims to be 1/3 smaller and faster to encode/decode, otherwise you can gz compress your content like explains in this post
  • You can use a Redis memory profiler like this one to search for a potential problem

If you test, let us know your results, it can be interesting.

Upvotes: 3

Related Questions