phpredisvalkey

How do I store images correctly in Redis or Valkey?


Decided to store images in Redis, how to do it correctly? Now I do this:

$redis->set('image_path', 'here is the base64 image code');

I'm not sure this is normal.


Solution

  • It is perfectly ok to store images in Valkey/Redis. Keys and values are both binary-safe

    Strings are binary safe, this means that a string can contain any kind of data, for instance a JPEG image or a serialized Ruby object.

    A String value can be at max 512 Megabytes in length.

    You can store the image in binary instead of base64 and it will be more efficient:

    You can do

    $client->set('profile_picture', file_get_contents("profile.png"));

    See Storing Binary Data in Redis