iOS-ZFJRedisLib-超便捷的键值对存储方案,Sqlite数据模型存储库,APP缓存解决方案

前言

本人以前写过一个数据模型存储方法,是我在上上家公司做缓存框架的时候写的,具体也就是在FMDB的基础上封装,直接对数据模型进行存储操作,如下:

《iOS-基于FMDB的操作封装,模型对象的增删改查》

最近本人也不是很忙,加之在Python项目中用到Redis和在APP中用到NSUserDefaults,我就想做一个便捷的基于数据库的键值对存储方案,当然这个方案并不比Redis优秀,我们也知道Redis是直接从内存中读取,所以速度方面没法说,但是在APP中我们不能把键值对放在内存中吧,本来APP的内存就不宽裕!

ZFJRedisLib

功能结构

ZFJRedisLib主要包含ZFJRedis和ZFJTable两部分,ZFJRedis主要是用于键值对存储,使用简单方便;ZFJTable主要是对于数据模型进行增删改查操作!全部API如下:

ZFJRedis

ZFJRedis将会自动在本地创建一个ZFJRedis的表,用于存储键值对数据;

1.存取值

字符串-NSString

    // NSString
    [ZFJRedis zfj_setValue:@"zhangfujie1128222====" forKey:@"name"];
    // 取值
    NSString *value = [ZFJRedis zfj_valueForKey:@"name"];
    NSLog(@"%@",value);

数字对象-NSNumber

    // NSNumber
    [ZFJRedis zfj_setValue:@(20) forKey:@"age"];
    // 取值
    NSNumber *age = [ZFJRedis zfj_valueForKey:@"age"];
    NSLog(@"%@",age);

数组-NSArray

    // NSArray
    [ZFJRedis zfj_setValue:@[@"111", @(222)] forKey:@"array1"];
    // 取值
    NSArray *array = [ZFJRedis zfj_valueForKey:@"array1"];
    NSLog(@"%@",array);

字典-NSDictionary

    // NSDictionary
    NSDictionary *dict = @{@"name":@"zhangfujie", @"age":@(22), @"sex":@"man"};
    [ZFJRedis zfj_setValue:dict forKey:@"zfj_infors"];
    // 取值
    NSDictionary *dict_value = [ZFJRedis zfj_valueForKey:@"zfj_infors"];
    NSLog(@"%@",dict_value);

数据-NSData

    // NSData
    NSData *data = [NSJSONSerialization dataWithJSONObject:@[@"aaaaa", @"bbbbb"] options:NSJSONWritingPrettyPrinted error:nil];
    [ZFJRedis zfj_setValue:data forKey:@"arrayData"];
    // 取值
    NSData *data_new = [ZFJRedis zfj_valueForKey:@"arrayData"];
    NSLog(@"data_new == %@",data_new);

数据模型-model

    // NSObject 数据模型
    ZFJModel *model = [[ZFJModel alloc] init];
    model.name = @"张福杰";
    model.age = 22;
    // 存
    [ZFJRedis zfj_setValue:model forKey:@"model"];
    // 取值
    ZFJModel *model_my = [ZFJRedis zfj_valueForKey:@"model"];
    NSLog(@"%@",model_my.name);
    NSLog(@"%ld",model_my.age);

批量存数据

    //根据字典赋值
    NSMutableDictionary *value_dict = [[NSMutableDictionary alloc] init];
    [value_dict setValue:@"aaaaaaaa" forKey:@"key_string"];
    [value_dict setValue:@(111) forKey:@"key_number"];
    [value_dict setValue:@[@"wwwwwww11111", @(222)] forKey:@"key_array"];
    [value_dict setValue:@{@"name":@"zhangfujie", @"age":@(22)} forKey:@"key_dict"];
    [value_dict setValue:model_my forKey:@"key_model"];
    [ZFJRedis zfj_setValuesForKeysWithDictionary:value_dict];

批量取数据

    NSDictionary *dict = [ZFJRedis zfj_dictionaryWithValuesForKeys:value_dict.allKeys];
    NSLog(@"dict == %@",dict);

2.查询

获取全部keys

    // 获取全部keys
    NSArray *keys = [ZFJRedis zfj_allKeys];
    NSLog(@"keys == %@",keys);

获取全部values

    // 获取全部values
    NSArray *values = [ZFJRedis zfj_allValues];
    NSLog(@"values == %@",values);

获取全部条数

    // 获取全部条数
    NSInteger count = [ZFJRedis zfj_count];
    NSLog(@"count == %ld",count);

判断key是否存在

    // 判断当前key是否存在
    BOOL isExist = [ZFJRedis zfj_isExistkey:@"name"];
    if(isExist){
        NSLog(@"name 存在");
    }else{
        NSLog(@"name 不存在");
    }

3.删除

根据key删除

    // 删除
    [ZFJRedis zfj_removeObjectForKey:@"array"];

根据keys删除

    // 根据keys删除数据
    NSArray *key_list = @[@"name", @"age"];
    [ZFJRedis zfj_removeObjectsForKeys:key_list];

删除全部

    // 删除全部
    [ZFJRedis zfj_removeAllObjects];

ZFJTable

ZFJTable是根据数据模型自动创建表,直接根据模型进行增删改查操作的!

1.创建表

    // 创建表
    BOOL isScu = [ZFJTable zfj_createTable:[ZFJModel class]];
    if(isScu){
        NSLog(@"ZFJModel表创建成功");
    }else{
        NSLog(@"ZFJModel表创建失败");
    }

2.删除表

    //删除表
    BOOL isScu = [ZFJTable zfj_removeTable:[ZFJModel class]];
    if(isScu){
        NSLog(@"ZFJModel表删除成功");
    }else{
        NSLog(@"ZFJModel表删除失败");
    }

3.删除全部表

    // 删除数据库
    BOOL isScu = [ZFJTable zfj_removeAllTables];
    if(isScu){
        NSLog(@"数据库删除成功");
    }else{
        NSLog(@"数据库删除失败");
    }

4.插入数据

    // 插入一条数据
    ZFJModel *model_0 = [[ZFJModel alloc] init];
    model_0.name = @"张福杰";
    model_0.age = 22;
    model_0.gender = @"男";
    [ZFJTable zfj_insertModel:model_0 completed:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
    
    // 插入多条数据
    ZFJModel *model_0 = [[ZFJModel alloc] init];
    model_0.name = @"张福杰";
    model_0.age = 22;
    model_0.gender = @"男";

    ZFJModel *model_1 = [[ZFJModel alloc] init];
    model_1.name = @"大白菜";
    model_1.age = 88;
    model_1.gender = @"女";
    NSArray *models = @[model_0, model_1];
    [ZFJTable zfj_insertModels:models completed:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];

5.删除数据

    // 删除数据
    BOOL isScu = [ZFJTable zfj_deleteModel:[ZFJModel class] where:@"name = '张福杰'"];
    if(isScu){
        NSLog(@"数据删除成功");
    }else{
        NSLog(@"数据删除失败");
    }

6.修改数据

    // 修改数据
    ZFJModel *model_2 = [[ZFJModel alloc] init];
    model_2.name = @"大白菜";
    model_2.age = 22;
    model_2.gender = @"女";
    [ZFJTable zfj_updateModel:model_2 byKey:@"name" completed:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
    
    // 修改多条数据
    ZFJModel *model_3 = [[ZFJModel alloc] init];
    model_3.name = @"张福杰";
    model_3.age = 222;
    model_3.gender = @"男男";

    ZFJModel *model_4 = [[ZFJModel alloc] init];
    model_4.name = @"大白菜";
    model_4.age = 222;
    model_4.gender = @"女女";

    NSArray *models = @[model_3, model_4];
    [ZFJTable zfj_updateModels:models byKey:@"name" completed:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];

7.数据查询

    // 查询全部数据
    [ZFJTable zfj_selectTable:[ZFJModel class] completed:^(NSError * _Nonnull error, NSArray * _Nonnull models) {
        for (ZFJModel *model in models) {
            NSLog(@"%@",model.name);
        }
    }];
    
    // 条件查询
    [ZFJTable zfj_selectTable:[ZFJModel class] where:@"name = '张福杰'" completed:^(NSError * _Nonnull error, NSArray * _Nonnull models) {
        for (ZFJModel *model in models) {
            NSLog(@"%@",model.name);
        }
    }];
    
    // 查询数据条数
    NSInteger count = [ZFJTable zfj_selectTableCount:[ZFJModel class]];
    NSLog(@"count == %ld",count);
    
    // 根据条件查询数量
    NSDictionary *parameters = @{@"name":@"张福杰", @"age":@(22)};
    NSString *sqlStr = @"name = '张福杰' and age = '22'";
    NSInteger count = [ZFJTable zfj_selectTableCount:[ZFJModel class] where:sqlStr];
    NSLog(@"count == %ld",count);

8.表扩展字段

    // 向某个表添加一个字段
    BOOL isScu = [ZFJTable zfj_addProperty:[ZFJModel class] propertyName:@"newPro_1"];
    if(isScu){
        NSLog(@"字段添加成功");
    }else{
        NSLog(@"字段添加失败");
    }

8.错误信息

    // 最后一条错误
    NSError *error = [ZFJTable zfj_lastTableError];
    NSLog(@"error == %@",error);

源码

DEMO地址:https://gitee.com/zfj1128/ZFJRedisLib

GitHub地址:https://github.com/zfjsyqk/ZFJRedisLib

使用教程

  1. pod ‘ZFJRedisLib’
  2. pod install
  3. 导入头文件#import “ZFJRedisHeader.h”

结束语

欢迎各位大佬提供宝贵的建议和意见,也欢迎大家进群或加本人QQ一起交流学习!

阅读 287

推荐阅读