Minify_Cache_File::store PHP Method

store() public method

Write data to cache.
public store ( string $id, string $data ) : boolean
$id string cache id (e.g. a filename)
$data string
return boolean success
    public function store($id, $data)
    {
        $flag = $this->locking ? LOCK_EX : null;
        $file = $this->path . '/' . $id;
        if (!@file_put_contents($file, $data, $flag)) {
            $this->logger->warning("Minify_Cache_File: Write failed to '{$file}'");
        }
        // write control
        if ($data !== $this->fetch($id)) {
            @unlink($file);
            $this->logger->warning("Minify_Cache_File: Post-write read failed for '{$file}'");
            return false;
        }
        return true;
    }

Usage Example

function test_Minify_Cache_File()
{
    $data = str_repeat(md5(time()) . 'í', 100);
    // 3400 bytes in UTF-8
    $id = 'Minify_test_cache_noLock';
    $prefix = 'Minify_Cache_File : ';
    $cache = new Minify_Cache_File();
    echo "NOTE: Minify_Cache_File : path is set to: '" . $cache->getPath() . "'.\n";
    assertTrue(true === $cache->store($id, $data), $prefix . 'store');
    assertTrue(countBytes($data) === $cache->getSize($id), $prefix . 'getSize');
    assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), $prefix . 'isValid');
    ob_start();
    $cache->display($id);
    $displayed = ob_get_contents();
    ob_end_clean();
    assertTrue($data === $displayed, $prefix . 'display');
    assertTrue($data === $cache->fetch($id), $prefix . 'fetch');
    // test with locks
    $id = 'Minify_test_cache_withLock';
    $cache = new Minify_Cache_File('', true);
    assertTrue(true === $cache->store($id, $data), $prefix . 'store w/ lock');
    assertTrue(countBytes($data) === $cache->getSize($id), $prefix . 'getSize');
    assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), $prefix . 'isValid');
    ob_start();
    $cache->display($id);
    $displayed = ob_get_contents();
    ob_end_clean();
    assertTrue($data === $displayed, $prefix . 'display w/ lock');
    assertTrue($data === $cache->fetch($id), $prefix . 'fetch w/ lock');
}
All Usage Examples Of Minify_Cache_File::store