CNLivePlayerCache.m 2.42 KB
//
//  CNLivePlayerCache.m
//  CNLivePlayer_Example
//
//  Created by CNLive-zxw on 2019/7/26.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLivePlayerCache.h"
#import "YYCache.h"

@implementation CNLivePlayerCache
static NSString *const PlayerCache = @"CNLivePlayer_Data_Url_Cache";

static YYCache *_dataCache;

+ (void)initialize  {
    [super initialize];
    // 实例化方式一
    _dataCache = [[YYCache alloc] initWithName:PlayerCache];
    [CNLivePlayerCache player_SetCostLimit:1024*1024*30];
    
}

/**
 * 存储或者修改数据
 *
 * @param data 保存的数据
 * @param key 保存的key
 */
+ (void)player_SaveDataCache:(id)data forKey:(NSString *)key {
    // 默认会开启线程缓存
    [_dataCache setObject:data forKey:key withBlock:nil];
    
}

/**
 * 读取数据
 *
 * @param key 保存的key
 *
 * @return 返回id
 */
+ (id)player_ReadCache:(NSString *)key {
    return  [_dataCache objectForKey:key];
    
}

/**
 * 读取缓存文件的大小
 *
 * @return 返回NSString
 */
+ (NSString *)player_GetAllHttpCacheSize {
    // 总大小
    unsigned long long diskCache = [_dataCache.diskCache totalCost];
    
    NSString *sizeText = nil;
    
    if (diskCache >= pow(10, 9)) {
        // size >= 1GB
        sizeText = [NSString stringWithFormat:@"%.2fGB", diskCache / pow(10, 9)];
    }
    else if (diskCache >= pow(10, 6)) { // 1GB > size >= 1MB
        sizeText = [NSString stringWithFormat:@"%.2fMB", diskCache / pow(10, 6)];
    }
    else if (diskCache >= pow(10, 3)) { // 1MB > size >= 1KB
        sizeText = [NSString stringWithFormat:@"%.2fKB", diskCache / pow(10, 3)];
    }
    else { // 1KB > size
        sizeText = [NSString stringWithFormat:@"%zdB", diskCache];
    }
    
    return sizeText;
    
}

/**
 * 是否缓存过
 *
 * @param key 保存的key
 *
 * @return 返回NSString
 */
+ (BOOL)player_IsCache:(NSString *)key {
    return [_dataCache containsObjectForKey:key];
    
}

/**
 * 删除某个磁盘缓存文件
 *
 * @param key 保存的key
 *
 */
+ (void)player_RemoveChache:(NSString *)key {
    [_dataCache removeObjectForKey:key withBlock:nil];
    
}

/**
 * 删除所有的磁盘换存文件
 */
+ (void)player_RemoveAllCache {
    [_dataCache removeAllObjects];
    
}

/**
 * 磁盘最大缓存开销
 * 默认是30*1024*1024
 */
+ (void)player_SetCostLimit:(NSInteger)costLimit {
    [_dataCache.diskCache setCostLimit:costLimit];//磁盘最大缓存开销
    
}

@end