2019-11-22 11:11:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/11/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricStoragePlugin.h"
|
2021-01-29 18:03:02 +08:00
|
|
|
|
|
|
|
#if __has_include(<PINCache/PINCache.h>)
|
|
|
|
|
|
|
|
#import <PINCache/PINCache.h>
|
|
|
|
|
|
|
|
#define DoricCache PINCache
|
|
|
|
|
|
|
|
@interface PINCache (Doric)
|
2021-02-01 14:58:34 +08:00
|
|
|
- (void)setObject:(NSString *)value forKey:(NSString *)key withBlock:(void (^)(void))block;
|
2021-01-29 18:03:02 +08:00
|
|
|
|
|
|
|
- (void)objectForKey:(NSString *)key withBlock:(void (^)(NSString *, id <NSCoding>))block;
|
|
|
|
|
|
|
|
- (void)removeObjectForKey:(NSString *)key withBlock:(void (^)(NSString *))block;
|
|
|
|
|
2021-02-01 14:58:34 +08:00
|
|
|
- (void)removeAllObjectsWithBlock:(void (^)(void))pFunction;
|
2021-01-29 18:03:02 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation PINCache (Doric)
|
|
|
|
|
2021-02-01 14:58:34 +08:00
|
|
|
- (void)setObject:(NSString *)value forKey:(NSString *)key withBlock:(void (^)(void))block {
|
2021-01-29 18:03:02 +08:00
|
|
|
[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);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2021-02-01 14:58:34 +08:00
|
|
|
- (void)removeAllObjectsWithBlock:(void (^)(void))block {
|
2021-01-29 18:03:02 +08:00
|
|
|
[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
|
|
|
|
|
2019-11-22 11:11:22 +08:00
|
|
|
|
2019-11-22 13:19:54 +08:00
|
|
|
static NSString *doric_prefix = @"pref";
|
2019-11-22 11:11:22 +08:00
|
|
|
|
|
|
|
@interface DoricStoragePlugin ()
|
2021-01-29 18:03:02 +08:00
|
|
|
@property(atomic, strong) NSMutableDictionary <NSString *, DoricCache *> *cachedMap;
|
|
|
|
@property(nonatomic, strong) DoricCache *defaultCache;
|
2019-11-22 13:19:54 +08:00
|
|
|
@property(nonatomic, copy) NSString *basePath;
|
2019-11-22 11:11:22 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricStoragePlugin
|
2019-11-22 13:19:54 +08:00
|
|
|
- (instancetype)initWithContext:(DoricContext *)doricContext {
|
|
|
|
if (self = [super initWithContext:doricContext]) {
|
|
|
|
_basePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
|
|
|
|
stringByAppendingPathComponent:@"doric"];
|
2019-11-22 17:07:16 +08:00
|
|
|
_cachedMap = [NSMutableDictionary new];
|
2019-11-22 13:19:54 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:03:02 +08:00
|
|
|
- (DoricCache *)defaultCache {
|
2019-11-22 11:11:22 +08:00
|
|
|
if (!_defaultCache) {
|
2021-01-29 18:03:02 +08:00
|
|
|
_defaultCache = [[DoricCache alloc] initWithName:doric_prefix rootPath:self.basePath];
|
2019-11-22 11:11:22 +08:00
|
|
|
}
|
|
|
|
return _defaultCache;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:03:02 +08:00
|
|
|
- (DoricCache *)getDiskCache:(NSString *)zone {
|
|
|
|
DoricCache *diskCache;
|
2019-11-22 11:11:22 +08:00
|
|
|
if (zone) {
|
|
|
|
diskCache = self.cachedMap[zone];
|
|
|
|
if (!diskCache) {
|
2021-01-29 18:03:02 +08:00
|
|
|
diskCache = [[DoricCache alloc] initWithName:[NSString stringWithFormat:@"%@_%@", doric_prefix, zone]
|
|
|
|
rootPath:self.basePath];
|
2019-11-22 11:11:22 +08:00
|
|
|
self.cachedMap[zone] = diskCache;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
diskCache = self.defaultCache;
|
|
|
|
}
|
|
|
|
return diskCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setItem:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
|
|
|
|
NSString *zone = argument[@"zone"];
|
|
|
|
NSString *key = argument[@"key"];
|
|
|
|
NSString *value = argument[@"value"];
|
2021-01-29 18:03:02 +08:00
|
|
|
DoricCache *diskCache = [self getDiskCache:zone];
|
2019-11-22 11:11:22 +08:00
|
|
|
[diskCache setObject:value forKey:key withBlock:^{
|
|
|
|
[promise resolve:nil];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)getItem:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
|
|
|
|
NSString *zone = argument[@"zone"];
|
|
|
|
NSString *key = argument[@"key"];
|
2021-01-29 18:03:02 +08:00
|
|
|
DoricCache *diskCache = [self getDiskCache:zone];
|
2020-08-07 10:53:07 +08:00
|
|
|
[diskCache objectForKey:key withBlock:^(NSString *_Nonnull key, id <NSCoding> _Nullable object) {
|
|
|
|
[promise resolve:object];
|
2019-11-22 11:11:22 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)remove:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
|
|
|
|
NSString *zone = argument[@"zone"];
|
|
|
|
NSString *key = argument[@"key"];
|
2021-01-29 18:03:02 +08:00
|
|
|
DoricCache *diskCache = [self getDiskCache:zone];
|
2019-11-22 11:11:22 +08:00
|
|
|
[diskCache removeObjectForKey:key withBlock:^(NSString *key) {
|
|
|
|
[promise resolve:nil];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)clear:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
|
|
|
|
NSString *zone = argument[@"zone"];
|
2021-01-29 18:03:02 +08:00
|
|
|
DoricCache *diskCache = [self getDiskCache:zone];
|
2019-11-22 11:11:22 +08:00
|
|
|
[diskCache removeAllObjectsWithBlock:^{
|
|
|
|
[promise resolve:nil];
|
|
|
|
}];
|
|
|
|
}
|
2021-02-01 14:58:34 +08:00
|
|
|
@end
|