This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-iOS/Pod/Classes/Plugin/DoricStoragePlugin.m

238 lines
7.3 KiB
Mathematica
Raw Normal View History

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-02-01 18:13:13 +08:00
#import "DoricExtensions.h"
#import "DoricSingleton.h"
#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;
- (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;
@end
@implementation PINCache (Doric)
2021-02-01 14:58:34 +08:00
- (void)setObject:(NSString *)value forKey:(NSString *)key withBlock:(void (^)(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);
}];
}
2021-02-01 14:58:34 +08:00
- (void)removeAllObjectsWithBlock:(void (^)(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
2021-02-01 18:13:13 +08:00
#else
@interface DoricCache : NSObject
- (instancetype)initWithName:(NSString *)prefix rootPath:(NSString *)path;
- (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
- (instancetype)initWithName:(NSString *)prefix rootPath:(NSString *)path {
return [self init];
}
- (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 (^)())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 ()
@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;
}
- (DoricCache *)defaultCache {
2019-11-22 11:11:22 +08:00
if (!_defaultCache) {
_defaultCache = [[DoricCache alloc] initWithName:doric_prefix rootPath:self.basePath];
2019-11-22 11:11:22 +08:00
}
return _defaultCache;
}
- (DoricCache *)getDiskCache:(NSString *)zone {
DoricCache *diskCache;
2019-11-22 11:11:22 +08:00
if (zone) {
diskCache = self.cachedMap[zone];
if (!diskCache) {
diskCache = [DoricSingleton.instance.storageCaches objectForKey:zone];
if (!diskCache) {
diskCache = [[DoricCache alloc] initWithName:[NSString stringWithFormat:@"%@_%@", doric_prefix, zone]
rootPath:self.basePath];
[DoricSingleton.instance.storageCaches setObject:diskCache forKey:zone];
}
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 optString:@"zone"];
NSString *key = [argument optString:@"key"];
NSString *value = [argument optString:@"value"];
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 optString:@"zone"];
NSString *key = [argument optString:@"key"];
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 optString:@"zone"];
NSString *key = [argument optString:@"key"];
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 optString:@"zone"];
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