get all keys that starts with namespace prefix

397 views
Skip to first unread message

Mudassir Maredia

unread,
Jun 12, 2015, 9:17:25 AM6/12/15
to jedis...@googlegroups.com

I have a web-sockets application where I need to use Redis and Jedis as a client. When  a web sockets request comes in to the controller I save it using Jedis.set("websockets:<long socket id>", "sessionId");
There is another process that sends out data to all sockets. How can I get:

1. all socket IDs that starts with "websockets:" OR all data as collection at least ?
2. To close all sockets when user session time outs , how can I get all socket IDs based on sessionId ?

Mudassir.

Marcos Lilljedahl

unread,
Jun 12, 2015, 6:09:40 PM6/12/15
to jedis...@googlegroups.com
Mudassir

Why are you storing your socket ids as standard keys? I believe the best thing is to use a hashmap and do something like. 

Jedis.hset("websockets", "<long socket id>", "sessionId")

Then, if you need to retrieve a specific sessionId the operation would be very simple

Jedis.hget("websockets","<long socket id>");

At the same time if you need to retrieve the complete list you can do

Jedis.hgetall("websockets"); 


And this will return the complete list of websockets



--
You received this message because you are subscribed to the Google Groups "Jedis" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jedis_redis...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mudassir Maredia

unread,
Jun 15, 2015, 7:32:23 PM6/15/15
to jedis...@googlegroups.com
Thanks Marcos,

The approach you highlighted makes more sense. I changed my code to use hset and hget but it is throwing this error while I try to set some value using hset. Do I have to initialze "websockets"collection first ?

Code:
           getJedis().hset("websockets", sock.writeHandlerID(), payload.toString()); // all are strings.

Exception:

redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of value

Marcos Lilljedahl

unread,
Jun 15, 2015, 7:36:33 PM6/15/15
to jedis...@googlegroups.com
Mudassir, seems like you already have a key that is called "websockets" which is not of hashmap type (maybe you're using it somewhere else). 

Try flushing your redis instance and trying again.

Mudassir Maredia

unread,
Jun 15, 2015, 9:26:10 PM6/15/15
to jedis...@googlegroups.com
Marcos,

Yes, that was the problem. I flushed redis and it worked :) Thank You !!

-Mudassir.
Reply all
Reply to author
Forward
0 new messages