Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to store client ip in Redis?

Tags:

redis

Can I save client IP in SET command in Redis by Redis' engine?

Something like this:

SET my_key $client_ip
like image 821
Alex Bar Avatar asked Jan 07 '23 15:01

Alex Bar


1 Answers

Assuming you don't know your $client_ip, you can do the following:

  1. Set a unique name for your connection to Redis with CLIENT SETNAME
  2. Get the client list with CLIENT LIST
  3. Locate the line with your connection's name and extract the IP address
  4. Use the obtained IP address in your SET command

Example

127.0.0.1:6379> client getname
(nil)
127.0.0.1:6379> client setname FreddyFrog
OK
127.0.0.1:6379> client getname
"FreddyFrog"
127.0.0.1:6379> client list
id=4 addr=127.0.0.1:49426 fd=6 name=FreddyFrog age=25 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
like image 160
Itamar Haber Avatar answered Jan 12 '23 17:01

Itamar Haber