CNLivePlayerCache.m
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// 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