YYKIT系列之 —— YYCache

YYCache是一个优秀的缓存框架,其文件结构如下:


YYCache的文件结构分为四个,YYCAche、YYDiskCache、YYKVStorage、YYMemoryCache

YYCAche使用以及介绍:

初始化方法:

- (nullable instancetype)initWithName:(NSString *)name;

- (nullable instancetype)initWithPath:(NSString *)path;

+ (nullable instancetype)cacheWithName:(NSString *)name;

+ (nullable instancetype)cacheWithPath:(NSString *)path;

属性:

@property (copy, readonly) NSString *name;

@property (strong, readonly) YYMemoryCache *memoryCache;

@property (strong, readonly) YYDiskCache *diskCache;

缓存操作方法:

- (BOOL)containsObjectForKey:(NSString *)key;

- (void)containsObjectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key, BOOL contains))block;

- (nullable id<NSCoding>)objectForKey:(NSString *)key;

- (void)objectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key, id<NSCoding> object))block;

- (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key;

- (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key withBlock:(nullable void(^)(void))block;

- (void)removeObjectForKey:(NSString *)key;

- (void)removeObjectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key))block;

- (void)removeAllObjects;

- (void)removeAllObjectsWithBlock:(void(^)(void))block;

- (void)removeAllObjectsWithProgressBlock:(nullable void(^)(int removedCount, int totalCount))progress

                                 endBlock:(nullable void(^)(BOOL error))end;

源文件中的一些方法实现初探:

- (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];

}


- (instancetype)initWithPath:(NSString *)path {

    if (path.length == 0) return nil;

    YYDiskCache *diskCache = [[YYDiskCache alloc] initWithPath:path];

    if (!diskCache) return nil;

    NSString *name = [path lastPathComponent];

    YYMemoryCache *memoryCache = [YYMemoryCache new];

    memoryCache.name = name;

    

    self = [super init];

    _name = name;

    _diskCache = diskCache;

    _memoryCache = memoryCache;

    return self;

}

初始化方法中在创建缓存时同时创建了YYDiskCache与YYMemoryCache

- (BOOL)containsObjectForKey:(NSString *)key {

    return [_memoryCache containsObjectForKey:key] || [_diskCache containsObjectForKey:key];

}

判断是否具有每个缓存时对 YYDiskCache与 YYMemoryCache同时进行判断

- (id<NSCoding>)objectForKey:(NSString *)key {

    id<NSCoding> object = [_memoryCache objectForKey:key];

    if (!object) {

        object = [_diskCache objectForKey:key];

        if (object) {

            [_memoryCache setObject:object forKey:key];

        }

    }

    return object;

}

读取缓存时先从YYMemoryCache中去读取数据,如果YYMemoryCache中不存在时就从YYDiskCache读取数据,并把数据存放在YYMemoryCache中

由以上方法可以确定,YYCache的操作同时是对YYDiskCache与YYMemoryCache的操作

具体使用方法如下:

1、同步缓存操作

func userYYCache() -> Void {

        let cacheName:String = "TongCache"

        let cache:YYCache = YYCache.init(name: cacheName)!

        let cacheKey:String = "TongCacheKey"

        print("开始进行同步存储数据")

        cache.setObject("data" as NSCoding, forKey: cacheKey)

        sleep(5)

        print("同步存储数据结束")

        

        print("开始同步判断是否含有缓存")

        let isContent:Bool = cache.containsObject(forKey: cacheKey)

        sleep(5)

        print("判断缓存是否存在结束")

        print("开始读取缓存数据")

        let data:String = cache.object(forKey: cacheKey) as! String

        sleep(5)

        print("读取缓存数据结束")

    }

执行结果如下:

开始进行同步存储数据

同步存储数据结束

开始同步判断是否含有缓存

判断缓存是否存在结束

开始读取缓存数据

读取缓存数据结束

block块进行数据操作

func userCacheForAsyn() -> Void {

        print("开始进行缓存操作")

        let cacheName:String = "YiCache"

        let cache:YYCache = YYCache.init(name: cacheName)!

        let cacheKey:String = "YiCacheKey"

        print("开始进行异步存储数据")

        cache.setObject("data" as NSCoding, forKey: cacheKey) {

            print("====")

        }

        sleep(5)

        print("异步存储数据结束")

        

        

        print("开始同步判断是否含有缓存")

        cache.containsObject(forKey: cacheKey) { (name, icContent) in

            print("name:\(name), icContent:\(icContent)")

        }

        sleep(3)

        print("判断缓存是否存在结束")


        

        print("开始读取缓存数据")

        cache.object(forKey: cacheKey) { (name, nSCoding) in

            print("name:\(name), coding:\(nSCoding)")

        }

        sleep(7)

        print("读取缓存数据结束")

        

        print("结束缓存操作")

    }

执行结果

开始进行缓存操作

开始进行同步存储数据

数据缓存完毕

同步存储数据结束

开始同步判断是否含有缓存

name:YiCacheKey, icContent:true

判断缓存是否存在结束

开始读取缓存数据

name:YiCacheKey, coding:data

读取缓存数据结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 iOS 中,可以使用 YYLabel 来显示富文本,包括 HTML 格式的富文本。YYLabel 是由 YYKit 提供的一个组件,它支持更多的文本属性设置,包括字体、颜色、行间距、字间距等等。 要在 YYLabel 中加载 HTML 富文本,可以使用 NSAttributedString 的 initWithData:options:documentAttributes:error: 方法来实现。具体步骤如下: 1. 将 HTML 字符串转换为 NSData 对象,可以使用 NSString 的 dataUsingEncoding: 方法来实现。 2. 使用 NSAttributedString 的 initWithData:options:documentAttributes:error: 方法,将 NSData 对象转换为 NSAttributedString 对象。 3. 将 NSAttributedString 对象赋值给 YYLabel 的 attributedText 属性,即可在 YYLabel 中显示 HTML 富文本。 下面是一个示例代码: ``` NSString *htmlString = @"<p>This is a <strong>bold</strong> text.</p>"; NSData *htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding]; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:htmlData options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil]; YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]; label.attributedText = attributedString; [self.view addSubview:label]; ``` 上面的代码中,我们首先将 HTML 字符串转换为 NSData 对象,然后使用 NSAttributedString 的 initWithData:options:documentAttributes:error: 方法将其转换为 NSAttributedString 对象。最后,我们将 NSAttributedString 对象赋值给 YYLabel 的 attributedText 属性,即可在 YYLabel 中显示 HTML 富文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值