{"id":6056,"date":"2012-07-30T13:35:25","date_gmt":"2012-07-30T08:05:25","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6056"},"modified":"2016-12-19T15:06:34","modified_gmt":"2016-12-19T09:36:34","slug":"using-apache-to-save-data-in-redis","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-apache-to-save-data-in-redis\/","title":{"rendered":"Using Apache to save data in redis"},"content":{"rendered":"<p>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.<\/p>\n<p>The first step for this was to install apache by firing the following command in terminal :<\/p>\n<p>[bash]sudo apt-get install apache2[\/bash]<\/p>\n<p>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:<\/p>\n<p>[bash]<br \/>\n&lt;VirtualHost *:80&gt;<br \/>\nServerAdmin webmaster@localhost<br \/>\nServerName www.raj.com<br \/>\nServerAlias raj.com<\/p>\n<p>DocumentRoot \/usr\/lib\/cgi-bin<\/p>\n<p>&lt;Directory \/&gt;<br \/>\nOptions FollowSymLinks<br \/>\nAllowOverride All<br \/>\nOrder allow,deny<br \/>\n&lt;\/Directory&gt;<\/p>\n<p>ScriptAlias \/cgi-bin\/ \/usr\/lib\/cgi-bin\/<br \/>\n&lt;Directory &quot;\/usr\/lib\/cgi-bin&quot;&gt;<br \/>\nAllowOverride All<br \/>\nAddHandler cgi-script .cgi<br \/>\nOptions +ExecCGI -MultiViews +SymLinksIfOwnerMatch<br \/>\nOrder allow,deny<br \/>\nAllow from all<br \/>\n&lt;\/Directory&gt;<\/p>\n<p>ErrorLog ${APACHE_LOG_DIR}\/error.log<\/p>\n<p># Possible values include: debug, info, notice, warn, error, crit,<br \/>\n# alert, emerg.<br \/>\nLogLevel warn<\/p>\n<p>CustomLog ${APACHE_LOG_DIR}\/access.log combined<\/p>\n<p>Alias \/doc\/ &quot;\/usr\/share\/doc\/&quot;<br \/>\n&lt;Directory &quot;\/usr\/share\/doc\/&quot;&gt;<br \/>\nOptions Indexes MultiViews FollowSymLinks<br \/>\nAllowOverride All<br \/>\nOrder deny,allow<br \/>\nDeny from all<br \/>\nAllow from 127.0.0.0\/255.0.0.0 ::1\/128<br \/>\n&lt;\/Directory&gt;<\/p>\n<p>&lt;\/VirtualHost&gt;<\/p>\n<p>[\/bash]<\/p>\n<p>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).<br \/>\nAddHandler cgi-script .cgi means that we are adding a cgi-script handler for files whose extension is cgi.<\/p>\n<p>Now my site is in the list of available sites, but it&#8217;s still not enabled. To enable it I fired the following command in terminal :<\/p>\n<p>[bash]a2ensite www.raj.com[\/bash]<\/p>\n<p>This command created a soft link of my site into \/etc\/apache2\/sites-enabled directory and now my site was enabled.<\/p>\n<p>The last step was to make an entry for my site in\u00a0 \/etc\/hosts. I appended following line to \/etc\/hosts to do so :<\/p>\n<p>[bash]127.0.0.1 www.raj.com[\/bash]<\/p>\n<p>Finally, i was required to reload apache using command<\/p>\n<p>[bash]service apache2 reload [\/bash]<\/p>\n<p>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.<\/p>\n<p>[bash]<\/p>\n<p>#!\/bin\/sh<br \/>\necho &quot;Content-type: text\/html\\n\\n&quot;<\/p>\n<p>date=&quot;na&quot;<br \/>\npartnerSite=&quot;na&quot;<br \/>\nvideoId=&quot;na&quot;<\/p>\n<p>date=`echo $QUERY_STRING | sed &#8216;s\/.*date\\=\\([^&amp;]\\+\\).*\/\\1\/&#8217;`<\/p>\n<p>partnerSite=`echo $QUERY_STRING | sed &#8216;s\/.*partnerSite\\=\\([^&amp;]\\+\\).*\/\\1\/&#8217;`<\/p>\n<p>videoId=`echo $QUERY_STRING | sed &#8216;s\/.*videoId\\=\\([^&amp;]\\+\\).*\/\\1\/&#8217;`<\/p>\n<p>`redis-cli hincrby &quot;$videoId-$date-views&quot; $partnerSite 1`<\/p>\n<p>[\/bash]<\/p>\n<p>Here shebang is used to execute script in the bash shell and the content type is set to text\/html.<br \/>\nIn redis, HINCRBY command is used in relation with hashes, where a key has one or more fields each with a corresponding value.<br \/>\nIf 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&#8217;t exists, HINCRBY creates a key with specified filed and value.<\/p>\n<p>Thus, if i hit the url  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 &#8220;wwYXWU-20July2012-views&#8221;, field will be &#8220;facebook&#8221; and value will be 1. If the same url is received again, the value of field &#8220;facebook&#8221; of key &#8220;wwYXWU-20July2012-views&#8221; will become 2 and so on.<\/p>\n<p>In this way, we can easily use apache to save and update data in redis database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[7],"tags":[18,260,857,453],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6056"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6056"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6056\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}