PHP cache, SimpleCache class

xiaoxiao2021-03-06  40

A simple page cache, the effect is still more obvious

/ * Page Cache Mechanism * Summary: Page Cache Mechanism * Class Name: SimpleCache * File Name: SimpleCache_Class.php * Version: 0.0.2 * Creation Time: 2004.7.24 03:38 * Modification Time: 2004.11.28 23:28 * OF: shadow_wwp@yahoo.com * copyright: cOPYRIGHT (C) 2004-2005 streaming video studio * / / * Usage require_once ( "SimpleCache.php"); $ effect_param = array ( "a", "b", " c "," d "," e "); $ effect_flag_param = array (" a "," b "," c "); $ cache = new simplecche (); $ cache-> start (5, $ effect_param, $ Effect_flag_param); // --- // echo "hhhh"; // - - // $ cache-> end (); * / define ("cache_no_create", "no"

Define ("cache_create", "yes"

); Define ("cache_create_fault", "err"

);

// Cache class path Define ("root", "/ data1"

Define ("cache", root. "/ cache"

Class

SimpleCache

{Var $ filename;

// The file name to save var $ flag

; Var $ EFFECT_TIME

Var $ cache_error

;

/ / The following is a 0.0.2 version update var $ effect_PARAM = Array ();

// Valid parameter var $ effect_flag_param = array ();

// Mark type parameter var $ cache_path;

// To save the file directory name // Enable cache // $ flag cache_create ob_start boot // $ flag cache_no_create does not need to create a cache read cache // $ flag cache_create_fault creation cache failed //0.0.2 update function start ($ Effect, $ Effect_Param, $ Effect_Flag_Param

) {$ This-> Effect_time = $ Effect

; If (! Is_ARRAY ($ EFFECT_PARAM

)) {$ This-> flag = cache_create_fault

$ This-> cache_error. = "/N"."/$Effect_param is not an array"

} Else {$ this-> Effect_Param = $ Effect_Param;}} (! Is_ARRAY ($ this-> Effect_Flag_Param

)) {$ This-> flag = cache_create_fault

$ This-> cache_error. = "/N"."effect_flag_param is not an array"

} Else {$ this-> effect_flag_param = $ effect_flag_param

} $ This-> IS_EXPIRE

(); IF ($ this-> flag == cache_no_create)

// Read the signal to see if it should be created

{$ This-> cache_read

();}} ($ This-> flag == cache_create

) {If (! OB_Start

()) {$ This-> flag = cache_create_fault

$ This-> cache_error. = "/ N". "Ob_start () failed! / N"

}}

// debug echo "/ n flag. "-> / n";

}

/ / Enable the cache end function end

() {IF ($ this-> flag == cache_create

) {

// Create a file if (! $ This-> Create_Cache

()) {$ This-> flag = cache_create_fault

$ This-> cache_error. = "/ N". "Create Cache Fail / N"

EXIT (0

}

/ / Mark Next No Buy $ this-> Flag = Cache_NO_CREATE

}}

/ / Judgment if the cache file is expired //0.0.2 Optimization Function is_expire

() {If (! $ This-> get_filename

()) {$ This-> cache_error. = "/ N". "Cannot Generate File Name!"

; $ This-> flag = cache_create_fault

Return 0

} $ Now = Time (); if (! File_exists ($ this-> filename

)) {$ This-> flag = cache_create

Return 0

} $ Cretetime = filemtime ($ this-> filename

$ TIME_FIELD = $ now - $ CREATETIME

;

// debug echo "/ n / n"; IF ($ TIME_FIELD> $ this-> Effect_time

) {$ This-> flag = cache_create

Return 0

} $ This-> flag = cache_no_create

}

/ / Name // Naming Rules Yes: Parameter _ Parameter Value _ Created File Name //0.0.2 Optimization Function Get_FileName

() {If (! $ This-> GET_DIR

()) {$ This-> cache_error. = "/ N". "Cannot get a directory"

Return False

} Foreach ($ this-> effect_Param as $ key

) {$ This-> filename = $ this-> filename. "_". $ Key. "_". $ _ Request [$ key

} $ This-> filename = md5 ($ this-> filename). "_" .Basename ($ _ server ["php_self"

]); $ This-> filename = $ this-> cache_path. "/". $ This-> filename

Return True

}

// Get the directory name If the directory does not exist, create a //0.0.2 to increase the function get_dir

() {$ This-> cache_path = cache

Foreach ($ this-> effect_flag_param as $ key

) {$ This-> cache_path = $ this-> cache_path. "/". MD5 ($ _ request [$ key

]); If (! $ This-> check_dir

()) {RETURN FALSE

}} Return true

}

// Detecting the directory name If there is no existence, create a //0.0.2 increase function check_dir () {if (! Is_dir ($ this-> cache_path

)) {If (! Mkdir ($ this-> cache_path

)) {$ This-> cache_error. = "/ N". "Can't create a cache directory!"

Return False

}} Return true

}

/ / Take data from the cache Function Cache_read

() {Include ($ this-> filename

Die ();

/ / Generate Cache Page Function Create_Cache

() {$ This-> info

();

// Write this page to file IF (! ($ Fp = fopen ($ this-> filename, "w"

)) {RETURN FALSE

}} If (! $ Fp

) {Echo $ this-> filename

FLOCK ($ FP, 2

); If (! Fputs ($ fp, ob_get_contents

())) {RETURN FALSE

Flock ($ fp, 3

Fclose ($ FP)

);

// Write to complete OB_END_FLUSH

();

// ob_end_clean (); return true

}

// Print debugging information Function Info

() {

#echo "/ n

Echo "file name:" $ this-> filename. "/ N"

Echo "Valid Period:" $ this-> Effect_time. "/ N"

Echo "Cache Directory:" $ this-> cache_path. "/ N"

; Echo created cache name: ". BaseName ($ _ server [" php_self "])." / N "

; Echo "------------------------------------------- / n "

Echo $ this-> cache_error. "/ N"

; Echo "------------------------------------------- / n "

;

#echo "-> / n";

}

?>

转载请注明原文地址:https://www.9cbs.com/read-77933.html

New Post(0)