/** * 获取缓存文件路径 * @param string $key * @param string $extension 缓存后缀 * @return string */ public function getCacheFile($key = null, $extension = '.cache') { $cacheDir = $this->configs['cache_dir']; /** * 1. 如果有传入了缓存key,则默认为公共缓存,缓存文件全部放入公共缓存中 * 2. 如果key=null,则认为是模块的特殊缓存,按照模块将缓存分类 */ if ($key) { $dir = getHashCode($key) % self::$_FILE_OPACITY; $cacheDir .= "common/{$dir}/"; return $cacheDir . "{$key}.cache"; } else { $cacheDir .= $this->baseKey . '/'; $filename = $this->baseKey; if ($this->ftype) { $cacheDir .= $this->ftype . '/'; $filename .= '_' . $this->ftype; } if ($this->factor) { if (is_numeric($this->factor)) { $cacheDir .= $this->factor % self::$_FILE_OPACITY . '/'; } else { $factor = HashUtils::BKDRHash($this->factor); $cacheDir .= $factor % self::$_FILE_OPACITY . '/'; } $filename .= '_' . $this->factor; } return $cacheDir . $filename . $extension; } }
/** * 计算字符串的hash值 * @param string $str * @return int */ function getHashCode($str) { return \herosphp\utils\HashUtils::BKDRHash($str); }