iOS:Remove dependency of YYCache,add support for YYCache,TMCache,or PINCache

This commit is contained in:
pengfeizhou 2021-01-29 18:03:02 +08:00 committed by osborn
parent 4c78a91f7a
commit c74ceefc42
4 changed files with 122 additions and 15 deletions

View File

@ -21,7 +21,6 @@ Doric iOS SDK for cross platform develpment
} }
s.public_header_files = 'doric-iOS/Pod/Classes/**/*.h' s.public_header_files = 'doric-iOS/Pod/Classes/**/*.h'
s.dependency 'YYCache'
s.dependency 'YogaKit/Core' s.dependency 'YogaKit/Core'
s.dependency 'Yoga' s.dependency 'Yoga'
end end

View File

@ -9,9 +9,10 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#import "NavigationController.h" #import "NavigationController.h"
#import "ViewController.h" #import "ViewController.h"
#if __has_include(<SDWebImage/SDWebImage.h>)
#import <SDWebImage/SDWebImage.h> #import <SDWebImage/SDWebImage.h>
#import <SDWebImageWebPCoder/SDWebImageWebPCoder.h> #import <SDWebImageWebPCoder/SDWebImageWebPCoder.h>
#endif
@interface AppDelegate () @interface AppDelegate ()
@property(nonatomic, strong) UIViewController *rootVC; @property(nonatomic, strong) UIViewController *rootVC;
@property(nonatomic, strong) NavigationController *navigationController; @property(nonatomic, strong) NavigationController *navigationController;
@ -30,7 +31,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window.rootViewController = self.navigationController; self.window.rootViewController = self.navigationController;
[self.window addSubview:self.navigationController.view]; [self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible]; [self.window makeKeyAndVisible];
#if __has_include(<SDWebImage/SDWebImage.h>)
[SDImageCodersManager.sharedManager addCoder:SDImageWebPCoder.sharedCoder]; [SDImageCodersManager.sharedManager addCoder:SDImageWebPCoder.sharedCoder];
#endif
return YES; return YES;
} }

View File

@ -16,6 +16,11 @@ target 'Example' do
pod 'SDWebImageWebPCoder' pod 'SDWebImageWebPCoder'
pod 'YYCache'
#pod 'TMCache'
pod 'PINCache'
target 'ExampleTests' do target 'ExampleTests' do
inherit! :search_paths inherit! :search_paths
# Pods for testing # Pods for testing

View File

@ -18,13 +18,113 @@
// //
#import "DoricStoragePlugin.h" #import "DoricStoragePlugin.h"
#import "YYDiskCache.h"
#if __has_include(<PINCache/PINCache.h>)
#import <PINCache/PINCache.h>
#define DoricCache PINCache
@interface PINCache (Doric)
- (void)setObject:(NSString *)value forKey:(NSString *)key withBlock:(void (^)())block;
- (void)objectForKey:(NSString *)key withBlock:(void (^)(NSString *, id <NSCoding>))block;
- (void)removeObjectForKey:(NSString *)key withBlock:(void (^)(NSString *))block;
- (void)removeAllObjectsWithBlock:(void (^)())pFunction;
@end
@implementation PINCache (Doric)
- (void)setObject:(NSString *)value forKey:(NSString *)key withBlock:(void (^)())block {
[self setObjectAsync:value forKey:key completion:^(id <PINCaching> cache, NSString *key, id object) {
block();
}];
}
- (void)objectForKey:(NSString *)key withBlock:(void (^)(NSString *, id <NSCoding>))block {
[self objectForKeyAsync:key completion:^(id <PINCaching> cache, NSString *key, id object) {
block(key, object);
}];
}
- (void)removeObjectForKey:(NSString *)key withBlock:(void (^)(NSString *))block {
[self removeObjectForKeyAsync:key completion:^(id <PINCaching> cache, NSString *key, id object) {
block(key);
}];
}
- (void)removeAllObjectsWithBlock:(void (^)())block {
[self removeAllObjectsAsync:^(id <PINCaching> cache) {
block();
}];
}
@end
#elif __has_include(<YYCache/YYCache.h>)
#import <YYCache/YYCache.h>
@interface DoricCache : YYCache
- (instancetype)initWithName:(NSString *)prefix rootPath:(NSString *)path;
@end
@implementation DoricCache
- (instancetype)initWithName:(NSString *)prefix rootPath:(NSString *)path {
return [self initWithPath:[path
stringByAppendingPathComponent:prefix]];
}
@end
#elif __has_include(<TMCache/TMCache.h>)
#import <TMCache/TMCache.h>
@interface DoricCache : TMCache
- (void)setObject:(NSString *)value forKey:(NSString *)key withBlock:(void (^)())block;
- (void)objectForKey:(NSString *)key withBlock:(void (^)(NSString *, id <NSCoding>))block;
- (void)removeObjectForKey:(NSString *)key withBlock:(void (^)(NSString *))block;
- (void)removeAllObjectsWithBlock:(void (^)())pFunction;
@end
@implementation DoricCache
- (void)setObject:(NSString *)value forKey:(NSString *)key withBlock:(void (^)())block {
[self setObject:value forKey:key block:^(TMCache *cache, NSString *key, id object) {
block();
}];
}
- (void)objectForKey:(NSString *)key withBlock:(void (^)(NSString *, id <NSCoding>))block {
[self objectForKey:key block:^(TMCache *cache, NSString *key, id object) {
block(key, object);
}];
}
- (void)removeObjectForKey:(NSString *)key withBlock:(void (^)(NSString *))block {
[self removeObjectForKey:key block:^(TMCache *cache, NSString *key, id object) {
block(key);
}];
}
- (void)removeAllObjectsWithBlock:(void (^)())block {
[self removeAllObjects:^(TMCache *cache) {
block();
}];
}
@end
#endif
static NSString *doric_prefix = @"pref"; static NSString *doric_prefix = @"pref";
@interface DoricStoragePlugin () @interface DoricStoragePlugin ()
@property(atomic, strong) NSMutableDictionary <NSString *, YYDiskCache *> *cachedMap; @property(atomic, strong) NSMutableDictionary <NSString *, DoricCache *> *cachedMap;
@property(nonatomic, strong) YYDiskCache *defaultCache; @property(nonatomic, strong) DoricCache *defaultCache;
@property(nonatomic, copy) NSString *basePath; @property(nonatomic, copy) NSString *basePath;
@end @end
@ -38,20 +138,20 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
return self; return self;
} }
- (YYDiskCache *)defaultCache { - (DoricCache *)defaultCache {
if (!_defaultCache) { if (!_defaultCache) {
_defaultCache = [[YYDiskCache alloc] initWithPath:[self.basePath stringByAppendingPathComponent:doric_prefix]]; _defaultCache = [[DoricCache alloc] initWithName:doric_prefix rootPath:self.basePath];
} }
return _defaultCache; return _defaultCache;
} }
- (YYDiskCache *)getDiskCache:(NSString *)zone { - (DoricCache *)getDiskCache:(NSString *)zone {
YYDiskCache *diskCache; DoricCache *diskCache;
if (zone) { if (zone) {
diskCache = self.cachedMap[zone]; diskCache = self.cachedMap[zone];
if (!diskCache) { if (!diskCache) {
diskCache = [[YYDiskCache alloc] initWithPath:[self.basePath diskCache = [[DoricCache alloc] initWithName:[NSString stringWithFormat:@"%@_%@", doric_prefix, zone]
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@", doric_prefix, zone]]]; rootPath:self.basePath];
self.cachedMap[zone] = diskCache; self.cachedMap[zone] = diskCache;
} }
} else { } else {
@ -64,7 +164,7 @@ - (void)setItem:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSString *zone = argument[@"zone"]; NSString *zone = argument[@"zone"];
NSString *key = argument[@"key"]; NSString *key = argument[@"key"];
NSString *value = argument[@"value"]; NSString *value = argument[@"value"];
YYDiskCache *diskCache = [self getDiskCache:zone]; DoricCache *diskCache = [self getDiskCache:zone];
[diskCache setObject:value forKey:key withBlock:^{ [diskCache setObject:value forKey:key withBlock:^{
[promise resolve:nil]; [promise resolve:nil];
}]; }];
@ -73,7 +173,7 @@ - (void)setItem:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
- (void)getItem:(NSDictionary *)argument withPromise:(DoricPromise *)promise { - (void)getItem:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSString *zone = argument[@"zone"]; NSString *zone = argument[@"zone"];
NSString *key = argument[@"key"]; NSString *key = argument[@"key"];
YYDiskCache *diskCache = [self getDiskCache:zone]; DoricCache *diskCache = [self getDiskCache:zone];
[diskCache objectForKey:key withBlock:^(NSString *_Nonnull key, id <NSCoding> _Nullable object) { [diskCache objectForKey:key withBlock:^(NSString *_Nonnull key, id <NSCoding> _Nullable object) {
[promise resolve:object]; [promise resolve:object];
}]; }];
@ -82,7 +182,7 @@ - (void)getItem:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
- (void)remove:(NSDictionary *)argument withPromise:(DoricPromise *)promise { - (void)remove:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSString *zone = argument[@"zone"]; NSString *zone = argument[@"zone"];
NSString *key = argument[@"key"]; NSString *key = argument[@"key"];
YYDiskCache *diskCache = [self getDiskCache:zone]; DoricCache *diskCache = [self getDiskCache:zone];
[diskCache removeObjectForKey:key withBlock:^(NSString *key) { [diskCache removeObjectForKey:key withBlock:^(NSString *key) {
[promise resolve:nil]; [promise resolve:nil];
}]; }];
@ -90,7 +190,7 @@ - (void)remove:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
- (void)clear:(NSDictionary *)argument withPromise:(DoricPromise *)promise { - (void)clear:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSString *zone = argument[@"zone"]; NSString *zone = argument[@"zone"];
YYDiskCache *diskCache = [self getDiskCache:zone]; DoricCache *diskCache = [self getDiskCache:zone];
[diskCache removeAllObjectsWithBlock:^{ [diskCache removeAllObjectsWithBlock:^{
[promise resolve:nil]; [promise resolve:nil];
}]; }];