Memcached And PHP, Caching Mysql Query Result

I’ve been messing around with memcached and php-pecl-memcache to cache sql query result. Many web sites & applications such as Facebook, LiveJournal, Flickr, Slashdot, WikiPedia/MediaWiki, SourceForge, Digg and Twitter use memcached to enhance their performance.

Memcached (Memory Cache Daemon) was developed by the team at LiveJournal to improve performance of their social blogging site by minimizing the impact of the bottleneck caused by reading data directly from the database. Memcached is a server that caches Name Value Pairs in memory. The “Name”, or key, is limited to 250 characters, and the “Value” is limited to 1MB in size. Values can consist of data, HTML  Fragments, or binary objects; almost any type of data that can be serialized and fits in memcached can be stored.

here is simple example/demonstration how to cache regular sql query

memcached flow
memcached flow

First of all, we need memcached daemon run on system

$ ps ax | grep memcached
 8955 ?        Ssl    0:00 memcached -d -p 11211 -u memcached -m 256 -c 1024 -P /var/run/memcached/memcached.pid -l 127.0.0.1

Setup simple mysql database/tables as shown bellow:

mysql-shell> CREATE DATABASE memcache;

Copy/Paste this tables schema to your mysql shell/console

CREATE TABLE memc
(
 personID int NOT NULL AUTO_INCREMENT,
 PRIMARY KEY(personID),
 FirstName varchar(15),
 LastName varchar(15),
 Age int
);
hit enter/return key

Insert some data

mysql-shell> INSERT INTO memc (FirstName, LastName, Age) VALUES('Memory', 'Cache', '100');

postfix-2.9.20110501 SRC RPM With sqlite3 Support

Since postfix-2.9. 20100617, sqlite3 support feature was added. Now, in  postfix-2.9.20110501 rpm source, i’ve included sqlite3 feature after some tweaking on Simon J. Mudd’s postfix source. Adding sqlite3 support howto can be read officially from here:

http://www.postfix.org/SQLITE_README.html

Before getting any further, i must warn you that i’m not test sqlite feature yet, since i don’t use sqlite as sql storage backend. it was compiled againts sqlite library in my system. but not tested.

rpm source can be obtain here:

postfix-2.9.20110501 (14 downloads )

Instructions:

After download the source install the source

$ rpm -Uvh  postfix-2.9.20110501.src.rpm

Nginx As Reverse Proxy IPV6 to IPV4 Website

Like many other reverse proxying mechanism, the configuration is quite simple. All we need is just website that's lived in ipv4, and Nginx with IPv6 capabled network. [text] server { listen [::]:80; # your server's public IP address server_name www.example.com; # your domain name access_log /var/log/nginx/www.example.com-access.log main; location / {…

nginx mod_strip module and textarea

I was asking to Evan Miller if his nginx mod_strip module can honour <textarea> tag. i got quick responses from him.
previous mod_strip module eliminates newline characters within <textarea> list contents. here’s examples with latest mod_strip. I’ve tested it using postfixadmin.

configuration:

server {
         listen		xxx.xxx.xxx.xxx:80;
         server_name	www.example.com;
         strip		on;
         access_log	/var/log/nginx/www.example.com-access.log  main;

         location  / {
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-Host $host;
             proxy_set_header X-Forwarded-Server $host;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_pass http://127.0.0.1:80;
         }
}

Nginx Configuration SyntaxHighlighting

Feel bored with black and white display nginx configuration? Evan Miller create Syntax highlighting for the world's best text editor, vim. Here's the tutorial http://www.vim.org/scripts/script.php?script_id=1886, or this how i've made it work on mine. First change to .vim directory on home directory, then create directory called syntax. example: [bash] # cd .vim…

Nginx + Vlc Streaming Cheap Webcam

webcam streaming

I was experimenting with cheap webcam, vlc on my windows xp workstation. Playing directly using vlc, performance was adequate. but when it came accross network/WAN, delay started increasing between 4-5 seconds(video/audio). i suspect, transcoding are behind these streaming lag. i don’t know what is the best tuning. but here’s the final setting.

start /high vlc.exe --logo-file avatar.png dshow:// :dshow-vdev="VideoCAM Eye" :dshow-adev="SigmaTel Audio" :dshow-caching="0" :sout=#transcode{vcodec=h264{keyint=15,vbv-maxrate=160,vbv-bufsize=160},acodec=mp3,deinterlace,samplerate=44100,sfilter=logo}:std{access=http{mime=video/x-flv},mux=ffmpeg{mux=flv},dst=0.0.0.0:8088/stream.flv,sdp=sap://,name="Webcam 3G"} --ttl 12

postfix-2.9.20110323 SRC RPM

postfix-2.9.20110323.src.rpm since snapshot 20110320, postfix has a new feature called "enable_long_queue_ids" thread detail can be read at postfix-user mailing list or here [text] Below is the manpage entry for long queue ID support. Let me know if there's anything missing. This code is part of this weekend's snapshot (*). Several…

Build Nginx RPM With ngx_cache_purge module

I’m not going have much words to say, here we go..

first download nginx source rpm from this excellent contributor/packager and install the source

$ wget http://centos.alt.ru/pub/nginx/0.9/RHEL/SRPMS/nginx-0.9.5-1.el5.src.rpm
$ rpm -Uvh  nginx-0.9.5-1.el5.src.rpm

Change to /path/rpm/SOURCE and download ngx_cache_purge

$ cd `rpm --eval '%{_sourcedir}'`
$ wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz

Now we need to modified nginx.spec a bit, so it will include ngx_cache_purge module when compiled

$ cd `rpm --eval '%{_specdir}' `
$ vi nginx.spec

Add ngx_cache_source-1.2.tar.gz module to source list as Source20

Source11:       nginx_mod_h264_streaming-2.2.7.tar.gz
Source20:       ngx_cache_purge-1.2.tar.gz

In %prep section add this line %setup -T -D -a 20

%setup -T -D -a 11
%setup -T -D -a 20
%setup -T -D -a 21

In %build section add this line –add-module=%{_builddir}/nginx-%{version}/ngx_cache_purge-1.2 \

...
    --add-module=%{_builddir}/nginx-%{version}/nginx_upload_module-2.2.0 \
    --add-module=%{_builddir}/nginx-%{version}/ngx_cache_purge-1.2 \
    --add-module=%{_builddir}/nginx-%{version}/nginx_mod_h264_streaming-2.2.7
...