skip to Main Content

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.

2

Answers


  1. It is perfectly ok to store images in Redis. Redis keys and values are both binary-safe

    Redis Strings are binary safe, this means that a Redis 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.

    See Data types

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

    • In RAM memory usage on your redis server
    • In Network usage
    • In compute (CPU) usage assuming you are passing the images in binary to the final client

    You can do

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

    See Storing Binary Data in Redis

    Login or Signup to reply.
  2. Here is my simple example of storing image file into Redis Database and retrieve them as well

    from PIL import Image
    import redis
    from io import BytesIO
    
    
    output = BytesIO()
    im = Image.open("/home/user/im.jpg")
    im.save(output, format=im.format)
    
    r = redis.StrictRedis(host='localhost', port= 6379, db =0)
    r.set('imgdata', output.getvalue())
    output.close()
    r.save  #redis-cli --raw get 'imgdata' >test.jpg
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search