100% concurrent errors, PHP efficient text cache class. code
Time = time ();} public function read ($ key, $ keep_time = 0) {# obtain the first row. determine the Expiration Time, does not exist, and the exception affects the return value. the return value determines the call of the write method. $ keep_time = 0 & $ keep_time = $ this-> keep_time; $ list = $ this->__ get_one (); # whether to update and judge. $ check = true; if (! $ List [$ key]) $ check = false; if ($ check & (time ()-$ list [$ key] ['t']) >=$ keep_time) $ check = false; # obtain the row data. $ line = $ list [$ key] ['L'] + 0; $ I = 1; $ data = ''; if ($ this-> handle) {while (! Feof ($ this-> handle) {if ($ I ===$ line) {$ data = fgets ($ this-> handle);} else {// TODO: is there any way to skip this step... fgets ($ this-> handle) ;}$ I ++ ;}# ensure that data is returned as an array in any case. if ($ data) {$ data = $ this->__ data_parse ($ data, 'decode ');}! $ Data & $ data = array (); return $ data;} public function write ($ key, $ val) {# obtain the first row. determine whether the key already exists .. $ list = $ this->__ get_one (); # under what conditions can be written. if ($ list [$ key]) {# expired. after the write function is called, it is regarded as expired in any case. $ list [$ key] ['t'] = $ this-> time; $ list ['end'] = $ list [$ key] ['L'];} else {$ list [$ key] = array ('t' => $ this-> time, 'L' => ($ list ['end'] + 1 )); $ list ['end'] = $ list [$ key] ['L'];} # data is encrypted before being passed to _ write. $ list ['end'] indicates update Which row. return $ this->__ write ($ list ['end'], $ this->__ data_parse ($ val), serialize ($ list ));} private function _ get_one () {$ this->__ read_fopens (); if (! $ This-> handle) return array (); $ list = array (); if ($ seria = rtrim (fgets ($ this-> handle ))) {$ list = unserialize ($ seria); unset ($ seria );}! $ List & $ list = array (); return $ list;} private function _ data_parse ($ data, $ cls = 'encoding ') {# $ State parameter to reduce is_string and count calls. if ($ cls = 'encoding') {# Be sure to return a line without line breaks. remember # gzcompress occupies a very high percentage of memory, only executed during write. $ data = base64_encode (gzcompress (serialize ($ data), 9);} else {$ data = unserialize (gzuncompress (base64_decode (rtrim ($ data )))); if ($ State = false & count ($ data) = 1 & isset ($ data [0]) = true ){ $ Data = $ data [0];} return $ data;} private function _ write ($ line, $ data, $ firstline) {$ savedata = array (); $ line + = 0; $ I = 1; $ savedata [0] = $ firstline; if ($ this-> handle) {while (! Feof ($ this-> handle) {$ savedata [$ I] = rtrim (fgets ($ this-> handle); if ($ I ===$ line) {$ savedata [$ I] = $ data ;}$ I ++ ;}} if (! $ Savedata [$ line]) $ savedata [$ line] = $ data; if ($ this-> handle) $ this->__ closes (); # re-write the file. if (! $ Fp = fopen ($ this-> file_path, 'WB ') if (! $ Fp = fopen ($ this-> file_path, 'WB ') if (! $ Fp = fopen ($ this-> file_path, 'WB ') if (! $ Fp = fopen ($ this-> file_path, 'WB ') if (! $ Fp = fopen ($ this-> file_path, 'WB ') return 0; flock ($ fp, LOCK_EX | LOCK_NB); $ ints = 0; // $ ints = fwrite ($ fp, implode (PHP_EOL, $ savedata); foreach ($ savedata AS $ key => $ val) {if ($ key = 0) {$ ints + = fwrite ($ fp, $ val);} else {$ ints + = fwrite ($ fp, PHP_EOL. $ val);} if ($ key = 0 & $ ints <= 0) break;} unset ($ savedata); flock ($ fp, LOCK_UN ); fclose ($ fp); return $ ints;} private function _ read_fopens () {$ Cls = 'RB'; if ($ this-> handle) $ this->__ closes (); if (is_file ($ this-> file_path) === false) return false; if (! $ This-> handle = fopen ($ this-> file_path, $ cls) if (! $ This-> handle = fopen ($ this-> file_path, $ cls) if (! $ This-> handle = fopen ($ this-> file_path, $ cls) if (! $ This-> handle = fopen ($ this-> file_path, $ cls) if (! $ This-> handle = fopen ($ this-> file_path, $ cls) $ this-> handle = null; if ($ this-> handle) flock ($ this-> handle, LOCK_EX | LOCK_NB);} private function _ closes () {if ($ this-> handle) {flock ($ this-> handle, LOCK_UN); fclose ($ this-> handle); $ this-> handle = null ;}}} ######################################## ### call code ################################### ###### set_time_limit (0 ); $ atime = microtime (true); $ obj = new caches (); $ read = 1; // (0/1) write or read, test the effect. $ size = 10000; // 10 K $ arr = range (1,100); // The first time is much faster,: 1 s,: 11 s foreach ($ arr AS $ val) {if ($ read = 0) {// write test. $ ints = $ obj-> write ('key '. $ val, array ('key' => str_repeat ('A', $ size); echo 'key '. $ val. 'write size :'. ($ ints/1000 ). 'kb
';} Else {// read cache test. $ ints = $ obj-> read ('key '. $ val); echo 'key '. $ val. 'read size :'. strlen ($ ints ['key'])/1000. 'kb
';}} #################################### The following code is for monitoring purpose #################################### echo'
Execution time: '; echo sprintf (' % 1.4f ', microtime (true)-$ atime).' second '; echo'
Memory monitoring: '; echo $ new = sprintf (' % 1.4f ', memory_get_peak_usage ()/1024). 'KB'; echo'
Original memory: '; echo $ old; echo'
Increase memory: '; echo sprintf (' % 1.4f ', $ new-$ old). 'KB'; exit ();?>