alexk307 / cache_deco

Backend agnostic cache decorator
MIT License
81 stars 7 forks source link

A value can lose its type when there is cache hit #3

Closed enzzc closed 8 years ago

enzzc commented 8 years ago

Cached values seem to be always retrieved and returned with type str. This can be a problem when a function is expected to return an int for example.

>>> mytest.my_method(2,3,2) # cache miss
512
>>> mytest.my_method(2,3,2) # cache hit
'512'

What would be the best way to force a certain return type?

alexk307 commented 8 years ago

Good catch!

I believe I fixed this last night by using the pickle module. Instead of converting everything to a string I'm pickling the object and un-pickling it when we load back from Redis. I would like to stay away from specifying a type to store since that to me seems less 'pythonic' FWIW.

I'm not sure if there are any limitations to using pickle but it seems to work with most objects/types.

enzzc commented 8 years ago

Yes, specifying a type would not be very pythonic... I've just pulled the changes, the pickle solution seems to work perfectly!

alexk307 commented 8 years ago

Great! Let me know if you have any other issues :)