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/iOS/Pod/Classes/Plugin/DoricStoragePlugin.m

98 lines
3.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"
#import "YYDiskCache.h"
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 *, YYDiskCache *> *cachedMap;
@property(nonatomic, strong) YYDiskCache *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;
}
2019-11-22 11:11:22 +08:00
- (YYDiskCache *)defaultCache {
if (!_defaultCache) {
2019-11-22 13:19:54 +08:00
_defaultCache = [[YYDiskCache alloc] initWithPath:[self.basePath stringByAppendingPathComponent:doric_prefix]];
2019-11-22 11:11:22 +08:00
}
return _defaultCache;
}
- (YYDiskCache *)getDiskCache:(NSString *)zone {
YYDiskCache *diskCache;
if (zone) {
diskCache = self.cachedMap[zone];
if (!diskCache) {
2019-11-22 13:19:54 +08:00
diskCache = [[YYDiskCache alloc] initWithPath:[self.basePath
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@", doric_prefix, 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[@"zone"];
NSString *key = argument[@"key"];
NSString *value = argument[@"value"];
YYDiskCache *diskCache = [self getDiskCache:zone];
[diskCache setObject:value forKey:key withBlock:^{
[promise resolve:nil];
}];
}
- (void)getItem:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSString *zone = argument[@"zone"];
NSString *key = argument[@"key"];
YYDiskCache *diskCache = [self getDiskCache:zone];
[diskCache objectForKey:key withBlock:^(NSString *key, NSString *value) {
[promise resolve:value];
}];
}
- (void)remove:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSString *zone = argument[@"zone"];
NSString *key = argument[@"key"];
YYDiskCache *diskCache = [self getDiskCache:zone];
[diskCache removeObjectForKey:key withBlock:^(NSString *key) {
[promise resolve:nil];
}];
}
- (void)clear:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSString *zone = argument[@"zone"];
YYDiskCache *diskCache = [self getDiskCache:zone];
[diskCache removeAllObjectsWithBlock:^{
[promise resolve:nil];
}];
}
@end