├── README.md ├── cache └── .gitignore ├── simple_cache_class.php └── teste.php /README.md: -------------------------------------------------------------------------------- 1 | SimpleCachePhp 2 | ============== 3 | 4 | A simple mode to cache php pages. Support automatic on sessions and querystrings. 5 | 6 | Using the cache 7 | ============== 8 | To use this feature only add to the start of your code: 9 | 10 | 14 | 15 | And this snippet at the end: 16 | 17 | CacheEnd(); ?> 18 | 19 | 20 | Example 21 | ============== 22 | For example, imagine that you have the following php page (uncached): 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |

hello world

31 |

Now is

32 | 33 | 34 | 35 | Cached: 36 | 37 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |

hello world

49 |

Now is

50 | 51 | 52 | 53 | CacheEnd(); ?> 54 | 55 | How this works? 56 | ============== 57 | 58 | The first time the page is accessed it is generated normally. With ob_start, ob_get_contents and fopen a file is created with the cache. 59 | 60 | The files are stored in the /cache. At each new access is checked if cache exists and has not expired. 61 | 62 | The cache takes into account the filename, sessions and querystrings. 63 | 64 | In the example above, the file would be generated: /cache/index.php__, and expire in 24 hours. 65 | 66 | The parameters 67 | ============== 68 | 69 | $cache = new SimpleCachePhp($filename, $time, $ignoreKeysOnParametrize, $folderCache); 70 | 71 | $filename 72 |
73 | The name of the cache file will be saved. To use the current name of the file, use: __FILE__ 74 |
75 | 76 | $time 77 |
78 | The time the cache is stored before expiring. 79 | Default: 86400 (24 hours) 80 |
81 | 82 | $ignoreKeysOnParametrize 83 |
84 | An array of keys that should be ignored. For example, if you use a login session but the pages are identical. 85 | Default: null 86 |
87 | 88 | $folderCache 89 |
90 | The folder that the files are stored. 91 | /cache/ 92 |
-------------------------------------------------------------------------------- /cache/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/verissimor/SimpleCachePhp/d0310a1021f3cbc1d88f1913ee947cc1ce6935e5/cache/.gitignore -------------------------------------------------------------------------------- /simple_cache_class.php: -------------------------------------------------------------------------------- 1 | "; 24 | exit(); 25 | } else { 26 | $this->folderCache = $folderCache; 27 | $this->filename = $filename; 28 | ob_start(); 29 | } 30 | } 31 | 32 | public function CacheEnd() { 33 | $s = ob_get_contents(); 34 | self::criarFileCache($this->folderCache, $this->filename, $s); 35 | ob_end_flush(); 36 | } 37 | 38 | private function getFileName($filename, $ignoreKeysOnParametrize) { 39 | if (!isset($_SESSION)) { 40 | session_start(); 41 | } 42 | 43 | $s = basename($filename); 44 | $s .= "_"; 45 | $s .= "_-s-_" . self::parameterize_array($_SESSION, $ignoreKeysOnParametrize); 46 | $s .= "_-q-_" . self::parameterize_array($_GET, $ignoreKeysOnParametrize); 47 | return self::toRewriteString($s); 48 | } 49 | 50 | private static function parameterize_array($array, $ignoreKeysOnParametrize) { 51 | $out = array(); 52 | foreach ($array as $key => $value) { 53 | if (!in_array($key, $ignoreKeysOnParametrize)) 54 | $out[] = "$key-$value"; 55 | } 56 | 57 | return join("_", $out); 58 | } 59 | 60 | private static function toRewriteString($s) { 61 | $s = trim($s); 62 | $s = mb_strtolower($s, 'UTF-8'); 63 | 64 | //Letra a 65 | $s = str_replace("á", "a", $s); 66 | $s = str_replace("à", "a", $s); 67 | $s = str_replace("ã", "a", $s); 68 | $s = str_replace("â", "a", $s); 69 | $s = str_replace("ä", "a", $s); 70 | 71 | //letra e 72 | $s = str_replace("é", "e", $s); 73 | $s = str_replace("ê", "e", $s); 74 | $s = str_replace("è", "e", $s); 75 | $s = str_replace("ë", "e", $s); 76 | 77 | //letra i 78 | $s = str_replace("í", "i", $s); 79 | $s = str_replace("ì", "i", $s); 80 | $s = str_replace("î", "i", $s); 81 | $s = str_replace("ï", "i", $s); 82 | 83 | //letra o 84 | $s = str_replace("ó", "o", $s); 85 | $s = str_replace("ô", "o", $s); 86 | $s = str_replace("õ", "o", $s); 87 | $s = str_replace("ò", "o", $s); 88 | $s = str_replace("ö", "o", $s); 89 | 90 | //letra u 91 | $s = str_replace("ú", "u", $s); 92 | $s = str_replace("ü", "u", $s); 93 | $s = str_replace("û", "u", $s); 94 | $s = str_replace("ù", "u", $s); 95 | 96 | //letra c 97 | $s = str_replace("ç", "c", $s); 98 | 99 | //ultimos caracteres indesejaveis 100 | $s = str_replace(" ", " ", $s); 101 | $s = str_replace(" ", "-", $s); 102 | 103 | $s = preg_replace("/[^a-zA-Z0-9_.-]/", "", $s); 104 | $s = str_replace("-.", ".", $s); 105 | return $s; 106 | } 107 | 108 | /** 109 | * Verficia se o arquivo de cache existe e se o seu tempo é maior que a variavel time. Unidade de medida time é em segundos ex.: 5 * 60 = 5 min 110 | * @param string $filename Nome do arquivo 111 | * @param int $time Tempo que o arquivo deve estar em cache 112 | * @return boolean 113 | */ 114 | public static function verificaCacheFile($filename, $time = 86400) { 115 | if (file_exists($filename) && (time() - $time < filemtime($filename))) { 116 | return true; 117 | } else { 118 | return false; 119 | } 120 | } 121 | 122 | /** 123 | * 124 | * Verifica se os diretorio de cache existe e se possui permissoes de escrita 125 | * @param string $folder Pasta que os arquivos de cache serao armazenados. 126 | * @return boolean 127 | */ 128 | private static function verificaDiretorios($folder) { 129 | 130 | if (!is_dir($folder)) { 131 | if (!mkdir($folder)) { 132 | die("sistemaCache: Nao foi possivel criar o diretorio $folder. Tente criar via ftp, liberando as permissoes de escrita."); 133 | return false; 134 | } 135 | } 136 | 137 | 138 | if (!is_writable($folder)) { 139 | if (!chmod($folder, 0777)) { 140 | die("sistemaCache: O diretorio $folder nao possui permissao de escrita. Tente criar via ftp, liberando as permissoes de escrita."); 141 | return false; 142 | } 143 | } 144 | 145 | return true; 146 | } 147 | 148 | public static function criarFileCache($folderCache, $fileName, $conteudo) { 149 | self::verificaDiretorios($folderCache); 150 | 151 | $filename = $folderCache . "/" . $fileName; 152 | 153 | $fp = @fopen($filename, "w"); 154 | if ($fp) { 155 | fwrite($fp, $conteudo); 156 | fclose($fp); 157 | 158 | if (file_exists($filename)) { 159 | chmod($filename, 0777); 160 | return true; 161 | } 162 | } 163 | 164 | echo ""; 165 | return false; 166 | } 167 | 168 | public static function obterDocumentRoot() { 169 | $AppRoot = $_SERVER['DOCUMENT_ROOT']; 170 | 171 | if ($AppRoot[strlen($AppRoot) - 1] != "/") { 172 | $AppRoot.= "/"; 173 | } 174 | 175 | return $AppRoot; 176 | } 177 | 178 | } 179 | 180 | -------------------------------------------------------------------------------- /teste.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |

hello world

8 |

Now is

9 | 10 | --------------------------------------------------------------------------------