THINKPHP文件缓存管理

/**
 * 添加缓存key
 * @param $key
 * @return bool
 */
public static function addCacheKey($key) {
    $data = self::readLog();
    if(in_array($key, $data)){
        return true;
    }
    $data[] = $key;
    file_put_contents(LOG_PATH.'cache_keys.json', json_encode($data));
}

/**
 * 删除缓存
 * @param $key
 * @param bool $like 是否删除相似的key
 * @return bool
 */
public static function removeCacheKey($key, $like = false){
    $data = self::readLog();
    if(in_array($key, $data)){
        return true;
    }
    foreach ($data as $p => $k){
        if($like){
            if(strpos($k, $key) !== false){
                unset($data[$p]);
                S($k, NULL);
            }
        }else{
            if($k == $key){
                unset($data[$p]);
                S($k, NULL);
            }
        }
    }
    return true;
}

private static function readLog(){
    $json = file_get_contents(LOG_PATH.'cache_keys.json');
    if($json){
        return json_decode($json, true);
    }
    return array();
}

猜你喜欢

转载自my.oschina.net/u/232595/blog/1824586
今日推荐