首页 > 其他分享 >shared_preferences缓存

shared_preferences缓存

时间:2023-12-27 09:22:06浏览次数:17  
标签:key 缓存 return preferences static SharedPreferences shared String

封装

import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class JSpUtil {
  JSpUtil._internal(); // 私有的构造方法,防止外部实例化

  factory JSpUtil() => _instance; // 工厂方法,返回 JSpUtil 唯一实例

  static late final JSpUtil _instance = JSpUtil._internal(); // JSpUtil 唯一实例

  static late SharedPreferences _preferences; // SharedPreferences 对象

  static Future<JSpUtil> getInstance() async {
    _preferences = await SharedPreferences.getInstance(); // 获取 SharedPreferences 实例
    return _instance;
  }

  /// 根据key存储int类型
  static Future<bool> setInt(String key, int value) {
    return _preferences.setInt(key, value); // 使用 SharedPreferences 存储 int 类型的数据
  }

  /// 根据key获取int类型
  static int? getInt(String key, {int defaultValue = 0}) {
    return _preferences.getInt(key) ?? defaultValue; // 使用 SharedPreferences 获取 int 类型数据,如果不存在则返回默认值
  }

  /// 根据key存储double类型
  static Future<bool> setDouble(String key, double value) {
    return _preferences.setDouble(key, value); // 使用 SharedPreferences 存储 double 类型的数据
  }

  /// 根据key获取double类型
  static double? getDouble(String key, {double defaultValue = 0.0}) {
    return _preferences.getDouble(key) ?? defaultValue; // 使用 SharedPreferences 获取 double 类型数据,如果不存在则返回默认值
  }

  /// 根据key存储字符串类型
  static Future<bool> setString(String key, String value) {
    return _preferences.setString(key, value); // 使用 SharedPreferences 存储字符串类型的数据
  }

  /// 根据key获取字符串类型
  static String? getString(String key, {String defaultValue = ""}) {
    return _preferences.getString(key) ?? defaultValue; // 使用 SharedPreferences 获取字符串类型数据,如果不存在则返回默认值
  }

  /// 根据key存储布尔类型
  static Future<bool> setBool(String key, bool value) {
    return _preferences.setBool(key, value); // 使用 SharedPreferences 存储布尔类型的数据
  }

  /// 根据key获取布尔类型
  static bool? getBool(String key, {bool defaultValue = false}) {
    return _preferences.getBool(key) ?? defaultValue; // 使用 SharedPreferences 获取布尔类型数据,如果不存在则返回默认值
  }

  /// 根据key存储字符串类型数组
  static Future<bool> setStringList(String key, List<String> value) {
    return _preferences.setStringList(key, value); // 使用 SharedPreferences 存储字符串类型数组
  }

  /// 根据key获取字符串类型数组
  static List<String> getStringList(String key,
      {List<String> defaultValue = const []}) {
    return _preferences.getStringList(key) ?? defaultValue; // 使用 SharedPreferences 获取字符串类型数组,如果不存在则返回默认值
  }

  /// 根据key存储Map类型
  static Future<bool> setMap(String key, Map value) {
    return _preferences.setString(key, json.encode(value)); // 使用 SharedPreferences 存储 Map 类型的数据,将 Map 转为 json 字符串存储
  }

  /// 根据key获取Map类型
  static Map getMap(String key) {
    String jsonStr = _preferences.getString(key) ?? ""; // 使用 SharedPreferences 获取存储的 json 字符串
    return jsonStr.isEmpty ? Map : json.decode(jsonStr); // 将 json 字符串解码为 Map 对象,如果 json 为空则返回空 Map 对象
  }
}

 

标签:key,缓存,return,preferences,static,SharedPreferences,shared,String
From: https://www.cnblogs.com/xbinbin/p/17929791.html

相关文章

  • uniGUI学习之禁用缓存(75)
    引用了第三方js,css等文件,但是因为缓存的原因,修改后没有及时生效。方法一:在ServerModule的OnHTTPCommand事件中,加入以下代码AResponseInfo.CacheControl:='max-age=0,no-cache,no-store,must-revalidate,proxy-revalidate';但是此方法有缺点,只支持exe模式,exe模式下所有的请求......
  • mybatis的一级、二级缓存
    Mybatis的一级、二级缓存一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为Session,当Session进行flush或close之后,该Session中的所有Cache就将清空,默认打开一级缓存二级缓存是基于namespace和mapper的作用域起作用的,不是依赖于SQLsession,默认也是采用PerpetualCach......
  • 借shared_ptr实现写时复制(COW)
    原理1、使用智能指针管理共享资源2、write端,若引用计数为1,则write端独占资源,若引用计数不为1,则对共享资源备份进行写操作,以确保线程安全3、read端,读之前引用计数加1,write端此时若并发访问共享资源,则会发现引用计数不为1,write端不会直接写共享资源,确保线程安全代码#include<m......
  • SpringBoot多级缓存
    SpringBoot实现项目更删改查后,会有新的问题需要解决,就是并发大的问题,一般而言,解决查询并发大的问题,常见的手段是为查询接口增加缓存,从而可以减轻持久层的压力。按照我们以往的经验,在查询接口中增加Redis缓存即可,将查询的结果数据存储到Redis中,执行查询时首先从Redis中命中,如果命中......
  • 看看 Asp.net core Webapi 项目如何优雅地使用分布式缓存
    前言缓存是提升程序性能必不可少的方法,Asp.netcore支持多级缓存配置,主要有客户端缓存、服务器端缓存,内存缓存和分布式缓存等。其中客户端缓和服务器端缓存在使用上都有比较大的限制,而内存缓和分布式缓存则比较灵活。分布式缓存是一种用于存储和管理数据的技术,它将数据存储在......
  • Python:diskcache实现基于文件的数据缓存
    diskcache是一个基于Sqlite文件的数据缓存如果在不想使用redis作为缓存的情况下,可以使用diskcache替代,减少对redis的依赖文档https://grantjenks.com/docs/diskcache/https://github.com/grantjenks/python-diskcachehttps://pypi.org/project/diskcache/安装pipinstall......
  • 缓存双写一致性之更新策略探讨
    缓存双写一致性之更新策略探讨面试题上面业务逻辑你用java代码如何写?你只要用缓存,就可能涉及到Redis缓存与数据库双存储双写,只要是双写就一定会有数据一致性的问题,那么如何解决?双写一致性,你先动缓存Redis还是数据库MySQL?Why?延时双删你做过吗?会有哪些问题?有这么一种情况,微服......
  • 【flink番外篇】4、flink的sink(内置、mysql、kafka、redis、clickhouse、分布式缓存、
    文章目录Flink系列文章一、maven依赖二、Jdbc/mysql示例1、maven依赖2、实现1)、userbean2)、内部匿名类实现3)、lambda实现4)、普通继承RichSinkFunction实现5)、完整代码3、验证本文介绍了Flink将数据sink到mysql中,其实是通过jdbc来将数据sink到rmdb中,mysql是一个常见的数据库,故......
  • Spring 解决循环依赖为什么需要三级缓存,而不是两级缓存?
    ......
  • [群晖]DSM SSD Cache缓存寿命与S.M.A.R.T识别硬盘真伪
    群晖NASSSDCache缓存机制缓存技术不是群晖独有,但是群晖科技公司有其自己开发的缓存算法。这种缓存机制的主要目的是通过使用固态硬盘(SSD)的高速读写能力,来提高存储设备的数据处理速度和性能。具体来说,群晖SSDCache缓存机制的工作原理是,当存储设备接收到数据读写请求时,首先会检查......