LoginSignup
1
0

More than 5 years have passed since last update.

DoctrineのFilesystemCacheでローカルにキャッシュ

Last updated at Posted at 2016-04-05

Redis とか、memcached とか使うまでもない、ちょちょっとしたキャッシュ用に。

1. インストール

composer.json

{
    "require": {
        "doctrine/cache": "1.6.*"
    }
}
$ composer install

2. キャッシュの保存

<?php

use Doctrine\Common\Cache\FilesystemCache;

$cache  = new FilesystemCache('/tmp/hoge');
$data = [
    'id' => 1,
    'name' => 'John',
    'phone' => '03-0000-2222',
];
$id = "users/1";
$cache->save($id, $data, $lifeTime = 10);

3. キャッシュの取得

<?php

use Doctrine\Common\Cache\FilesystemCache;


$cache_id = "users/1";

$cache  = new FilesystemCache('/tmp/hoge');
$result = $cache->fetch($cache_id);

print_r($result);

Array
(
    [id] => 1
    [name] => John
    [phone] => 03-0000-2222
)

4. キャッシュの削除

$cache->delete($id);

5. 確認

$cache->contains($id);
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0