Upgraded to wordpress 3.0

smooth upgrade.
ref:
http://wordpress.org/development/2010/06/thelonious/

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Posted in Default | Tagged | Leave a comment

Peak at C# 4.0: optional parameter, named parameter and method resolution

Optional and named parameter is an awesome feature introduced in C# 4.0. Now C# combines some fancy features from dynamic languages like Python again (var knows why I say again. And the more dynamic dynamic is another topic, LOL).

Please note the “NEW:” comment for optional parameter declaration and named parameter assignment.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var top = new TestOptionalParameter();
double d = top.Demo(0);  // #1

// int d = top.Demo(0, s: “test”); // NEW: named parameter.
}
}

class TestOptionalParameter
{
public int Demo(int i, string s = “demo purpose”) // #2 NEW: optional parameter declaration
{
Console.WriteLine(“int Demo(int i, string s = \”demo purpose\”)”);
return 0;
}

public double Demo(int i) // #3
{
Console.WriteLine(“double Demo(int i)”);
return 0;
}
}
}

But when combined with method name resolution, unconsidered condition may occur.

Try these changes and you will realize what you’ll pay for fancy new features: (Play it with VS2010. I don’t want to copy and paste several times and just modify one place of return type or variable value.)

Change #1 Change #2 Change #3 Why?
Works as expected. Matches 2nd Demo method.
change double to int Easy to understand, the 1st Demo matches better. (Fully match in/out/return parameter, though there’s an optional parameter not specified when invoking the method)
add named parameter. change it to
double d = top.Demo(0, s: “test”);
still the 1st Demo matches better. Now named parameter takes place.
change int to double Match 1st. No implicit conversion required for parameter.
change 0 to 0.0 Compile error. Explicit conversion required.
change 0 to 0.0 change int to double Match 1st. No implicit conversion required for parameter.
Whenever add 2nd string parameter, named with “s” or unnamed. Match 1st.

Some looks-hard-but-actually-straightforward-and-ideal conclusion:

  1. Whenever named or unnamed parameter used, method don’t accept these parameters will be out.
  2. If implicit conversion is NOT required, traditional method resolution goes first, then goes to match methods with optional parameter. (please note there’s some a-bit-evil details to be discussed.)
  3. If implicit conversion is required for parameter or return type, match method with matched implicit parameter first.
  4. When explicit conversion is required or specified error parameter name or value type, compile time error will be triggered.
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Posted in Default | Tagged , | Leave a comment

memcached practice: A weird twitter gadget

memcached is widely used in modern websites like flickr, wikipedia, twitter, youtube, digg, WordPress and a long hot continuing list. In the past years, everyone is talking about XXX 2.0, and recently, yes, you got it, NoSQL….

Here’s a weird twitter gadget, to be used by other pages as gadget. You may notice some bad practice on variable naming, function organizing, but ignore them.
The main purpose is to demonstrate a memcached usage, when combined with HTTP cache control.

source generated by “php -s”


<?php
define
('SECRETSTRING''==='); // base64_encode('user:password')
define('USERTIMELINEURL''https://api.twitter.com/1/statuses/user_timeline.json?count=20');
define('EXPIRESECONDS'300); 

function gettweets()
{
    
$MEMCACHE_SERVERS = array(
        
"127.0.0.1"//localhost
    
);
    
$keyname 'fvn:tweets';

    $mc = new Memcache();
    foreach(
$MEMCACHE_SERVERS as $server){
        
$mc->addServer $server );
    }
    
$ret FALSE;
    if( !(isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE']))) {// force refresh
        
$tweets gettweetsdirectly();
        if (
$mc->get($keyname) === FALSE) {
            
$ret $mc->set($keyname$tweetsMEMCACHE_COMPRESSEDEXPIRESECONDS);
        } else {
            
$ret $mc->replace($keyname$tweetsMEMCACHE_COMPRESSEDEXPIRESECONDS);
        }
    } else {
        
$tweets $mc->get($keyname);
        if (
$tweets === FALSE)
        {
            
$tweets gettweetsdirectly();
            
// cache for 10 minutes
            
$mc->set($keyname$tweetsMEMCACHE_COMPRESSEDEXPIRESECONDS);
        }
    }
    return 
$tweets;
}

function gettweetsdirectly()
{
    
$context stream_context_create(array(
        
'http' => array(
            
'header'  => "Authorization: Basic " SECRETSTRING)
        )
    );
    
$data file_get_contents(USERTIMELINEURLfalse$context);
    
$tweets json_decode($datatrue);
    if (
$tweets == NULL)
        return 
FALSE;
    return 
$tweets;
}

// from http://github.com/dizzytree/PHP-Twitter-Client/blob/master/config.php
function  format_tweet($str)
{
        
$formatted_text preg_replace('/(\b(www\.|http\:\/\/|https\:\/\/)\S+\b)/'"<a target='_blank' href='$1'>$1</a>"$str);
        
$formatted_text preg_replace('/\#(\w+)/'"<a target='_blank' href='https://search.twitter.com/search?q=$1'>#$1</a>"$formatted_text);
        
$formatted_text preg_replace('/\@(\w+)/'"<a target='_blank' href='https://twitter.com/$1'>@$1</a>"$formatted_text);
        return 
$formatted_text;
}

$t gettweets();
if (
$t == FALSE)
{
    
header ('Failed to get tweets.'500);
    exit;
}

//cache control
$lasttime strtotime($t[0]['created_at']);
if (isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lasttime)) {
    
header('Last-Modified: '.gmdate('D, d M Y H:i:s'$lasttime).' GMT'true304);
    exit;
} else {
    
header('Last-Modified: ' gmdate("D, d M Y H:i:s"$lasttime) . ' GMT');
    
header('Expires: ' gmdate("D, d M Y H:i:s"$lasttime EXPIRESECONDS) . ' GMT');
}

//echo tweets
echo '<div class=\'gadget\' id=\'twitter\'>
    <span><a href="https://twitter.com/feuvan">tweets by feuvan</a></span>
    <ul>'
;
$len count($t);
for (
$i 0$i $len$i++)
{
    echo 
'
        <li><span>'
;
    echo 
format_tweet($t[$i]['text']);

    echo '
        </span><hr/></li>'
;
}
echo 
'</ul></div>';

?>


Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Posted in Default | Leave a comment

Upgrade your PHP to 5.3

To ppl who is suffering random crashes of PHP 5.2.6.
There’re several vulnerabilities and suhosin may end your php process.

just one example (search others by yourself):

http://packetstormsecurity.nl/0904-advisories/USN-761-2.txt

Migration guide:
http://www.php.net/manual/en/migration53.php

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Posted in Default | Tagged | Leave a comment

Upgraded to WP 2.9.2

and added WPtouch.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Posted in Default | Tagged , | Leave a comment