Example #1
0
 public function __construct()
 {
     if (self::$cache_dir == null) {
         self::$cache_dir = APPPATH . "../cache/filecache/";
         self::$lifetime = 24 * 3 * 3600;
     }
 }
 /**
  * PHP5-style constructor
  *
  * @param array   $config
  */
 function __construct($config = array())
 {
     parent::__construct($config);
     $this->_expire = isset($config['expire']) ? (int) $config['expire'] : 0;
     if (!$this->_expire || $this->_expire > W3TC_CACHE_FILE_EXPIRE_MAX) {
         $this->_expire = W3TC_CACHE_FILE_EXPIRE_MAX;
     }
 }
Example #3
0
 /**
  * Overwritten I do not want to throw an exception...just ignore it! return default and delete.
  * Retrieve a cached value entry by id. 
  *
  *     // Retrieve cache entry from file group
  *     $data = Cache::instance('file')->get('foo');
  *
  *     // Retrieve cache entry from file group and return 'bar' if miss
  *     $data = Cache::instance('file')->get('foo', 'bar');
  *
  * @param   string   $id       id of cache to entry
  * @param   string   $default  default value to return if cache miss
  * @return  mixed
  * @throws  Cache_Exception
  */
 public function get($id, $default = NULL)
 {
     $filename = Cache_File::filename($this->_sanitize_id($id));
     $directory = $this->_resolve_directory($filename);
     // Wrap operations in try/catch to return default
     try {
         // Open file
         $file = new SplFileInfo($directory . $filename);
         // If file does not exist
         if (!$file->isFile()) {
             // Return default value
             return $default;
         } else {
             // Open the file and parse data
             $created = $file->getMTime();
             $data = $file->openFile();
             $lifetime = $data->fgets();
             // If we're at the EOF at this point, corrupted!
             if ($data->eof()) {
                 $this->_delete_file($file, NULL, TRUE);
                 return $default;
             }
             $cache = '';
             while ($data->eof() === FALSE) {
                 $cache .= $data->fgets();
             }
             // Test the expiry
             if ($created + (int) $lifetime < time()) {
                 // Delete the file
                 $this->_delete_file($file, NULL, TRUE);
                 return $default;
             } else {
                 return unserialize($cache);
             }
         }
     } catch (ErrorException $e) {
         $this->_delete_file($file, NULL, TRUE);
         return $default;
     }
 }
Example #4
0
 /**
  * Delete a cache entry based on id
  * 
  *     // Delete 'foo' entry from the file group
  *     Cache::instance('file')->delete('foo');
  *
  * @param   string   id to remove from cache
  * @return  boolean
  */
 public function delete($id)
 {
     $filename = Cache_File::filename($this->_sanitize_id($id));
     $directory = $this->_resolve_directory($filename);
     return $this->_delete_file(new SplFileInfo($directory . $filename), NULL, TRUE);
 }
Example #5
0
<?php

require_once "Cache_File.inc";
$cache = new Cache_File("second.cache", 10);
if ($text = $cache->get()) {
    print $text;
    exit;
}
$cache->begin();
?>
<html>
<body>
<!-- Cacheable for a day -->
Today is <?php 
echo strftime("%A, %B %e %Y %H:%M:%S");
?>
 
</body>
</html>
<?php 
$cache->end();
Example #6
0
 /**
  * 文件修改时间
  * @param $pKey
  * @return int
  */
 static function mtime($pKey)
 {
     $instance =& Cache_File::instance();
     return is_file($instance->path . $pKey) ? filemtime($instance->path . $pKey) : 0;
 }
Example #7
0
 /**
  * Delete a cache entry based on id
  * 
  *     // Delete 'foo' entry from the file group
  *     Cache::instance('file')->delete('foo');
  *
  * @param   string   id to remove from cache
  * @return  boolean
  */
 public function delete($id)
 {
     // for ProfilerToolbar
     ProfilerToolbar::cacheLog('del', array_search($this, Cache::$instances), $id);
     // /for ProfilerToolbar
     $filename = Cache_File::filename($this->_sanitize_id($id));
     $directory = $this->_resolve_directory($filename);
     return $this->_delete_file(new SplFileInfo($directory . $filename), NULL, TRUE);
 }
 /**
  * Set a value to cache with id and lifetime
  *
  *     $data = 'bar';
  *
  *     // Set 'bar' to 'foo' in file group, using default expiry
  *     Cache::instance('file')->set('foo', $data);
  *
  *     // Set 'bar' to 'foo' in file group for 30 seconds
  *     Cache::instance('file')->set('foo', $data, 30);
  *
  * @param   string   $id        id of cache entry
  * @param   string   $data      data to set to cache
  * @param   integer  $lifetime  lifetime in seconds
  * @return  boolean
  */
 public function set($id, $data, $lifetime = NULL)
 {
     // dont set values when caching is disabled
     if (Arr::get($this->_config, 'disabled', FALSE)) {
         return FALSE;
     }
     $filename = Cache_File::filename($this->_sanitize_id($id));
     $directory = $this->_resolve_directory($filename);
     // If lifetime is NULL
     if ($lifetime === NULL) {
         // Set to the default expiry
         $lifetime = Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
     }
     // Open directory
     $dir = new SplFileInfo($directory);
     // If the directory path is not a directory
     if (!$dir->isDir()) {
         // Create the directory
         if (!mkdir($directory, 0777, TRUE)) {
             // throw new Cache_Exception(__METHOD__.' unable to create directory : :directory', array(':directory' => $directory));
         }
         // chmod to solve potential umask issues
         chmod($directory, 0777);
     }
     // Open file to inspect
     $resouce = new SplFileInfo($directory . $filename);
     $file = $resouce->openFile('w');
     try {
         $data = $lifetime . "\n" . serialize($data);
         $file->fwrite($data, strlen($data));
         return (bool) $file->fflush();
     } catch (ErrorException $e) {
         // If serialize through an error exception
         if ($e->getCode() === E_NOTICE) {
             // Throw a caching error
             // throw new Cache_Exception(__METHOD__.' failed to serialize data for caching with message : '.$e->getMessage());
         }
         // Else rethrow the error exception
         // throw $e;
     }
 }
Example #9
0
 public function wxAccessToken($appId = NULL, $appSecret = NULL)
 {
     $appId = is_null($appId) ? $this->appId : $appId;
     $appSecret = is_null($appSecret) ? $this->appSecret : $appSecret;
     $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
     $tKey = md5($url);
     $access_token = Cache_File::get($tKey, 3600);
     if (!empty($access_token)) {
         return $access_token;
     }
     $result = $this->wxHttpsRequest($url);
     //print_r($result);
     $jsoninfo = json_decode($result, true);
     $access_token = $jsoninfo["access_token"];
     Cache_File::set($tKey, $access_token);
     return $access_token;
 }
Example #10
0
 /**
  * 单例模式
  *
  * 用于本类的单例模式(singleton)实例化
  *
  * @access public
  * @return object
  */
 public static function getInstance()
 {
     if (!self::$_instance) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }