CrossPHP
  • Namespace
  • Class
  • Download

Namespaces

  • Cross
    • Auth
    • Cache
      • Driver
      • Request
    • Core
    • DB
      • Connecter
      • Drivers
      • SQLAssembler
    • Exception
    • Http
    • I
    • Lib
      • Document
    • Module
    • MVC
    • Runtime
  • None

Classes

  • Cross\Auth\CookieAuth
  • Cross\Auth\SessionAuth
  • Cross\Cache\Driver\FileCacheDriver
  • Cross\Cache\Driver\MemcacheDriver
  • Cross\Cache\Driver\RedisDriver
  • Cross\Cache\Request\FileCache
  • Cross\Cache\Request\Memcache
  • Cross\Cache\Request\RedisCache
  • Cross\Cache\RequestCache
  • Cross\Core\Annotate
  • Cross\Core\Application
  • Cross\Core\ArrayMap
  • Cross\Core\Config
  • Cross\Core\CrossArray
  • Cross\Core\Delegate
  • Cross\Core\FrameBase
  • Cross\Core\Helper
  • Cross\Core\HttpAuth
  • Cross\Core\Loader
  • Cross\Core\Rest
  • Cross\Core\Router
  • Cross\DB\Connecter\BaseConnecter
  • Cross\DB\Connecter\MySQLConnecter
  • Cross\DB\Connecter\PgSQLConnecter
  • Cross\DB\Connecter\SQLiteConnecter
  • Cross\DB\DBFactory
  • Cross\DB\Drivers\CouchDriver
  • Cross\DB\Drivers\MongoDriver
  • Cross\DB\Drivers\PDOSqlDriver
  • Cross\DB\SQLAssembler\MySQLAssembler
  • Cross\DB\SQLAssembler\PgSQLAssembler
  • Cross\DB\SQLAssembler\SQLAssembler
  • Cross\DB\SQLAssembler\SQLiteAssembler
  • Cross\Http\Request
  • Cross\Http\Response
  • Cross\Lib\Array2XML
  • Cross\Lib\Document\CallTree
  • Cross\Lib\Document\CallTreeToHTML
  • Cross\Lib\Document\HTML
  • Cross\Lib\StringToPHPStream
  • Cross\Module\SQLModule
  • Cross\MVC\Controller
  • Cross\MVC\Module
  • Cross\MVC\View
  • Cross\Runtime\ClosureContainer

Interfaces

  • Cross\I\CacheInterface
  • Cross\I\HttpAuthInterface
  • Cross\I\PDOConnecter
  • Cross\I\RequestCacheInterface
  • Cross\I\RouterInterface
  • Cross\I\SqlInterface

Exceptions

  • Cross\Exception\CacheException
  • Cross\Exception\CoreException
  • Cross\Exception\CrossException
  • Cross\Exception\FrontException

Functions

  • ascLogo
  • line
  • tBody
  • th
  • tHead
  1 <?php
  2 /**
  3  * Cross - a micro PHP 5 framework
  4  *
  5  * @link        http://www.crossphp.com
  6  * @license     MIT License
  7  */
  8 
  9 namespace Cross\Cache\Request;
 10 
 11 use Cross\Cache\Driver\FileCacheDriver;
 12 use Cross\Exception\CoreException;
 13 use Cross\I\RequestCacheInterface;
 14 
 15 /**
 16  * @author wonli <wonli@live.com>
 17  *
 18  * Class FileCache
 19  * @package Cross\Cache\Request
 20  */
 21 class FileCache implements RequestCacheInterface
 22 {
 23     /**
 24      * @var string
 25      */
 26     private $cacheKey;
 27 
 28     /**
 29      * @var string
 30      */
 31     private $cachePath;
 32 
 33     /**
 34      * @var array
 35      */
 36     private $config;
 37 
 38     /**
 39      * 缓存过期时间(秒)
 40      *
 41      * @var int
 42      */
 43     private $expireTime = 3600;
 44 
 45     /**
 46      * @var FileCacheDriver
 47      */
 48     private $driver;
 49 
 50     /**
 51      * FileCache constructor.
 52      * 
 53      * @param array $config
 54      * @throws CoreException
 55      */
 56     function __construct(array $config)
 57     {
 58         if (!isset($config['cache_path'])) {
 59             throw new CoreException('请指定缓存目录');
 60         }
 61 
 62         if (!isset($config['key'])) {
 63             throw new CoreException('请指定缓存KEY');
 64         }
 65 
 66         if (isset($config['expire_time'])) {
 67             $this->expireTime = &$config['expire_time'];
 68         }
 69 
 70         $this->cacheKey = &$config['key'];
 71 
 72         $this->cachePath = rtrim($config['cache_path'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
 73         $this->driver = new FileCacheDriver($this->cachePath);
 74     }
 75 
 76     /**
 77      * 写入缓存
 78      *
 79      * @param string $value
 80      * @throws CoreException
 81      */
 82     function set($value)
 83     {
 84         $this->driver->set($this->cacheKey, $value);
 85     }
 86 
 87     /**
 88      * 获取缓存内容
 89      *
 90      * @return mixed get cache
 91      */
 92     function get()
 93     {
 94         return $this->driver->get($this->cacheKey);
 95     }
 96 
 97     /**
 98      * 是否有效
 99      *
100      * @return bool
101      */
102     function isValid()
103     {
104         $cacheFile = $this->cachePath . $this->cacheKey;
105         if (!file_exists($cacheFile)) {
106             return false;
107         }
108 
109         if ((time() - filemtime($cacheFile)) < $this->expireTime) {
110             return true;
111         }
112 
113         return false;
114     }
115 
116     /**
117      * 缓存配置
118      *
119      * @param array $config
120      */
121     function setConfig(array $config = array())
122     {
123         $this->config = $config;
124     }
125 
126     /**
127      * 获取缓存配置
128      *
129      * @return mixed
130      */
131     function getConfig()
132     {
133         return $this->config;
134     }
135 }
136 
CrossPHP API documentation generated by ApiGen