App App App App

<?php

function get_key($pi) {
    $terms = strlen($pi) != 0 ? $pi : 'Monday';
    $key = end(explode('/', $terms));
    return $key;
}

$key = get_key($_SERVER['PATH_INFO']);
$mckey = 'demowebapp.' . $key;

$mc = new Memcache;
$mc->connect('192.168.11.199', 11211);
$val = $mc->get($mckey);
if (!$val) {
    $pgconn = pg_connect("host=192.168.11.199 dbname=demowebapp");
    $res = pg_query($pgconn, "SELECT * from demowebapp_x WHERE id = '$key';");
    $row = pg_fetch_row($res);
    $val = $row[1];
    pg_free_result($res);
    pg_close($pgconn);
    $mc->set($mckey, $val, 0, 1);
}

print "$val\n";
?>
use strict;
use DBI;
use Cache::Memcached;

sub get_key {
    my $pi = shift;
    my @terms = split(/\//, $pi || "Monday");
    return $terms[-1];
}

my $app = sub {
    my $env = shift;

    my $key = get_key($env->{'PATH_INFO'});
    my $mckey = 'demowebapp.' . $key;

    my $mc = new Cache::Memcached({'servers' => ['192.168.11.199:11211']});
    my $val = $mc->get($mckey);
    if (!$val) {
        my $dbh = DBI->connect('DBI:Pg:dbname=demowebapp;host=192.168.11.199');
        my $sth = $dbh->prepare("SELECT * FROM demowebapp_x WHERE id = '$key';");
        $sth->execute();
        ($key, $val) = $sth->fetchrow_array();
        $sth->finish();
        $dbh->disconnect();
        $mc->set($mckey, $val, 1);
    }

    return ['200', ['Content-Type' => 'text/html'], [$val]];
};
import psycopg2
import memcache

def get_key(pi):
    terms = [token for token in pi.split('/') if token != '']
    if terms:
        return terms[-1]
    return 'Monday'

def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/html')])
    key = get_key(environ['PATH_INFO'])
    mckey = 'demowebapp.' + key
    mc = memcache.Client(['192.168.11.199:11211'])
    val = mc.get(mckey)
    if not val:
        pg = psycopg2.connect(database='demowebapp', host='192.168.11.199')
        csr = pg.cursor()
        csr.execute("SELECT * FROM demowebapp_x WHERE id = '%s';" % key)
        val = csr.fetchone()[1]
        csr.close()
        pg.close()
        mc.set(mckey, val, time=1)
    return [val]
class Lookup
    def get_key(pi)
        terms = pi != nil ? pi : 'Monday'
        terms.split('/')[-1]
    end

    def call env
        key = get_key(env['PATH_INFO'])
        mckey = 'demowebapp.' + key

        mc = Memcached.new('192.168.11.199:11211')
        begin
            val = mc.get mckey, false
        rescue
            val = nil
        end
        if not val
            pgconn = PGconn.open(:dbname => 'demowebapp', :host => '192.168.11.199')
            res = pgconn.exec("SELECT * from demowebapp_x WHERE id = '#{key}';")
            val = res[0]['content']
            res.clear
            pgconn.finish
            mc.set mckey, val, 1, false
        end

        [200, {'Content-Type' => 'text/html'}, [val]]
    end
end