SQLITE cache-PHP source code

Source: Internet
Author: User
SQLITE cache

Complete class source code
 */Class Sqlite {static protected $ handler; protected $ options = array ('table' => 'icaches '); /*** architecture function ** @ access public */public function _ construct ($ options = array ()) {$ this-> options ['prefix'] = isset ($ options ['prefix'])? $ Options ['prefix']: C ('data _ cache_prefix', null ,''); $ this-> options ['expire '] = isset ($ options ['expire'])? $ Options ['expire ']: C ('data _ CACHE_TIME', null, 36000 ); $ this-> options ['nowtime'] = isset ($ GLOBALS ['_ inintime'])? $ GLOBALS ['_ inintime']: microtime (true); $ dbFile = TEMP_PATH. 'caches. tmp '; $ isCreate = is_file ($ dbFile); if (empty (static ::$ handler) {static ::$ handler = new PDO ("sqlite: {$ dbFile} ", null, null, array (PDO: ATTR_PERSISTENT => true); empty ($ isCreate) & $ this-> exec ("PRAGMA encoding = 'utf8'; PRAGMA temp_store = 2; PRAGMA auto_vacuum = 0; PRAGMA count_changes = 1; PRAGMA cache_size = 9000 ;"); $ this-> chkTab Le () | $ this-> createTable () ;}} public function _ destruct () {return $ this-> exec ("delete from '{$ this-> options ['table']} 'Where 'expire' <strftime ('% s ', 'right'); VACUUM; ");} public function _ call ($ method, $ arguments) {if (method_exists (self ::$ handler, $ method )) {return call_user_func_array (array (self: $ handler, $ method), $ arguments);} else {E (_ CLASS __. ':'. $ method. L ('_ METHOD_NOT_EXIST _'); retur N ;}/ *** read cache * @ access public * @ param string $ name cache variable name * @ return mixed */public function get ($ name) {$ id = $ this-> getName ($ name); $…… static :: $ handler-> query ("SELECT 'value' FROM '{$ this-> options ['table']} 'Where 'id' =' {$ id} 'AND 'expire '> strftime (' % s ', 'Now ') LIMIT 1 ", PDO: FETCH_NUM); if (! Empty ($ Something) {N ('cache _ read', 1); list ($ data) = $ Something-> fetch (); return unserialize ($ data );} else {return false ;}} /*** write cache ** @ access public * @ param string $ name cache variable name * @ param mixed $ value data storage * @ param int $ expire valid time 0 is permanent *@ return boolean */public function set ($ name, $ value, $ expire = 0) {N ('cache _ write', 1); $ data = serialize ($ value ); if ($ expire <0 | $ expire = false) {return true;} elseif (I S_null ($ value) | $ value = false) {return $ this-> rm ($ name);} elseif ($ expire <1) {$ expire = 315360000;} $ id = $ this-> getName ($ name ); return $ this-> exec ("replace into '{$ this-> options ['table']} 'values (' {$ id} ',' {$ data }', {$ expire} + strftime ('% s', 'right '))");} /*** delete cache ** @ access public * @ param string $ name cache variable name * @ return boolean */public function rm ($ name) {$ id = $ this-> getName ($ name); return $ this-> Exec ("delete from '{$ this-> options ['table']} 'Where 'id' =' {$ id }'");} /*** clear cache ** @ access public * @ param string $ name cache variable name * @ return boolean */public function clear () {return $ this-> exec ("delete from '{$ this-> options ['table']}';");} /*** check whether the current table exists * @ return bool return the check result. If yes, True is returned. If no result exists, False is returned */protected function chkTable () is returned () {return in_array ($ this-> options ['table'], $ this-> getTables ();}/*** get the current database * @ Return array returns the list of Retrieved data tables */protected function getTables () {$ tables = $ data = array (); $…… = $ this-> query ("SELECT 'name' FROM 'sqlite _ Master' WHERE 'type' = 'table' union all select 'name' FROM 'sqlite _ temp_master" '); if (! Empty ($ Something) {while ($ row = $ Something-> fetch (PDO: FETCH_NUM, PDO: FETCH_ORI_NEXT )) {$ tables [] = $ row [0];} unset ($ TH, $ row);} return $ tables ;} /*** create the current data table * @ return integer success return 1, failure return 0 */protected function createTable () {return $ this-> exec ("create table if not exists '{$ this-> options ['table']}' ('id' VARCHAR PRIMARY KEY ON CONFLICT FAIL NOT NULL COLLATE 'nocase ', 'value' text not null, 'expire 'INTEGER NOT NULL); ");}/*** get cache name * @ param string $ name * @ return string */protected function getName ($ name) {if (! Is_string ($ name )&&! Is_numeric ($ name) {$ name = md5 (serialize ($ name);} return $ this-> options ['prefix']. $ name ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.