PHP Redis with socket connection

if you are using Redis on a single server and/or want the web server at the same location to use sockets instead of TCP, update your redis config (redis.conf) to include:

unixsocket /var/run/redis/redis.sock

(you may be able to just uncomment that line to use the default)

You will need to make sure the socket connection has the correct permissions for the apache/php service. You can set the “unixsocketperm” attribute in redis.conf and make sure the socket location is appropriate for your apache/php set-up, or you can add a line in your redis start-up script to update the permissions, something like this:

chown :apache $SOCK && chmod g+w $SOCK

I’m not sure what the best practice is here, but I needed to do one the above to get it to work.

Redis page created

internal link > Redis Page

Redis start/stop init.d script for Linux

The init script that came with Redis (open source, advanced key-value store server) didn’t work and I couldn’t find one for RedHat/CentOS with a quick search, so I combined the parts that did and came up with one that seems to work both for start/stop via /sbin/service as well as /sbin/chkconfig. Note: at a minimum, you must set the “daemonize” value in the redis.conf file to “yes”.

Use the redis.conf file comes with the distro — edit as needed and copy it to: /etc/redis.conf.
You can then use the script below and copy that to: /etc/init.d/redis (change the port and any other attributes as needed). (To run multiple servers, change the port (and pid) values as appropriate.)

Click here to view init.d script for Redis

#!/bin/sh
#
# redis        Startup script for Redis Server
#
# chkconfig: - 90 10
# description: Redis is an open source, advanced key-value store. 
#
# processname: redis-server
# config: /etc/redis.conf
# pidfile: /var/run/redis_6379.pid
 
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
 
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=`which redis-cli`
 
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis.conf"
 
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo -n "$PIDFILE exists, process is already running or crashed\n"
        else
                echo -n "Starting Redis server...\n"
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo -n "$PIDFILE does not exist, process is not running\n"
        else
		PID=$(cat $PIDFILE)
                echo -n "Stopping ...\n"
				$REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x ${PIDFILE} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
esac

Redis vs Memcached + PHP Libs for Redis

I like the looks of Redis as a fast utility that falls somewhere between a Sql DB and memory cache.

Here is one comparison with Memcached, Redis vs Memcached
the intro: take memcached and add a disk backing, replication, virtual memory, and some cool additional data structures. Hashes, lists, sets, sorting, joining, transactions, yay! I instantly got a geek boner scanning the feature list… (His enthusiasm goes, um, down from there.)

And if you do want to use it with PHP, see Ori Pekelman’s blog:
Which PHP Library to use with Redis? The Benchmark
Basically comes down to using a pure-php library or one that uses a PHP-compiled module and the trade-offs of compatibility vs speed.

To use it with Yii, see: rediscache

init.d script for redis