Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Cache/FileCache.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan\Cache;
 12: 
 13: use Kotchasan\Cache\CacheItem as Item;
 14: use Kotchasan\File;
 15: use Psr\Cache\CacheItemInterface;
 16: 
 17: /**
 18:  * Filesystem cache driver.
 19:  *
 20:  * @author Goragod Wiriya <admin@goragod.com>
 21:  *
 22:  * @since 1.0
 23:  */
 24: class FileCache extends Cache
 25: {
 26:     /**
 27:      * ไดเร็คทอรี่แคช.
 28:      *
 29:      * @var string /root/to/dir/cache/
 30:      */
 31:     protected $cache_dir = null;
 32:     /**
 33:      * อายุของแคช (วินาที) 0 หมายถึงไม่มีการแคช.
 34:      *
 35:      * @var int
 36:      */
 37:     protected $cache_expire = 0;
 38: 
 39:     /**
 40:      * class constructor.
 41:      *
 42:      * @throws Exception ถ้าไม่สามารถสร้างแคชได้
 43:      */
 44:     public function __construct()
 45:     {
 46:         $this->cache_expire = self::$cfg->get('cache_expire', 0);
 47:         if (!empty($this->cache_expire)) {
 48:             //  cache directory
 49:             $this->cache_dir = ROOT_PATH.'datas/cache/';
 50:             if (!File::makeDirectory($this->cache_dir)) {
 51:                 throw new \Exception('Folder '.str_replace(ROOT_PATH, '', $this->cache_dir).' cannot be created.');
 52:             }
 53:             // clear old cache every day
 54:             $d = is_file($this->cache_dir.'index.php') ? (int) file_get_contents($this->cache_dir.'index.php') : 0;
 55:             if ($d != (int) date('d')) {
 56:                 $this->clear();
 57:                 $f = @fopen($this->cache_dir.'index.php', 'wb');
 58:                 if ($f === false) {
 59:                     throw new \Exception('File '.str_replace(ROOT_PATH, '', $this->cache_dir).'index.php cannot be written.');
 60:                 } else {
 61:                     fwrite($f, date('d-m-Y H:i:s'));
 62:                     fclose($f);
 63:                 }
 64:             }
 65:         }
 66:     }
 67: 
 68:     /**
 69:      * เคลียร์แคช
 70:      * คืนค่า true ถ้าลบเรียบร้อย, หรือ false ถ้าไม่สำเร็จ.
 71:      *
 72:      * @return bool
 73:      */
 74:     public function clear()
 75:     {
 76:         $error = array();
 77:         if ($this->cache_dir && !empty($this->cache_expire)) {
 78:             $this->clearCache($this->cache_dir, $error);
 79:         }
 80: 
 81:         return empty($error) ? true : false;
 82:     }
 83: 
 84:     /**
 85:      * ลบแคชหลายๆรายการ
 86:      * คืนค่า true ถ้าสำเร็จ, false ถ้าไม่สำเร็จ.
 87:      *
 88:      * @param array $keys
 89:      *
 90:      * @return bool
 91:      */
 92:     public function deleteItems(array $keys)
 93:     {
 94:         if ($this->cache_dir) {
 95:             foreach ($keys as $key) {
 96:                 @unlink($this->fetchStreamUri($key));
 97:             }
 98:         }
 99: 
100:         return true;
101:     }
102: 
103:     /**
104:      * อ่านแคชหลายรายการ.
105:      *
106:      * @param array $keys
107:      *
108:      * @return array
109:      */
110:     public function getItems(array $keys = array())
111:     {
112:         $resuts = array();
113:         foreach ($keys as $key) {
114:             $file = $this->fetchStreamUri($key);
115:             if ($this->isExpired($file)) {
116:                 $item = new Item($key);
117:                 $resuts[$key] = $item->set(unserialize(preg_replace('/^<\?php\sexit\?>/', '', file_get_contents($file), 1)));
118:             }
119:         }
120: 
121:         return $resuts;
122:     }
123: 
124:     /**
125:      * ตรวจสอบแคช
126:      * คืนค่า true ถ้ามี.
127:      *
128:      * @param string $key
129:      *
130:      * @return bool
131:      */
132:     public function hasItem($key)
133:     {
134:         return $this->isExpired($this->fetchStreamUri($key));
135:     }
136: 
137:     /**
138:      * บันทึกแคช
139:      * สำเร็จคืนค่า true ไม่สำเร็จคืนค่า false.
140:      *
141:      * @param CacheItemInterface $item
142:      *
143:      * @throws Exception ถ้าไม่สามารถสร้างแคชได้
144:      *
145:      * @return bool
146:      */
147:     public function save(CacheItemInterface $item)
148:     {
149:         if ($this->cache_dir && !empty($this->cache_expire)) {
150:             $f = @fopen($this->fetchStreamUri($item->getKey()), 'wb');
151:             if (!$f) {
152:                 throw new \Exception('resource cache file cannot be created.');
153:             } else {
154:                 fwrite($f, '<?php exit?>'.serialize($item->get()));
155:                 fclose($f);
156: 
157:                 return true;
158:             }
159:         }
160: 
161:         return false;
162:     }
163: 
164:     /**
165:      * ลบไฟล์ทั้งหมดในไดเร็คทอรี่ (cache).
166:      *
167:      * @param string $dir
168:      * @param array  $error เก็บรายชื่อไฟล์ที่ไม่สามารถลบได้
169:      */
170:     private function clearCache($dir, &$error)
171:     {
172:         $f = @opendir($dir);
173:         if ($f) {
174:             while (false !== ($text = readdir($f))) {
175:                 if ($text != '.' && $text != '..' && $text != 'index.php') {
176:                     if (is_dir($dir.$text)) {
177:                         $this->clearCache($dir.$text.'/', $error);
178:                     } elseif (is_file($dir.$text)) {
179:                         if (@unlink($dir.$text) === false) {
180:                             $error[] = $dir.$text;
181:                         }
182:                     }
183:                 }
184:             }
185:             closedir($f);
186:         }
187:     }
188: 
189:     /**
190:      * อ่านค่า full path ของไฟล์แคช.
191:      *
192:      * @param string $key
193:      *
194:      * @return string
195:      */
196:     private function fetchStreamUri($key)
197:     {
198:         return $this->cache_dir.md5($key).'.php';
199:     }
200: 
201:     /**
202:      * ตรวจสอบวันหมดอายุของไฟล์แคช
203:      * คืนค่า true ถ้าแคชสามารถใช้งานได้.
204:      *
205:      * @param string $file
206:      *
207:      * @return bool
208:      */
209:     private function isExpired($file)
210:     {
211:         if ($this->cache_dir && !empty($this->cache_expire)) {
212:             return file_exists($file) && time() - filemtime($file) < $this->cache_expire;
213:         }
214: 
215:         return false;
216:     }
217: }
218: 
Kotchasan API documentation generated by ApiGen