画竜点睛を衝く@mapyo

日々やった事をつらつらと書くブログです

PHPのキャッシュライブラリについて調べてみる

この記事は Pepabo Advent Calendar 2015 - Qiita の17日目の記事です。

PHPのキャッシュライブラリを使おうと思った時に、何がいいんだろう?と思っていくつか調べてみました。 ほんとは全部使って自分が書いたサンプルコードを載せたりしたかったけど、時間がかかるので、ざっと調べた内容とか使われ方とか、所感を書いて行こうと思います。 あと、このライブラリは何なのか?という説明が各ライブラリにあると思うんですがその辺を見比べてみると面白いなと思ったのでのせています。

この有名なライブラリがないぞ!などのご意見はあるかもしれませんが、その辺はご了承頂けるとありがたいです。 もっといっぱい調べようと思ったけど途中で挫折したのでそこまで多くは調べてません。。。

phpfastcache

概要

github.com

www.phpfastcache.com

the php high-performance object caching system ever.

  • Star数が1000近くある。
  • PHP5.1以上
  • Redis, Memcached, Fileなどなどのキャッシュに対応してる
  • 老舗っぽい感じ(完全な主観

使い方のサンプル

<?php
/*
 * welcome to learn lesson
 * this is very simple php code of caching
 */

// require library
// keep it auto or setup it as "files","sqlite","wincache" ,"apc","memcache","memcached", "xcache"
require_once("phpfastcache.php");
phpfastcache::setup("storage","auto");
// phpfastcache::setup("storage","files");
// or open phpfastcache.php and edit your config value there

// simple caching with:
$cache = phpfastcache();
// $cache = phpfastcache("redis");

// try to get $products from caching first
// product_page is "identity keyword";
$products = $cache->get("product_page");

if($products == null) {
    $products = "db queries | function_get_products | array | string | objects";
    // write products to cache in 10 minutes with same keyword
    $cache->set("product_page",$products , 600);
}

// use your products here or return it;
echo $products;

GitHubのREADMEから引用させて頂きました。

Stash

概要

github.com

http://www.stashphp.com

stash makes it easy to speed up your code by caching the results of expensive functions or code.

使い方のサンプル

<?php

// get a cache item.
    $item = $pool->getitem('path/to/item');

// attempt to get the data
$data = $item->get();

// check to see if the data was a miss.
if($item->ismiss())
{
    // let other processes know that this one is rebuilding the data.
    $item->lock();

    // run intensive code
    $data = codethattakesalongtime();

    // store the expensive to generate data.
    $item->set($data);
}

// continue as normal.
usedataforstuff($data);

こちらより引用させて頂きました。

doctrine/cache

概要

http://docs.doctrine-project.org/en/latest/reference/caching.html

github.com

doctrine_cache offers an intuitive and easy-to-use query caching solution.

使い方のサンプル

<?php
$memcached = new Memcached();
$memcached->addServer('memcache_host', 11211);

$cacheDriver = new \Doctrine\Common\Cache\MemcachedCache();
$cacheDriver->setMemcached($memcached);
$cacheDriver->save('cache_id', 'my_data');

cakephp/cache

概要

http://book.cakephp.org/3.0/en/core-libraries/caching.html

https://github.com/cakephp/cache

使い方のサンプル

if (($posts = Cache::read('posts')) === false) {
    $posts = $someService->getAllPosts();
    Cache::write('posts', $posts);
}

illuminate/cache

概要

laravel.com

github.com

Laravel provides a unified API for various caching systems.

使い方のサンプル

ドキュメントにいろいろ書いてありますが、以下のような使い方が気に入りました

$value = Cache::remember('users', $minutes, function() {
    return DB::table('users')->get();
});

Cache_Lite

Cache_Lite

Fast and Safe little cache system

  • 昔からあるサービスに入ってそうなpearのライブラリ。
  • ファイルキャッシュのみに対応
  • 直近のリリース2014年5月だった

使い方のサンプルはこちら

<?php

// include the package
require_once('cache/lite.php');

// set a id for this cache
$id = '123';

// set a few options
$options = array(
    'cachedir' => '/tmp/',
    'lifetime' => 3600
);

// create a cache_lite object
$cache_lite = new cache_lite($options);

// test if thereis a valide cache for this id
if ($data = $cache_lite->get($id)) {

    // cache hit !
    // content is in $data
    // (...)

} else { // no valid cache found (you have to make the page)

    // cache miss !
    // put in $data datas to put in cache
    // (...)
    $cache_lite->save($data);

}

?>

こちらのページより引用させて頂きました。

結局、どのライブラリを使うのがよいのか?

迷いますよね。 これについて考える前に、自分はキャッシュを使ってどういう事を実現するのか?を整理することが重要だと思います。そうする事で絞られてくると思います。

もうちょっと一般的な事でいうと、ライブラリを選定する上では以下の点が参考になるかと思います。

  • GitHubのStar数
  • 定期的にメンテナスされているか?
  • プルリクは溜まってないか
  • 有識者に聞く

GithubのStar数についてなのですが、フレームワークで使われているライブラリは、本体から分離されてリポジトリ化されているので、Star数は低めなので注意です。

後は自分が使ってみてテンションが上がるとか、チームメンバーからの賛同を得られそうとか、などですかね。

最後に

いかがでしたでしょうか。キャッシュのライブラリについていろいろ調べた事がなかったので勉強になりました。また、複数のライブラリを見比べていると、このライブラリはwriteというメソッドで値を書き込んでいるけど、このライブラリはsetというメソッド名を使っている。こういう機能が提供されているのいいなぁなどなど。面白い点が多かったです。

いろいろなライブラリを見ていくなかで、いろいろなコードを読んで、勉強になって、プログラマ的なスキルが上がっていくんだろうなぁと思う今日このごろです。