Giter Club home page Giter Club logo

fastsharedpreferences's Introduction

FastSharedPreferences——高性能Android key-value组件

FastSharedPreferences是一个Android平台的高性能key-value组件。

FastSharedPreferences特点

  1. 实现了SharedPreferences和SharedPreferences.Editor接口,便于无缝替换SharedPreferences。
  2. 增强了SharedPreferences,增加了写入/读取Serializable的接口。
  3. 高效的写入/读取性能,读写性能相比SharedPreferences增强了200-300倍
  4. 纯java实现,很好的兼容性和稳定性
  5. 实现代码简洁,aar体积很小
  6. 支持跨进程使用(同时实现高性能和跨进程的难度超出预期,这一点后面会逐渐优化)
  7. 支持基于LRU的缓存管理,减少内存使用

FastSharedPreferences原理

  • 适合高强度/高频次写入读取

    FastSharedPreferences由Cache层和同步层构成,特别适合高强度/高频次写入读取数据。

  • 内存Cache

    基于ConcurrentHashMap的内存Cache层,不仅提高了写入性能,也极大的提高了读取性能。

  • 同步管理

    通过脏数据标记等技术,减少了同步的次数,使得慢速的I/O不再成为读写速度的瓶颈。

fsp_principle

FastSharedPreferences使用指南

接入

dependencies {
    compile 'com.jeremyliao:fast-sharedpreferences:0.0.1'
}

快速上手

在application onCreate中初始化:
FastSharedPreferences.init(context);
用法和SharedPreferences几乎完全一致,例如:
  • 写入一个整数
FastSharedPreferences sharedPreferences = FastSharedPreferences.get(FSP_ID);
sharedPreferences.edit().putInt("test_key", 100).apply();
  • 读取一个整数
FastSharedPreferences sharedPreferences = FastSharedPreferences.get(FSP_ID);
        int ret = sharedPreferences.getInt("test_key", -1);

性能对比

我们将 FastSharedPreferences同 SharedPreferences、SQLite以及腾讯的MMKV进行对比,测试的结果是对每项测试重复200次取平均值(对SharedPreferences和SQLite没有重复这么多次,因为时间太长了:))。相关测试代码参见benchmark

  • 写入1000个整数

benchmark_write_int

(测试机器是 Nexus 6 64G,Android 7.1.1,每组操作重复 200次,时间单位是 ms)

  • 读取1000个整数

benchmark_read_int

(测试机器是 Nexus 6 64G,Android 7.1.1,每组操作重复 200次,时间单位是 ms)

fastsharedpreferences's People

Contributors

jeremyliao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.