VIMediaCache icon indicating copy to clipboard operation
VIMediaCache copied to clipboard

缓存文件空间浪费比较多

Open tonyzhou opened this issue 7 years ago • 1 comments

- (void)setContentInfo:(VIContentInfo *)contentInfo error:(NSError **)error {
    self.internalCacheConfiguration.contentInfo = contentInfo;
    @try {
        [self.writeFileHandle truncateFileAtOffset:contentInfo.contentLength];
        [self.writeFileHandle synchronizeFile];
    } @catch (NSException *exception) {
        NSLog(@"read cached data error %@", exception);
        *error = [NSError errorWithDomain:exception.name code:123 userInfo:@{NSLocalizedDescriptionKey: exception.reason, @"exception": exception}];
    }
}

这里的truncateFileAtOffset一开始就会占用视频的fullsize。如果用这个库在列表页做预加载,会非常浪费空间。 一个短视频如果有8M,预加载只下载前面800K,也需要用掉8M的空间。 其实这里不truncate也是完全没有什么问题的。

tonyzhou avatar Jul 24 '17 12:07 tonyzhou

因为用户在拖动播放进度的时候,dataRequest 请求的 range 不一定是连续的,这时如果文件没有 truncate 的话,用 FileHandle 的 seek 到一个没有数据的位置,就会失败,会导致后续的写缓存操作把正确的数据写入错误的位置,下次再取缓存,取到的数据就是错的,造成不能正常播放。

所以一开始 truncate 创建整个文件这个方案比较适合比较小的文件,对于大文件还要考虑 IO 开销和存储占用,这么写就不合适了。

lexrus avatar Jan 21 '21 10:01 lexrus