BDPKVManager.h 2.87 KB
//
//  BDPKVManager.h
//  BDPKV
//
//  Created by performance for gaobingnan on 2021/3/1.
//

#import <Foundation/Foundation.h>
#import "BDPMemoryKV.h"
#import "BDPDiskKV.h"

NS_ASSUME_NONNULL_BEGIN

typedef NS_OPTIONS(NSUInteger, BDPKVMode) {
    BDPKVModeMemory = (0x1 << 1),           // 仅内存模式
    BDPKVModeDisk = (0x1 << 2),             // 磁盘模式,内部同时支持内存缓存
    BDPKVModeMemoryAndDisk = (0x1 << 3),    // 磁盘模式+内存模式(适用于内存和磁盘存储的内容相互独立)
};

@interface BDPKVManager : NSObject

/// name of kv object
@property (nonatomic, copy, readonly) NSString *name;

/// only memory kv
@property (nonatomic, strong, readonly) BDPMemoryKV *memoryKV;

/// disk kv, include memory kv
@property (nonatomic, strong, readonly) BDPDiskKV *diskKV;

#pragma mark - create Methods
/**
 Create a new instance with the specified name.
 ⚠️Multiple instances with the same name will make the kv unstable.
 The default mode is BDPKVModeMemory.
 
 @param name  The name of the kv.
 @result A new kv object, or nil if an error occurs.
 */
- (nullable instancetype)initWithName:(NSString *)name;

/**
 Create a new instance with the specified name.
 ⚠️Multiple instances with the same name will make the kv unstable.
 The default sandbox path is Documents/bdpkv/disk/.
 
 @param name  The name of the kv.
 @param mode  The mode of the kv.
 @result A new kv object, or nil if an error occurs.
 */
- (nullable instancetype)initWithName:(NSString *)name mode:(BDPKVMode)mode;

/**
 Create a new instance with the specified path.
 ⚠️Multiple instances with the same name will make the kv unstable.
 The default value is BDPKVModeMemory.
 
 @param path  Full path of a directory in which the kv will write data.
     Once initialized you should not read and write to this directory.
 @result A new kv object, or nil if an error occurs.
 */
- (nullable instancetype)initWithPath:(NSString *)path;

/**
 Create a new instance with the specified path.
 Multiple instances with the same name will make the kv unstable.
 
 @param path  Full path of a directory in which the kv will write data.
     Once initialized you should not read and write to this directory.
 @param mode  The mode of the kv.
 @result A new kv object, or nil if an error occurs.
 */
- (nullable instancetype)initWithPath:(NSString *)path mode:(BDPKVMode)mode;

/**
 find instance with the specified name.
 Multiple instances with the same name will make the kv unstable.
 
 @param name  The name of the kv.
 @result A created kv object, or nil if not find.
 */
+ (nullable instancetype)findKVWithName:(NSString *)name;

/**
 find instance with the specified name.
 Multiple instances with the same name will make the kv unstable.
 
 @param path  The path of the kv.
 @result A created kv object, or nil if not find.
 */
+ (nullable instancetype)findKVWithPath:(NSString *)path;

@end

NS_ASSUME_NONNULL_END