Search code examples
phppythonlaravelredisphpredis

Redis : is it possible to share datetimes directly between php/laravel and python?


I'm using Redis (phpredis) to share data between my Laravel and my Python app.

Within Laravel, I save on a specific channel my array:

$data = MyModel::where('start_date','>', "2018-05-02 10:00:00")->get();

// $data = [{'id':1, 'start_date': "2018-05-03 12:00:00", 'name': "}…]

Redis::set("barChannel", json_encode($data));

Within Python, I read this data:

import redis
import json

myredis = redis.Redis()

data = json.loads(myredis.get('barChannel'));
// data = [{'id':1, 'start_date': "2018-05-03 12:00:00", 'name': "}…]

So basically the data passes successfully but instead of having directly a datetime value in my python list, I have a string which I have to convert. With the above example I can use then datetime.datetime.strptime but in the case of more complicated data structure (nested lists of dicts)... it's quite a pain to convert each variable.

Is there a way to store datetimes in Redis which would be natively recognized in Python?


Solution

  • No there is not, after little research you would know, that Redis stores its values in string representation only, thus your only option is to store serialized data and unserialize when needed.

    Start with following: