基于MMKV 封装了一个通用的缓存工具类

2022-12-19  本文已影响0人  YannChee

由于YYCache 在iOS15以上系统有bug,且停止维护很多年,缓存框架最近切换成MMKV 是微信18年开源的一框缓存库,支持多个平台,截止目前Star 15K+

虽然可以使用SwiftyUserDefaults 这个框架呢,每次还得在调用地方增加DefaultsKey 的extension,在extension 定义DefaultsKey的key,使用起来很不方便而且对框架耦合比较高;而且性能也不高

SwiftyUserDefaults是对系统NSUserDefauls扩展,MMKV的性能要比前者高出3个数量级

基于MMKV 封装了一个通用的缓存工具类QYCacheManager,
主要提供了基于用户ID缓存和基于设备缓存的相关方法,可以拿到任意项目使用

QYCacheManager.h文件

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface QYCacheManager : NSObject 

/** app启动后调用 */
+ (void)initConfigWhenAppDidLaunch;

/** 设置UserID 关联缓存,分别在登录成功 和 退出登录调用 */
+ (void)bindUserID:(NSInteger)userID;

// 关联用户的缓存读写
+ (void)cacheBindUserObject:(nullable NSObject<NSCoding> *)object forKey:(NSString *)key;
+ (id<NSCoding>)objectBindUserForKey:(NSString *)key objClass:(Class)objClass;
+ (void)removeBindUserObjectForKey:(NSString *)key;
+ (void)clearAllBindUserObject;

// 不关联用户的缓存读写
+ (void)cacheWithoutUserObject:(nullable NSObject<NSCoding> *)object forKey:(NSString *)key;
+ (id<NSCoding>)objectWhithoutUserForKey:(NSString *)key objClass:(Class)objClass;
+ (void)removeObjectWhithoutUserForKey:(NSString *)key;
+ (void)clearAllWithoutUserObject;

+ (void)clearAll;

@end

NS_ASSUME_NONNULL_END

QYCacheManager.m文件


#import "QYCacheManager.h"
#import <MMKV/MMKV.h>

static NSString *mainPath = nil;
static NSString *deviceFlag = @"Device";

static NSUInteger userIDFlag = 0;

typedef enum : NSUInteger {
    QYCachePathTypeDefault, /**< 默认 不关联userID */
    QYCachePathTypeBindUser,/**< 关联userID */
} QYCachePathType;


@implementation QYCacheManager

#pragma mark - public methods

+ (void)initConfigWhenAppDidLaunch {
    
    mainPath = [NSString stringWithFormat:@"%@/QYCache", NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject];
    // 自定义rootDir,默认是{NSDocumentDirectory}/mmkv
    [MMKV initializeMMKV:mainPath];
}

+ (void)bindUserID:(NSInteger)userID {
    userIDFlag = userID;
}

+ (void)cacheBindUserObject:(nullable NSObject<NSCoding> *)object forKey:(NSString *)key {
    [self cacheObject:object withKey:key pathType:QYCachePathTypeBindUser];
}

+ (id<NSCoding>)objectBindUserForKey:(NSString *)key objClass:(Class)objClass {
    return [self objectForKey:key objClass:objClass pathType:QYCachePathTypeBindUser];
}

+ (id<NSCoding>)objectWhithoutUserForKey:(NSString *)key objClass:(Class)objClass {
    return [self objectForKey:key objClass:objClass pathType:QYCachePathTypeDefault];
}

+ (void)cacheWithoutUserObject:(nullable NSObject<NSCoding> *)object forKey:(NSString *)key {
    [self cacheObject:object withKey:key pathType:QYCachePathTypeDefault];
}

+ (void)clearAll {
    [self clearAllBindUserObject];
    [self clearAllWithoutUserObject];
}

+ (void)removeBindUserObjectForKey:(NSString *)key {
    [self removeObjectForKey:key pathType:QYCachePathTypeBindUser];
}

+ (void)removeObjectWhithoutUserForKey:(NSString *)key {
    [self removeObjectForKey:key pathType:QYCachePathTypeDefault];
}

+ (void)clearAllBindUserObject {
    [self.userMMKV clearAll];
}

+ (void)clearAllWithoutUserObject {
    [self.deviceMMKV clearAll];
}

#pragma mark - base methods
+ (void)cacheObject:(nullable NSObject<NSCoding> *)object
            withKey:(NSString *)key
           pathType:(QYCachePathType)pathType {
    
    MMKV *mmkv = (pathType == QYCachePathTypeDefault) ? self.deviceMMKV : self.userMMKV;
    if(object && key){
        [mmkv setObject:object forKey:key];
    }
}

+ (id<NSCoding>)objectForKey:(NSString *)key
                    objClass:(Class)objClass
                    pathType:(QYCachePathType)pathType {
    MMKV *mmkv = (pathType == QYCachePathTypeDefault) ? self.deviceMMKV : self.userMMKV;
    if(key && objClass){
        return [mmkv getObjectOfClass:objClass forKey:key];
    }
    return nil;
}

+ (void)removeObjectForKey:(NSString *)key pathType:(QYCachePathType)pathType {
    if (!key) {return;}
    MMKV *mmkv = (pathType == QYCachePathTypeDefault) ? self.deviceMMKV : self.userMMKV;
    [mmkv removeValueForKey:key];
}

#pragma mark - private methods

+ (MMKV *)deviceMMKV {
    static MMKV *deviceMmkv  = nil;
    if (!deviceMmkv) {
        deviceMmkv = [MMKV mmkvWithID:deviceFlag rootPath:[self getPathWithType:QYCachePathTypeDefault]];
    }
    return deviceMmkv;
}

+ (MMKV *)userMMKV {
    static MMKV *userMmkv  = nil;
    if (!userMmkv) {
        userMmkv = [MMKV mmkvWithID:deviceFlag rootPath:[self getPathWithType:QYCachePathTypeBindUser]];
    }
    return userMmkv;
}

// 获取硬盘路径
+ (NSString *)getPathWithType:(QYCachePathType)pathType {
    if (pathType == QYCachePathTypeDefault) {  // 拼接存储路径不带Uid
        NSString *path = [NSString stringWithFormat:@"%@/%@" ,mainPath, deviceFlag];
        return path;
    }
    
    //  拼接存储路径带Uid
    NSString *uid = self.userID == 0 ? @"Non_Uid" : @(self.userID).stringValue;
    NSString *path = [NSString stringWithFormat:@"%@/%@" , mainPath, uid];
    
    return path;
}


+ (NSUInteger)userID {
    return userIDFlag;
}

@end

上一篇下一篇

猜你喜欢

热点阅读