Apr 17, 2014

JS OOP with publish/subscribe custom events using jQuery

As the Javascript script grows, traditional programming style will result in messy code. With the help of custom event and OOP, we can keep the code clean, easy to extends and maintain. Below is the snippet comes from my lesson:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
// For testing
function puts(text) {
    document.write("<br>" + text);
}

// Thanks to http://javascript.info/tutorial/all-one-constructor-pattern
function BaseClass() {
 // Private, invisible to child class and outside
 var x;

 // In lost context, using "self" to reference
 var self = this;

 // Public
 this.a = 'a freely property';

 // Public access to restricted var
 this.propertyX = function(_x) {
  if (typeof _x != 'undefined')  x = _x;
  return x;
 }
 this.actionY = function(message) {
  // Bla bla
  // "Publish" an event, pass params as an array
  $(self).triggerHandler('custom_event', [message]);
 }
 $(self).on('custom_event', function(event, message) {
  puts("Base: " + message);
 });
}

function ChildClass() {
 // Inherit, able to have many parents
 BaseClass.apply(this);
 var self = this;
 // To call parent implementation, keep a reference to it
 var parent = { actionY: this.actionY }

 this.actionY = function(message) {
  // Super call, note the params as an array
  parent.actionY.apply(this, [message]);
  // Customize here ...
 }

 this.actionZ = function() { /* Other.. */ }

 $(self).on('custom_event', function(event, message) {
  puts("Extended: " + message);
  puts("With access to: " + self.a);
 });
}

// Now it's time to play with class
c = new ChildClass();
c.propertyX(10);
puts(c.propertyX());
c.actionY('JS OOP is tricky');
</script>

Apr 16, 2014

Snippets for backup/deploy Postgres DB

Create dump or schema definition:
pg_dump --no-owner --no-acl -Fc db > db.dump
pg_dump --no-owner --no-acl --schema-only db > db.sql
Restore, deploy:
pg_restore -Fc -C db.dump | psql

Apr 5, 2014

Server-level cache

We often use "plugins" like "WP Super Cache"... to have our contents using less server resources.
We also have to write various PHP-level code to having such functionality.

However, there is an alternative way to do it *automatically*, without having to repeatedly writing code to cache our content at PHP-level.

We care about 4 aspects of caching:
1-Where to cache (often files)
2-How long it cached (says 5 min or 30days, depend on how freshness the website)
3-Bypass for dynamic sections (login, admin, realtime...)
4-Clear cache

Assuming we using Nginx and passing request to backend PHP using socket, typically we having:

proxy_pass http://127.0.0.1:9000

We added something like:
http {
   proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=zone-a:8m max_size=1000m inactive=600m;
}
server {
  proxy_pass http://127.0.0.1:9000
  proxy_cache_key $proxy_host$request_uri$is_args$args;
  proxy_cache_valid 200 302 5m;
  proxy_cache zone-a;
}

proxy_cache_path defines place to put our cache data, naming "zone". We have control over its size, how to creating sub- directories (levels=1:2 )

proxy_cache specifies which zone to store cache.

proxy_cache_key define the pattern to cache. We may need to add how to know that the page is dynamic generated, such as cookie. Or we may use proxy_cache_bypass to tell, example, wp-admin will be bypassed.

set $wp_bypass 0;
if ($request_uri ~ "wp-admin") {
    set $wp_bypass 1;
}
proxy_cache_bypass $wp_bypass;
To clear cache, simply deletes files under zone directory!

Apache also having similar features, and if we need more flexible, a mature cache engine like Varnish may help us. Speed up and concentrate more on development!