$lastModified) && $cacheTime + self::$gcLifetime > time() ) { $c = file_get_contents($fileCache); $c = unserialize($c); if (is_array($c) && isset($c['value'])) { return $c['value']; } } } return null; } /** * Put in cache the result of $operation on $what, * which is known as dependant from the content of $options * * @param string $operation * @param mixed $what * @param mixed $value * @param array $options */ public function setCache($operation, $what, $value, $options = []) { $fileCache = self::$cacheDir . self::cacheName($operation, $what, $options); $c = ['value' => $value]; $c = serialize($c); file_put_contents($fileCache, $c); if (self::$forceRefresh === 'once') { self::$refreshed[$fileCache] = true; } } /** * Get the cache name for the caching of $operation on $what, * which is known as dependant from the content of $options * * @param string $operation * @param mixed $what * @param array $options * * @return string */ private static function cacheName($operation, $what, $options = []) { $t = [ 'version' => self::CACHE_VERSION, 'operation' => $operation, 'what' => $what, 'options' => $options ]; $t = self::$prefix . sha1(json_encode($t)) . ".$operation" . ".scsscache"; return $t; } /** * Check that the cache dir exists and is writeable * * @throws \Exception */ public static function checkCacheDir() { self::$cacheDir = str_replace('\\', '/', self::$cacheDir); self::$cacheDir = rtrim(self::$cacheDir, '/') . '/'; if (! file_exists(self::$cacheDir)) { if (! mkdir(self::$cacheDir)) { throw new Exception('Cache directory couldn\'t be created: ' . self::$cacheDir); } } elseif (! is_dir(self::$cacheDir)) { throw new Exception('Cache directory doesn\'t exist: ' . self::$cacheDir); } elseif (! is_writable(self::$cacheDir)) { throw new Exception('Cache directory isn\'t writable: ' . self::$cacheDir); } } /** * Delete unused cached files */ public static function cleanCache() { static $clean = false; if ($clean || empty(self::$cacheDir)) { return; } $clean = true; // only remove files with extensions created by SCSSPHP Cache // css files removed based on the list files $removeTypes = ['scsscache' => 1]; $files = scandir(self::$cacheDir); if (! $files) { return; } $checkTime = time() - self::$gcLifetime; foreach ($files as $file) { // don't delete if the file wasn't created with SCSSPHP Cache if (strpos($file, self::$prefix) !== 0) { continue; } $parts = explode('.', $file); $type = array_pop($parts); if (! isset($removeTypes[$type])) { continue; } $fullPath = self::$cacheDir . $file; $mtime = filemtime($fullPath); // don't delete if it's a relatively new file if ($mtime > $checkTime) { continue; } unlink($fullPath); } } }