Issue #35 resolved

about the region get_multi / get_or_create_multi return type

Lx Yu
created an issue

Currently the get multi apis returns list, while I think the dict is a better choice.

Take this code for example:

If return list, I have to make sure all order_ids exist, also must I sort the result based on the keys. If any key misses or the order of result mixed, it can lead to incorrect cache, which is severe error.

@region.cache_multi_on_arguments()
def get(*order_ids):
    orders = DBSession().query(Order).filter(Order.id.in_(order_ids)).all()
    assert(len(orders) == len(order_ids))
    return sorted(orders, key=lambda o: order_ids.index(o.id))

If return dict, it'll be much simpler:

@region.cache_multi_on_arguments()
def get(*order_ids):
    orders = DBSession().query(Order).filter(Order.id.in_(order_ids))
    return {order.id: order for order in orders}

So if the return type is dict, it can be more fault tolerant without lose in function.

Comments (3)

  1. Mike Bayer repo owner

    are we talking about the get_multi() return type (this returns NO_VALUE for missing keys so the result is unambiguous, just call dict(zip(keys, values)), or the data type expected by the decorated function in cache_multi_on_arguments() (in which case, add a keyword argument to cache_multi_on_arguments(asdict=True) ), there's no issue having both ways for that one?

  2. Log in to comment