#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class TTURLRequest;
/**
* A general purpose URL cache for caching data in memory and on disk.
*
* Etags are supported.
*/
@interface TTURLCache : NSObject {
NSString* _name;
NSString* _cachePath;
NSMutableDictionary* _imageCache;
NSMutableArray* _imageSortedList;
NSUInteger _totalPixelCount;
NSUInteger _maxPixelCount;
NSInteger _totalLoading;
NSTimeInterval _invalidationAge;
BOOL _disableDiskCache;
BOOL _disableImageCache;
}
/**
* 禁用磁盘缓存,也禁用etag支持
*/
@property (nonatomic) BOOL disableDiskCache;
/**
* 对于图片禁用内存缓存
*/
@property (nonatomic) BOOL disableImageCache;
/**
* 获取磁盘缓存的路径
*/
@property (nonatomic, copy) NSString* cachePath;
/**
*获取etags缓存的磁盘路径
*/
@property (nonatomic, readonly) NSString* etagCachePath;
/**
* 图片缓存在内存中的对大像素
*设置0,不限制最大数,默认是0
*/
@property (nonatomic) NSUInteger maxPixelCount;
/**
* 当实效以后,设置文件修改的时间
*/
@property (nonatomic) NSTimeInterval invalidationAge;
/**
*根据唯一的名字 获取共享缓存
*/
+ (TTURLCache*)cacheWithName:(NSString*)name;
/**
* TTURLCache单例对象
*/
+ (TTURLCache*)sharedCache;
/**
* 设置TTURLCache单例对象
*/
+ (void)setSharedCache:(TTURLCache*)cache;
- (id)initWithName:(NSString*)name;
/**
* 获取response 使用缓存的key
*/
- (NSString *)keyForURL:(NSString*)URL;
/**
*url存储的缓存路径
*/
- (NSString*)cachePathForURL:(NSString*)URL;
/**
*获取key缓存的路径
*/
- (NSString*)cachePathForKey:(NSString*)key;
/**
* Etag缓存文件存储的方法 :
* File name: <key>
* File data: <etag value>
* @根据给定的key返回etag缓存路径
*/
- (NSString*)etagCachePathForKey:(NSString*)key;
/**
* 确定是否存在一个url缓存条目
*/
- (BOOL)hasDataForURL:(NSString*)URL;
/**
*确定是否存在一个key的缓存条目
*/
- (BOOL)hasDataForKey:(NSString*)key expires:(NSTimeInterval)expirationAge;
/**
* 是否存在一个url的图片缓存条目
*/
- (BOOL)hasImageForURL:(NSString*)URL fromDisk:(BOOL)fromDisk;
/**
*如果存在就从缓存中获取url的数据
* @如果url没有缓存,就return nil
*/
- (NSData*)dataForURL:(NSString*)URL;
/**
* 如果存在并且比最小时间还晚,就从缓存中获取url的数据
*如果没有缓存 或者缓存条目比最小时间还早,就return nil
*/
- (NSData*)dataForURL:(NSString*)URL expires:(NSTimeInterval)expirationAge
timestamp:(NSDate**)timestamp;
- (NSData*)dataForKey:(NSString*)key expires:(NSTimeInterval)expirationAge
timestamp:(NSDate**)timestamp;
/**
*从内存中获取图片缓存
*没有缓存会返回 nil
*/
- (id)imageForURL:(NSString*)URL;
- (id)imageForURL:(NSString*)URL fromDisk:(BOOL)fromDisk;
/**
*从给定的cache key里面获取etag
*/
- (NSString*)etagForKey:(NSString*)key;
/**
* 把数据存储到磁盘中
*/
- (void)storeData:(NSData*)data forURL:(NSString*)URL;
- (void)storeData:(NSData*)data forKey:(NSString*)key;
/**
* 把图片存储到内存中
*/
- (void)storeImage:(UIImage*)image forURL:(NSString*)URL;
/**
* 在etag缓存中存储etag值
*/
- (void)storeEtag:(NSString*)etag forKey:(NSString*)key;
/**
* 为缓存在内存中的数据,便捷的方式为创建临时的url
* @return The temporary URL
*/
- (NSString*)storeTemporaryImage:(UIImage*)image toDisk:(BOOL)toDisk;
/**
* 为缓存在磁盘中的数据,便捷的方式为创建临时的url
*/
- (NSString*)storeTemporaryData:(NSData*)data;
/**
* 便捷的方式为一个文件并移动到磁盘缓存中 ,创建一个临时的url
*
* @return The temporary URL
*/
- (NSString*)storeTemporaryFile:(NSURL*)fileURL;
/**
* Moves the data currently stored under one URL to another URL.
*
* This is handy when you are caching data at a temporary URL while the permanent URL is being
* retrieved from a server. Once you know the permanent URL you can use this to move the data.
*/
- (void)moveDataForURL:(NSString*)oldURL toURL:(NSString*)newURL;
- (void)moveDataFromPath:(NSString*)path toURL:(NSString*)newURL;
- (NSString*)moveDataFromPathToTemporaryURL:(NSString*)path;
/**
* Removes the data for a URL from the memory cache and optionally from the disk cache.
*/
- (void)removeURL:(NSString*)URL fromDisk:(BOOL)fromDisk;
- (void)removeKey:(NSString*)key;
/**
* Erases the memory cache and optionally the disk cache.
*/
- (void)removeAll:(BOOL)fromDisk;
/**
* Invalidates the file in the disk cache so that its modified timestamp is the current
* time minus the default cache expiration age.
*
* This ensures that the next time the URL is requested from the cache it will be loaded
* from the network if the default cache expiration age is used.
*/
- (void)invalidateURL:(NSString*)URL;
- (void)invalidateKey:(NSString*)key;
/**
* Invalidates all files in the disk cache according to rules explained in `invalidateURL`.
*/
- (void)invalidateAll;
- (void)logMemoryUsage;
@end