<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CodeSanity &#187; Tutorials</title>
	<atom:link href="http://codesanity.net/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://codesanity.net</link>
	<description>PHP, Javascript &#38; Technology Ramblings</description>
	<lastBuildDate>Wed, 07 Jul 2010 00:28:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using MongoDB for CodeIgniter Logs</title>
		<link>http://codesanity.net/2010/05/mongodb-codeigniter-logs/</link>
		<comments>http://codesanity.net/2010/05/mongodb-codeigniter-logs/#comments</comments>
		<pubDate>Tue, 18 May 2010 06:01:09 +0000</pubDate>
		<dc:creator>Tom Schlick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[clusters]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[logs]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[nosql]]></category>
		<category><![CDATA[production]]></category>

		<guid isPermaLink="false">http://codesanity.net/?p=229</guid>
		<description><![CDATA[As developers, we like to log everything. Flat file logging is great for single server setups but becomes troublesome to manage on a cluster. We will use MongoDB to fix this in regards to the CodeIgniter error logs.

<br />]]></description>
			<content:encoded><![CDATA[<p>Anyone who has ever worked in a production environment knows that your users will find errors you have not even dreamed of. They will find your broken links, they will find your missing files, and yes they will find every database connectivity issue you hoped would never occur. Thankfully we are all great developers and sysadmins so we log everything we possibly can to find and remove these errors.</p>
<p>This is great at first but after a while log files become enormous and eat up lots of disk space. The problem multiplies when you start getting into a clustered environment (more than 1 server). You now have to read &amp; maintain logs on 2+ machines which will most likely lead to neglecting your error fixing duties. Fortunately MongoDB recently hit the scene.</p>
<h2>MongoDB</h2>
<p><a href="http://www.mongodb.org/" target="_blank">MongoDB</a> is a scalable, high-performance, open source, document-oriented database. It&#8217;s ability to accept huge amounts of data very quickly make it perfect for application logging. It has built in support for auto-sharding &amp; replication so when your application takes off mongodb will scale with you. It uses a JSON based storage system meaning you can give the database an object (from any programming language with support) and when you pull it back out of the database it will be the exact same as when you put it in. No conversion, no hassle, no bullshit.</p>
<h2>Capped Collections</h2>
<p>Mongo uses things called Collections which are essentially tables in mysql. The main difference is these collections are schemaless. I can have 10 fields in one document (row) and 500 in another and they will play nice inside the collection. This allows a huge amount of flexibility to the developer.</p>
<p>These collections also have the ability to be &#8220;Capped&#8221;. This means I can set a max document count of 5000 and only the most recent 5000 documents will be kept. The older documents are deleted as new ones come in. This is awesome for logging because you will never have to rotate your logs in fear that they will become too large and occupy all of your file system space.</p>
<p>Use this command to cap a collection (in a mongo shell):</p>
<blockquote>
<pre><strong>db.createCollection("my_collection", {capped:true, size:5000})</strong></pre>
</blockquote>
<p><a title="More Information on Capped Collections" href="http://www.mongodb.org/display/DOCS/Capped+Collections" target="_blank">More Information on Capped Collections</a></p>
<h2>Use in CodeIgniter</h2>
<p>By default CodeIgniter writes to log files in /system/logs/ . I have created a class that overwrites that default functionality and sends all logs to a mongo db collection. Adding this library to all of your servers will allow all of those servers to write to the same mongo collection. I have also added some more data to the logs that is helpful in a clustered environment such as the server name, server ip, request uri, and a few more. Just place the MY_Log.php in your application/libraries directory and codeigniter should handle the rest.</p>
<p>You can view and fork the class on Github:<br />
<a href="http://github.com/trs21219/codeigniter-mongo-logs/" target="_blank">http://github.com/trs21219/codeigniter-mongo-logs/</a></p>
<p><script src="http://gist.github.com/404697.js?file=MY_Log.php"></script></p>
<p>Feel free to modify data inserted to fit your specific needs. If you find any bugs or have suggestions you can comment below or raise an <a href="http://github.com/trs21219/codeigniter-mongo-logs/issues" target="_blank">issue here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://codesanity.net/2010/05/mongodb-codeigniter-logs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Avoiding Database Overload with Memcached</title>
		<link>http://codesanity.net/2010/03/avoiding-database-overload-with-memcached/</link>
		<comments>http://codesanity.net/2010/03/avoiding-database-overload-with-memcached/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 06:23:30 +0000</pubDate>
		<dc:creator>Tom Schlick</dc:creator>
				<category><![CDATA[Downloads]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[key pair]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://codesanity.net/?p=194</guid>
		<description><![CDATA[As you build out your application, scalability will start become an issue. You will find yourself making queries to the database every page load cost you precious cpu and load time. The trick is to cache frequently used data so that a database query is not necessary, reducing the overhead on your server.]]></description>
			<content:encoded><![CDATA[<h2>Overview</h2>
<p>Probably the biggest challenge for a newly popular site these days is scalability. The most common point of failure in applications seems to always be the database. These days our applications are totally reliant on some sort of relational database to store the information for our website&#8217;s content. Each and every page load brings many queries that read or insert information into this database all while rendering the web page in a timely manner. This isn&#8217;t so much a problem for your Mom &amp; Pop business, but when your website starts to get some real traffic pointed towards it,  your underlying architecture that you thought was impervious to anything will crumble before your eyes. The solution? Stop making so many queries to your database.</p>
<h2>Caching</h2>
<p>Caching is nothing new. Servers have been caching content for many years quite successfully. There are many different ways you can cache data in your application but right now we will just be talking about using <a href="http://memcached.org/" target="_blank">Memcached</a>. Memcached is a high-performance, distributed memory object caching system. So what does this mean? Basically it can store any kind of value that you would normally store in your database and retrieve it much much faster than your database can.</p>
<h2>Memcached Flow</h2>
<p>When you implement memcached you use it along side of your database. Neither takes the place of each other, memcached just takes some of the load off of your database server after your database server has previously done the grunt work. It works on a key pair system. Meaning it has no tables, rows, columns etc. You give memcached a name and pass it data and it stores that data just as you passed to it. You can pass it an object, array, binary, etc and it will output the same way when you go to retrieve it.</p>
<p>So how does this all work with the database? First, you add code that checks to see if the memcached object (blogpost-45 for our example) exists before it checks the database for the data. If it does you return that data and never touch the DB.  If the memcached object does not exist  you retrieve data from your database, take the array that is passed back and store it in  memcached with the key of &#8220;blogpost-45&#8243;. So the next time you try to get the data for blogpost-45 it will return from memcached and not your database. This is where the load savings occurs.</p>
<p>So what happens when I want to change the data? When you change the data in your database you should delete the key in memcached. You shouldn&#8217;t re-add the data after you delete it, let the select query do that to avoid complications.</p>
<h2>CodeIgniter + Memcached</h2>
<p>I have created a CodeIgniter Library that can help you get a jump start on your integration with memcached, its available on my github page and will be updated when I get time.</p>
<p><a href="http://github.com/trs21219/memcached-library" target="_blank">http://github.com/trs21219/memcached-library</a></p>
]]></content:encoded>
			<wfw:commentRss>http://codesanity.net/2010/03/avoiding-database-overload-with-memcached/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Conditional .Htpasswd for Multi-Environment Setups</title>
		<link>http://codesanity.net/2009/11/conditional-htpasswd-multienvironment-setups/</link>
		<comments>http://codesanity.net/2009/11/conditional-htpasswd-multienvironment-setups/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 08:58:47 +0000</pubDate>
		<dc:creator>Tom Schlick</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[conditional]]></category>
		<category><![CDATA[environments]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[htpasswd]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://codesanity.net/?p=125</guid>
		<description><![CDATA[When using a Multi-Environment setup for your development process it is essential to have adequate protection from outside prying eyes, but also allow people to access your public/testing sites. This .htaccess code allows you to do that.]]></description>
			<content:encoded><![CDATA[<p>So a while back i set Chris over at <a href="http://css-tricks.com" target="_blank">CSS-Tricks</a> some code for his ongoing <a href="http://css-tricks.com/snippets/" target="_blank">snippet library</a> project. This is a quick explanation of that code.</p>
<p>You see when you are working with a multi-environment setup that is synced via a version control system such as Git or Subversion, you need a way to keep your development environments locked down while allowing access to your public environment.</p>
<p>Below is some text that you will input into a .htaccess file placed in your webroot</p>
<p>[sourcecode language='php']<br />
#allows a single uri through the .htaccess password protection<br />
SetEnvIf Request_URI &#8220;/testing_uri$&#8221; test_uri</p>
<p>#allows everything if its on a certain host<br />
SetEnvIf HOST &#8220;^testing.yoursite.com&#8221; testing_url<br />
SetEnvIf HOST &#8220;^yoursite.com&#8221; live_url<br />
Order Deny,Allow</p>
<p>AuthName &#8220;Restricted Area&#8221;<br />
AuthType Basic<br />
AuthUserFile /path/to/your/.htpasswd<br />
AuthGroupFile /<br />
Require valid-user</p>
<p>#Allow valid-user<br />
Deny from all<br />
Allow from env=test_uri<br />
Allow from env=testing_url<br />
Allow from env=live_url<br />
Satisfy any<br />
[/sourcecode]</p>
<p>so in the above code the &#8220;testing_uri&#8221; part could be if i only wanted to allow this url through my htaccess protections (useful for paypal pings) ex &#8220;http://mysite.com/paypal/ipn&#8221;</p>
<p>the host part is to allow anyone through if they are requesting the code from a specific domain such as &#8220;testing.yoursite.com&#8221;. if it is &#8220;development.yoursite.com&#8221; it will not allow the user through.</p>
<p><a href="http://css-tricks.com/snippets/htaccess/allow-single-url/" target="_blank">here is the code on css-tricks</a> and <a href="http://gist.github.com/229178" target="_blank">here is the code as a github gist</a></p>
<p>Thanks for reading and as always comments &amp; questions are heavily encouraged!</p>
]]></content:encoded>
			<wfw:commentRss>http://codesanity.net/2009/11/conditional-htpasswd-multienvironment-setups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Content Delivery Network via Amazon Web Services: S3: assets.codesanity.net.s3.amazonaws.com

Served from: codesanity.net @ 2010-09-09 19:29:06 -->