Google Crisis Response(Google)
災害に関する情報源や、行方不明者情報の収集と検索を行う『パーソンファインダー』を初めとするツールの提供が行われています。

2011年9月25日 日曜日

『希望』

Filed under: 読書中
タグ:,
時間:22時52分
投稿者:よしとも
AddClips 経由でソーシャルブックマークに登録

瀬名秀明氏の初短編集だそうです。『八月の博物館』以来約8年ぶりに読んでいますので、どう変わっただろうという気持ち半分です。収録されているのは次の7作品。

  • 魔法
  • 静かな恋の物語
  • ロボ
  • For a breath I tarry
  • 鶫とひばり
  • 光の栞
  • 希望

私の印象では、氏の作品に対して小説というよりも技術書のようなイメージを抱いています。このイメージはデビュー作の『パラサイト・イヴ』と次作品の『BRAIN VALLEY』から来ているのでしょう。『パラサイト・イヴ』は自身の専門分野が物語の中心にあるということで誤魔化しをしたくなかったのかと思いましたが、『BRAIN VALLEY』でも脳科学・人工知能・臨死体験など徹底的な調査をすることで専門的なデータを下地とした作品に仕上げていました。『八月の博物館』では博物学やエジプト考古学と、文系に変ガラッとわりましたがその徹底した情報収集は健在。

『パラサイト・イヴ』と『BRAIN VALLEY』しか読んだことがないと、氏の小説は専門情報が多すぎて入りづらいと感じるかもしれません。でも、今回の『希望』についてはかなり抑えられているように思えます。私個人としては『ガモフ全集』のようなタイプは望むところかのですが、一般向けではなく読者が絞られてしまう。その点では『希望』は大丈夫そうです。

Comments (0)

2011年9月17日 土曜日

WordPress の Transients API

Filed under: WordPress,ハック,プログラミング
タグ:, , ,
時間:18時29分
投稿者:よしとも
AddClips 経由でソーシャルブックマークに登録

ウェブアプリケーションでは、一定期間状態を保持したいことが多くあります。それを実現するためにさまざまな方法が提案され、利用されてきました。古くはブラウザに保持させる Cookie、サーバーに保存させるセッション。最近では HTML5 の Web Storage があります。

WordPress には、この一定期間保持するという目的にぴったりのAPI(Application Programming Interface)があります。Transients API と呼ばれるもので、指定した秒数が経過するまでデーターベース上に保持してくれるというものです。保存・取り出し・削除の3つの関数で構成されているシンプルなものです。WordPress 2.8.0 から使えるようになったもので、現在ではコアファイルのさまざまなところで使われています。

使い方については他のサイトを見ていただくとして、この記事ではhackネタを書いてみたいと思います。

まずは、ちょっと長いですが関数の定義部分から。引用元は WordPress 3.2.1 の wp-includes/function.php です。

  1. /**
  2.  * Delete a transient
  3.  *
  4.  * @since 2.8.0
  5.  * @package WordPress
  6.  * @subpackage Transient
  7.  *
  8.  * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
  9.  * @uses do_action() Calls 'deleted_transient' hook on success.
  10.  *
  11.  * @param string $transient Transient name. Expected to not be SQL-escaped.
  12.  * @return bool true if successful, false otherwise
  13.  */
  14. function delete_transient( $transient ) {
  15.     global $_wp_using_ext_object_cache;
  16.  
  17.     do_action( 'delete_transient_' . $transient, $transient );
  18.  
  19.     if ( $_wp_using_ext_object_cache ) {
  20.         $result = wp_cache_delete( $transient, 'transient' );
  21.     } else {
  22.         $option_timeout = '_transient_timeout_' . $transient;
  23.         $option = '_transient_' . $transient;
  24.         $result = delete_option( $option );
  25.         if ( $result )
  26.             delete_option( $option_timeout );
  27.     }
  28.  
  29.     if ( $result )
  30.         do_action( 'deleted_transient', $transient );
  31.     return $result;
  32. }
  1. /**
  2.  * Get the value of a transient
  3.  *
  4.  * If the transient does not exist or does not have a value, then the return value
  5.  * will be false.
  6.  *
  7.  * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
  8.  *  Any value other than false will "short-circuit" the retrieval of the transient
  9.  *  and return the returned value.
  10.  * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
  11.  *  the transient value.
  12.  *
  13.  * @since 2.8.0
  14.  * @package WordPress
  15.  * @subpackage Transient
  16.  *
  17.  * @param string $transient Transient name. Expected to not be SQL-escaped
  18.  * @return mixed Value of transient
  19.  */
  20. function get_transient( $transient ) {
  21.     global $_wp_using_ext_object_cache;
  22.  
  23.     $pre = apply_filters( 'pre_transient_' . $transient, false );
  24.     if ( false !== $pre )
  25.         return $pre;
  26.  
  27.     if ( $_wp_using_ext_object_cache ) {
  28.         $value = wp_cache_get( $transient, 'transient' );
  29.     } else {
  30.         $transient_option = '_transient_' . $transient;
  31.         if ( ! defined( 'WP_INSTALLING' ) ) {
  32.             // If option is not in alloptions, it is not autoloaded and thus has a timeout
  33.             $alloptions = wp_load_alloptions();
  34.             if ( !isset( $alloptions[$transient_option] ) ) {
  35.                 $transient_timeout = '_transient_timeout_' . $transient;
  36.                 if ( get_option( $transient_timeout ) < time() ) {
  37.                     delete_option( $transient_option  );
  38.                     delete_option( $transient_timeout );
  39.                     return false;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         $value = get_option( $transient_option );
  45.     }
  46.  
  47.     return apply_filters( 'transient_' . $transient, $value );
  48. }
  1. /**
  2.  * Set/update the value of a transient
  3.  *
  4.  * You do not need to serialize values. If the value needs to be serialized, then
  5.  * it will be serialized before it is set.
  6.  *
  7.  * @since 2.8.0
  8.  * @package WordPress
  9.  * @subpackage Transient
  10.  *
  11.  * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
  12.  *  transient value to be stored.
  13.  * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
  14.  *
  15.  * @param string $transient Transient name. Expected to not be SQL-escaped.
  16.  * @param mixed $value Transient value. Expected to not be SQL-escaped.
  17.  * @param int $expiration Time until expiration in seconds, default 0
  18.  * @return bool False if value was not set and true if value was set.
  19.  */
  20. function set_transient( $transient, $value, $expiration = 0 ) {
  21.     global $_wp_using_ext_object_cache;
  22.  
  23.     $value = apply_filters( 'pre_set_transient_' . $transient, $value );
  24.  
  25.     if ( $_wp_using_ext_object_cache ) {
  26.         $result = wp_cache_set( $transient, $value, 'transient', $expiration );
  27.     } else {
  28.         $transient_timeout = '_transient_timeout_' . $transient;
  29.         $transient = '_transient_' . $transient;
  30.         if ( false === get_option( $transient ) ) {
  31.             $autoload = 'yes';
  32.             if ( $expiration ) {
  33.                 $autoload = 'no';
  34.                 add_option( $transient_timeout, time() + $expiration, '', 'no' );
  35.             }
  36.             $result = add_option( $transient, $value, '', $autoload );
  37.         } else {
  38.             if ( $expiration )
  39.                 update_option( $transient_timeout, time() + $expiration );
  40.             $result = update_option( $transient, $value );
  41.         }
  42.     }
  43.     if ( $result ) {
  44.         do_action( 'set_transient_' . $transient );
  45.         do_action( 'setted_transient', $transient );
  46.     }
  47.     return $result;
  48. }

基本的には、2つの項目をセットにしてオプションとして保存しています。項目はデータ自体となる _transient_識別子文字列 と、保持期限となる _transient_timeout_識別子文字列

識別子文字列と値に対してのサニタイズやシリアライズ処理は関数が面倒を見てくれますが、識別子文字列が長い場合は注意が必要です。テーブルのカラムから来る制限で45文字を超えることが出来ないのですが、サニタイズによってこの長さを超えてしまう可能性があります。半角英数を使うのが無難でしょう。

取り出すときには有効期限ないかどうかを確認し(756行目)、期限を過ぎている場合には削除もしてくれます(757・758行目)。Codex を見る限りでは何もしなくても期限が過ぎれば削除してくれるようですが、相当する処理は見つけられませんでした。

オブジェクトキャッシュが有効になっている場合には、データーベースの変わりにそちらを使うので更なる高速化が期待できます。

拙作プラグインの AmazonLink でも試験的に導入してみました。自前で行っていた処理がほとんどいらなくなったおかげで、プログラムがかなり簡略化できています。今のところ問題も起きていないので、このまま次のリリースに入れる予定です。2.8.0以上という条件はありますが、これはお勧めです。

Comments (2)

HTML convert time: 2.303 sec. Powered by

Images is enhanced with WordPress Lightbox JS by Zeo