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

85 lines
1.5 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel\Cache;
class File extends Driver {
/**
* The file engine instance.
*
2011-08-30 22:35:32 -05:00
* @var Laravel\File
*/
private $file;
2011-08-26 21:42:04 -05:00
/**
* The path to which the cache files should be written.
*
* @var string
*/
private $path;
/**
* Create a new File cache driver instance.
*
2011-08-30 22:35:32 -05:00
* @param Laravel\File $file
* @param string $path
* @return void
*/
2011-08-30 22:35:32 -05:00
public function __construct(\Laravel\File $file, $path)
{
$this->file = $file;
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
{
2011-08-26 21:42:04 -05:00
if ( ! $this->file->exists($this->path.$key)) return null;
2011-08-18 19:56:29 -05:00
2011-08-26 21:42:04 -05:00
if (time() >= substr($cache = $this->file->get($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.
*
* @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)
{
2011-08-26 21:42:04 -05:00
$this->file->put($this->path.$key, (time() + ($minutes * 60)).serialize($value));
2011-08-18 19:56:29 -05:00
}
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)
{
2011-08-26 21:42:04 -05:00
$this->file->delete($this->path.$key);
2011-08-18 19:56:29 -05:00
}
}