colinmollenhour / Cm_Cache_Backend_Redis

A Zend_Cache backend for Redis with full support for tags (works great with Magento)
Other
389 stars 142 forks source link

Slave select #118

Closed Xon closed 7 years ago

Xon commented 7 years ago

This merges master into the sentinels-and-slaves and then patches Cm_Cache_Backend_Redis to implement callback based policy for what slave to be picked. This 'slave-select' option needs to be documented, and possibly cleaned up.

I'm open to suggestions for a better interface and feedback. I currently use this functionality to prefer the (any) local slave over picking a random slave https://github.com/Xon/XenForo-RedisCache/blob/master/upload/library/Zend/Cache/Backend/Redis.php#L18-L61

This process is horrible since there doesn't appear to be a standard way to get all local IPs that php can see. as such, I haven't included it in this pull request.

colinmollenhour commented 7 years ago

Thanks, @Xon !

Regarding a method to find local IPs, you could just test to see if the slaves are in the local ranges. Here is how I did this in a bash script once:

  # https://gist.github.com/jjarmoc/1299906
  IP=$1; IPNUM=0; for (( i=0 ; i<4 ; ++i )); do ((IPNUM+=${IP%%.*}*$((256**$((3-${i})))))); IP=${IP#*.}; done
  [ "$IPNUM" -ge 167772160 -a "$IPNUM" -le 184549375 ] && return 0 # 10.0.0.0 - 10.255.255.255
  [ "$IPNUM" -ge 2886729728 -a "$IPNUM" -le 2887778303 ] && return 0 # 172.16.0.0 - 172.31.255.255
  [ "$IPNUM" -ge 3232235520 -a "$IPNUM" -le 3232301055 ] && return 0 # 192.168.0.0 - 192.168.255.255
Xon commented 7 years ago

@colinmollenhour Sorry for the confusion;

I'm running a slave redis instance on the webserver node; and to reduce traffic congestion on the master redis/database node I want the webserver node to read from the local redis instance if possible.

This is what I'm having trouble doing in a portable way from php.

colinmollenhour commented 7 years ago

Oh, nevermind my previous comment, just knowing if they are local networks is not the issue you want machine-local.. I don't know of a better way to get this in PHP than you are already doing.