Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting values with the right type in Redis

I'm using redis in my python application to store simple values like counters and time stamp lists, but trying to get a counter and comparing it with a number I came across a problem.

If I do:

import redis ... myserver = redis.Redis("localhost") myserver.set('counter', 5) 

and then try to get that value like this:

if myserver.get('counter') < 10:      myserver.incr('counter') 

then I get a type error in the if statement because I'm comparing '5' < 10, which means I'm storing an integer value and getting a string one (which can be considered as a different value).

My question is: is this supposed to work like that? I mean its a very basic type, I understand if I have to parse objects but an int? Seems that I'm doing something wrong.

Is there any configuration I'm missing?

Is there any way to make redis return the right type and not always a string? I say this because its the same for lists and datetimes or even floating point values.

Could this be a problem with the redis-py client I'm using and not redis itself?

like image 670
jeruki Avatar asked Oct 25 '12 02:10

jeruki


People also ask

How do I get all Redis values?

The Redis command documentation contains no native commands for getting the key and value of multiple keys. The most native way of doing this would be to load a lua script into your redis using the SCRIPT LOAD command or the EVAL command.

What type of data is stored in Redis?

It can store any type of data, like text, integers, floats, videos, images, or audio files. In this example, SET and GET are Redis commands, which we will discuss later. name is the key, and educative is the string value that we are storing.

Can Redis store integers?

Redis stores integers in their integer representation, so for string values that actually hold an integer, there is no overhead for storing the string representation of the integer.

Is Redis key value store?

redis is an in-memory, key/value store. Think of it as a dictionary with any number of keys, each of which has a value that can be set or retrieved. However, Redis goes beyond a simple key/value store as it is actually a data structures server, supporting different kinds of values.


2 Answers

Technically speaking you need to take care of that on your own.

However, have a look at this link, especially at the part of their README that refers to parsers and response callbacks, maybe that's something you can use. Question would be whether this is an overkill for you or not.

like image 127
favoretti Avatar answered Sep 21 '22 08:09

favoretti


As @favoretti said, response callbacks will do the trick. It's not complicate at all, just one line and all will be taken care of.

In [2]: import redis In [3]: r = redis.Redis() In [10]: r.set_response_callback('HGET', float) In [11]: r.hget('myhash', 'field0') Out[11]: 4.6 

for hmget, it returns a list of strings, not one single string, so you need to construct a little more comprehensive callback function:

In [12]: r.set_response_callback('HMGET', lambda l: [float(i) for i in l])  In [13]: r.hmget('myhash', 'field0') Out[13]: [4.6] 

same for hgetall.

like image 22
pingz Avatar answered Sep 21 '22 08:09

pingz