Files
spa-api/laravel/cache/drivers/file.php

100 lines
1.9 KiB
PHP
Raw Normal View History

2011-10-12 21:55:05 -05:00
<?php namespace Laravel\Cache\Drivers;
2011-08-18 19:56:29 -05:00
class File extends Driver {
2011-08-26 21:42:04 -05:00
/**
* The path to which the cache files should be written.
*
* @var string
*/
2011-10-12 21:55:05 -05:00
protected $path;
2011-08-26 21:42:04 -05:00
/**
* Create a new File cache driver instance.
*
* @param string $path
* @return void
*/
public function __construct($path)
{
2011-08-26 21:42:04 -05:00
$this->path = $path;
}
2011-08-19 20:12:39 -05:00
/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return bool
*/
2011-08-18 19:56:29 -05:00
public function has($key)
{
return ( ! is_null($this->get($key)));
}
2011-08-19 20:12:39 -05:00
/**
* Retrieve an item from the cache driver.
*
* @param string $key
* @return mixed
*/
protected function retrieve($key)
2011-08-18 19:56:29 -05:00
{
if ( ! file_exists($this->path.$key)) return null;
2011-08-18 19:56:29 -05:00
// File based caches store have the expiration timestamp stored in
2012-04-05 21:30:53 -05:00
// UNIX format prepended to their contents. We'll compare the
// timestamp to the current time when we read the file.
if (time() >= substr($cache = file_get_contents($this->path.$key), 0, 10))
2011-08-18 19:56:29 -05:00
{
2011-08-19 20:12:39 -05:00
return $this->forget($key);
2011-08-18 19:56:29 -05:00
}
2011-08-19 20:12:39 -05:00
return unserialize(substr($cache, 10));
2011-08-18 19:56:29 -05:00
}
2011-08-19 20:12:39 -05:00
/**
* Write an item to the cache for a given number of minutes.
*
2011-10-12 21:55:05 -05:00
* <code>
* // Put an item in the cache for 15 minutes
* Cache::put('name', 'Taylor', 15);
* </code>
*
2011-08-19 20:12:39 -05:00
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
2011-08-18 19:56:29 -05:00
public function put($key, $value, $minutes)
{
if ($minutes <= 0) return;
2012-01-16 13:59:24 -06:00
$value = $this->expiration($minutes).serialize($value);
2011-10-26 21:21:31 -05:00
file_put_contents($this->path.$key, $value, LOCK_EX);
2011-08-18 19:56:29 -05:00
}
/**
* Write an item to the cache for five years.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function forever($key, $value)
{
return $this->put($key, $value, 2628000);
}
2011-08-19 20:12:39 -05:00
/**
* Delete an item from the cache.
*
* @param string $key
* @return void
*/
2011-08-18 19:56:29 -05:00
public function forget($key)
{
2012-01-16 13:59:24 -06:00
if (file_exists($this->path.$key)) @unlink($this->path.$key);
2011-08-18 19:56:29 -05:00
}
}