YYCache (1)

YYCache (1)

Prefácio:

Geralmente armazenamos em cache dados relativamente grandes solicitados da rede. Se não houver rede ou o logotipo solicitado for consistente com o logotipo anterior, indicando que os dados não foram alterados, você pode usar o cache para carregar sem puxar novamente os dados de a rede Aqui Geralmente use YYCache .

Gráfico 1.png colado

Desça de gitcima YYCache pod:

Gráfico colado. png

A estrutura do arquivo que pode ser vista YYCacheé relativamente simples, além YYCachedo arquivo de interface para uso externo, há também YYDiskCacheo cache de disco, YYKVStorageo armazenamento de chave-valor de metadados e YYMemoryCacheo cache de memória.

1. YYCache:

YYCacheÉ um cache de valor-chave thread-safe. Ele usa YYMemoryCacheobjetos armazenados em um cache de memória pequeno e rápido e persiste YYDiskCacheobjetos em um cache de disco grande e lento.

lista de propriedades:

属性就三个:
@interface YYCache : NSObject
/** 缓存名字,只读*/
@property (copy, readonly) NSString *name;
/** 内存缓存.*/
@property (strong, readonly) YYMemoryCache *memoryCache;
/** 磁盘缓存.*/
@property (strong, readonly) YYDiskCache *diskCache;
复制代码

Lista de métodos:

inicialização:

初始化:
/**
用指定的名称创建一个新实例。
 */
- (nullable instancetype)initWithName:(NSString *)name;
/**
用指定的路径创建一个新实例。
 */
- (nullable instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;

/**
便捷初始化,用指定的名称创建一个新实例。
 */
+ (nullable instancetype)cacheWithName:(NSString *)name;
/**
便捷初始化,用指定的路径创建一个新实例
 */
+ (nullable instancetype)cacheWithPath:(NSString *)path;
复制代码

Método de acesso:


/**
返回一个布尔值,一个指定的键是否在缓存中,可能会阻塞线程直到文件读取完成。
 */
- (BOOL)containsObjectForKey:(NSString *)key;

/**
返回指定键相对应的值。
 */
- (nullable id<NSCoding>)objectForKey:(NSString *)key;

/**
设置缓存中指定键的值。
 */
- (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key;

/**
删除缓存中指定键的值,如果为nil,则此方法无效。
 */
- (void)removeObjectForKey:(NSString *)key;
/**
清空缓存。
 */
- (void)removeAllObjects;
/**
用block清空缓存。可以通过参数得到进度。
 */
- (void)removeAllObjectsWithProgressBlock:(nullable void(^)(int removedCount, int totalCount))progress
                                 endBlock:(nullable void(^)(BOOL error))end;

复制代码

Veja .mo método de inicialização do arquivo initWithName:

- (instancetype)initWithName:(NSString *)name {
    if (name.length == 0) return nil;
    NSString *cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSString *path = [cacheFolder stringByAppendingPathComponent:name];
    return [self initWithPath:path];
}
复制代码

O Discovery initWithNamechama outro método de inicialização initWithPathe YYMemoryCacheo método de inicialização:

- (instancetype)initWithPath:(NSString *)path {
    return [self initWithPath:path inlineThreshold:1024 * 20]; // 20KB
}
查看initWithPath:(NSString *)path
             inlineThreshold:(NSUInteger)threshold发现:

- (instancetype)initWithPath:(NSString *)path
             inlineThreshold:(NSUInteger)threshold {
    
    YYKVStorageType type;
    if (threshold == 0) {
        type = YYKVStorageTypeFile;
    } else if (threshold == NSUIntegerMax) {
        type = YYKVStorageTypeSQLite;
    } else {
        type = YYKVStorageTypeMixed;
    }
	_inlineThreshold = threshold;
}
复制代码

Como pode ser visto no YYDiskCachelimite inline é 20KB, _inlineThresholdé inicializado como 20KB.

Clique para YYKVStorageTypevisualizar e descobrir que existem três tipos de armazenamento:
1. Arquivo 2. Banco de dados 3. Opcional

typedef NS_ENUM(NSUInteger, YYKVStorageType) {
    /// The `value` is stored as a file in file system.
    YYKVStorageTypeFile = 0,
    
    /// The `value` is stored in sqlite with blob type.
    YYKVStorageTypeSQLite = 1,
    
    /// The `value` is stored in file system or sqlite based on your choice.
    YYKVStorageTypeMixed = 2,
};
复制代码

Observe novamente a tarefa:

- (void)setObject:(id<NSCoding>)object forKey:(NSString *)key {
    NSData *value = nil;

    if (!value) return;
    NSString *filename = nil;
    if (_kv.type != YYKVStorageTypeSQLite) {
        if (value.length > _inlineThreshold) {
            filename = [self _filenameForKey:key];
        }
    }
    Lock();
    [_kv saveItemWithKey:key value:value filename:filename extendedData:extendedData];
    Unlock();
}
复制代码

Você pode ver value.length> _inlineThreshold, filenameserá atribuído, clique em saveItemWithKeymétodo:

- (BOOL)saveItemWithKey:(NSString *)key value:(NSData *)value filename:(NSString *)filename extendedData:(NSData *)extendedData {

    if (filename.length) {
        if (![self _fileWriteWithName:filename data:value]) {
            return NO;
        }
        if (![self _dbSaveWithKey:key value:value fileName:filename extendedData:extendedData]) {
            [self _fileDeleteWithName:filename];
            return NO;
        }
        return YES;
    } else {
        if (_type != YYKVStorageTypeSQLite) {
            NSString *filename = [self _dbGetFilenameWithKey:key];
            if (filename) {
                [self _fileDeleteWithName:filename];
            }
        }
        return [self _dbSaveWithKey:key value:value fileName:nil extendedData:extendedData];
    }
}
复制代码

Pode-se observar filename.lengthque se houver um valor, ele será gravado diretamente no arquivo, se não for maior que 20KB, use sqlitewrite.

Autor : NSURLCache, FBDiskCache são baseados no banco de dados SQLite. O cache baseado em banco de dados pode suportar metadados, fácil de expandir e rápido nas estatísticas de dados. Também é fácil implementar LRU ou outros algoritmos de eliminação. A única incerteza é o desempenho da leitura e gravação do banco de dados. Por esse motivo, avaliei SQLite em desempenho on-board real. No iPhone 6 64G, o desempenho de gravação do SQLite é maior do que a gravação direta do arquivo, mas o desempenho de leitura depende do tamanho dos dados: quando um único dado é menor que 20K, quanto menor o dado, maior o desempenho de leitura do SQLite; quando o único pedaço de dados é maior que 20K, escreva diretamente Será mais rápido para o arquivo.

Dois, YYDiskCache:

(Esses mesmos parâmetros e métodos não são reescritos)

Lista de propriedades:


/**
如果对象的数据大小(以字节为单位)大于此值,则对象将
存储为文件,否则对象将存储在sqlite中。
0表示所有对象将存储为分开的文件,NSUIntegerMax表示所有对象
对象将存储在sqlite中。
默认值为20480 (20KB)。
 */
@property (readonly) NSUInteger inlineThreshold;
/**
如果这个块不是nil,那么这个块将被用来存档对象
NSKeyedArchiver。您可以使用此块来支持不支持的对象
遵守' NSCoding '协议。
默认值为空。
 */
@property (nullable, copy) NSData *(^customArchiveBlock)(id object);
/**
如果这个块不是nil,那么这个块将被用来解存档对象
NSKeyedUnarchiver。您可以使用此块来支持不支持的对象
遵守' NSCoding '协议。
默认值为空。
 */
@property (nullable, copy) id (^customUnarchiveBlock)(NSData *data);
/**
当需要将对象保存为文件时,将调用此块来生成
指定键的文件名。如果块为空,缓存使用md5(key)作为默认文件名。默认值为空。
 */
@property (nullable, copy) NSString *(^customFileNameBlock)(NSString *key);
#pragma mark - Limit
/**
缓存应容纳的最大对象数。
默认值为NSUIntegerMax,即不限制。
这不是一个严格的限制-如果缓存超过限制,缓存中的一些对象
缓存可以稍后在后台队列中被清除。
 */
@property NSUInteger countLimit;
/**
在开始清除对象之前,缓存可以保留的最大总开销。
默认值为NSUIntegerMax,即不限制。
这不是一个严格的限制-如果缓存超过限制,缓存中的一些对象
缓存可以稍后在后台队列中被清除。
 */
@property NSUInteger costLimit;
/**
缓存中对象的最大过期时间。

>值为DBL_MAX,即无限制。
这不是一个严格的限制-如果一个对象超过了限制,对象可以
稍后在后台队列中被驱逐。
 */
@property NSTimeInterval ageLimit;
/**
缓存应保留的最小空闲磁盘空间(以字节为单位)。

>默认值为0,表示不限制。
如果可用磁盘空间低于此值,缓存将删除对象
释放一些磁盘空间。这不是一个严格的限制——如果空闲磁盘空间没有了
超过限制,对象可能稍后在后台队列中被清除。
 */
@property NSUInteger freeDiskSpaceLimit;
/**
自动修整检查时间间隔以秒为单位。默认值是60(1分钟)。
缓存保存一个内部计时器来检查缓存是否达到
它的极限,一旦达到极限,它就开始驱逐物体。
 */
@property NSTimeInterval autoTrimInterval;
/**
设置“YES”为调试启用错误日志。
 */
@property BOOL errorLogsEnabled;
复制代码

Lista de métodos:

/**
指定的初始化式。
threshold数据存储内联阈值,单位为字节。如果对象的数据
Size(以字节为单位)大于此值,则对象将存储为
文件,否则对象将存储在sqlite中。0表示所有对象都会
NSUIntegerMax表示所有对象都将被存储
sqlite。如果您不知道对象的大小,20480是一个不错的选择。
在第一次初始化后,您不应该更改指定路径的这个值。
如果指定路径的缓存实例在内存中已经存在,
该方法将直接返回该实例,而不是创建一个新实例。
 */
- (nullable instancetype)initWithPath:(NSString *)path inlineThreshold:(NSUInteger)threshold NS_DESIGNATED_INITIALIZER;
/**
返回此缓存中的对象数量。
 */
- (NSInteger)totalCount;
/**
以字节为单位的对象开销总数。
 */
- (NSInteger)totalCost;
#pragma mark - 修剪
/**
使用LRU从缓存中移除对象,直到' totalCount '低于指定值。
 */
- (void)trimToCount:(NSUInteger)count;
/**
使用LRU从缓存中移除对象,直到' totalCost '低于指定值,完成后调用回调。
 */
- (void)trimToCost:(NSUInteger)cost;
/**
使用LRU从缓存中删除对象,直到所有过期对象被指定值删除为止。
 */
- (void)trimToAge:(NSTimeInterval)age;
/**
从对象中获取扩展数据。
 */
+ (nullable NSData *)getExtendedDataFromObject:(id)object;
/**
将扩展数据设置为一个对象。
 */
+ (void)setExtendedData:(nullable NSData *)extendedData toObject:(id)object;

@end
复制代码

3. YYMemoryCache:

Lista de propriedades:

/** 存中的对象数量(只读)*/
@property (readonly) NSUInteger totalCount;

/** 缓存中对象的总开销(只读)*/
@property (readonly) NSUInteger totalCost;

/**
自动修整检查时间间隔以秒为单位。默认是5.0。
 */
@property NSTimeInterval autoTrimInterval;
/**
如果' YES ',当应用程序收到内存警告时,缓存将删除所有对象。
 */
@property BOOL shouldRemoveAllObjectsOnMemoryWarning;
/**
如果是,当应用程序进入后台时,缓存将删除所有对象。
 */
@property BOOL shouldRemoveAllObjectsWhenEnteringBackground;
/**
当应用程序收到内存警告时要执行的块。
 */
@property (nullable, copy) void(^didReceiveMemoryWarningBlock)(YYMemoryCache *cache);
/**
当应用程序进入后台时执行的一个块。
 */
@property (nullable, copy) void(^didEnterBackgroundBlock)(YYMemoryCache *cache);
/**
如果' YES ',键值对将在主线程上释放,否则在后台线程上释放。默认为NO。。
 */
@property BOOL releaseOnMainThread;
/**
如果' YES ',键值对将在主线程上释放,否则在后台线程上释放。默认为NO。
 */
@property BOOL releaseAsynchronously;
复制代码

Resumir:

Compare NSCache:

@interface NSCache <KeyType, ObjectType> : NSObject
@property (copy) NSString *name;
@property (nullable, assign) id<NSCacheDelegate> delegate;
- (nullable ObjectType)objectForKey:(KeyType)key;
- (void)setObject:(ObjectType)obj forKey:(KeyType)key; // 0 cost
- (void)setObject:(ObjectType)obj forKey:(KeyType)key cost:(NSUInteger)g;
- (void)removeObjectForKey:(KeyType)key;
- (void)removeAllObjects;
@property NSUInteger totalCostLimit;	// limits are imprecise/not strict
@property NSUInteger countLimit;	// limits are imprecise/not strict
@property BOOL evictsObjectsWithDiscardedContent;
@end
@protocol NSCacheDelegate <NSObject>
@optional
- (void)cache:(NSCache *)cache willEvictObject:(id)obj;
@end
复制代码

Você descobrirá NSCacheque é relativamente simples e YYCacheque existem muitas interfaces para caches de memória e disco para preparar e controlar o número e o ciclo de vida dos caches.

Guess you like

Origin juejin.im/post/7214024762855505975