It just takes 15 seconds. Send your
details and we’ll get back to you soon.

Using Apache to save data in redis

In one of my projects, I was using redis database to collect some statistics and I thought of saving data into it at apache level. This would considerably enhance the speed of saving data as it would not require the interception of grails to save data.

The first step for this was to install apache by firing the following command in terminal :

sudo apt-get install apache2

After installing apache, it was required to set up a site. For that I created a file named www.raj.com in /etc/apache2/sites-available directory and configured it as follows:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName www.raj.com
ServerAlias raj.com

DocumentRoot /usr/lib/cgi-bin

<Directory />
Options FollowSymLinks
AllowOverride All
Order allow,deny
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride All
AddHandler cgi-script .cgi
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

Here the ServerName is set to www.raj.com and DocumentRoot is set to /usr/lib/cgi-bin. It means that if I hit url www.raj.com, then i will see the contents of /usr/lib/cgi-bin directory (This will come into action after enabling the site).
AddHandler cgi-script .cgi means that we are adding a cgi-script handler for files whose extension is cgi.

Now my site is in the list of available sites, but it’s still not enabled. To enable it I fired the following command in terminal :

a2ensite www.raj.com

This command created a soft link of my site into /etc/apache2/sites-enabled directory and now my site was enabled.

The last step was to make an entry for my site in  /etc/hosts. I appended following line to /etc/hosts to do so :

127.0.0.1 www.raj.com

Finally, i was required to reload apache using command

service apache2 reload 

And now my site was live. To save data into the redis database, i created a file named saveData.cgi in /usr/lib/cgi-bin folder, given execution permissions to it and entered the following script into it.


#!/bin/sh
echo "Content-type: text/html\n\n"

date="na"
partnerSite="na"
videoId="na"

date=`echo $QUERY_STRING | sed 's/.*date\=\([^&]\+\).*/\1/'`

partnerSite=`echo $QUERY_STRING | sed 's/.*partnerSite\=\([^&]\+\).*/\1/'`

videoId=`echo $QUERY_STRING | sed 's/.*videoId\=\([^&]\+\).*/\1/'`

`redis-cli hincrby "$videoId-$date-views" $partnerSite 1`

Here shebang is used to execute script in the bash shell and the content type is set to text/html.
In redis, HINCRBY command is used in relation with hashes, where a key has one or more fields each with a corresponding value.
If the key and field already exists in the redis database, HINCRBY command would increment the value of the field by specified number. If the key doesn’t exists, HINCRBY creates a key with specified filed and value.

Thus, if i hit the url http://www.raj.com/saveData.cgi?date=20July2012&partnerSite=facebook&videoId=wwYXWU in my browser, then date, partnerSite and videoId are extracted from the query string using sed command and in the redis database an entry would be saved whose key will be “wwYXWU-20July2012-views”, field will be “facebook” and value will be 1. If the same url is received again, the value of field “facebook” of key “wwYXWU-20July2012-views” will become 2 and so on.

In this way, we can easily use apache to save and update data in redis database.


Posted in Database, Grails | Tagged , , , .

One Response to Using Apache to save data in redis

  1. Fazal Majid says:

    Or you could just use Webdis:
    http://webd.is/

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + 6 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>