Giter Club home page Giter Club logo

share's Introduction

##背景 想必大家一定用过 sharedpreferences 吧!就我个人而言,特别讨厌每次 put 完数据还要 commit。对 我就是这么懒!哈哈。另外,sharedpreferences 不能存类,集合和bitmap等数据!这点也让人非常不爽啊!所以,我就在这个美好的星期天撸了名为 SHARE 的工具类用来替代 sharedpreferences。


##项目介绍

整体架构

先来看一下,整体架构图(画的不好请大家见谅):

图片

从图中,我们可以了解到,当我们 put 数据的时候,我们同时存入到 内存和和sd卡中。读取的时候,优先从内存中获取,如果内存中没有,则从sd中获取。如果两者都没有,则使用用户自己设置的默认值!

代码介绍

下来看一下代码目录结构: 图片

  • DiskLruCache:硬盘缓存的解决方案(非Google官方编写,但获得官方认证。地址)
  • Cache:接口。抽象了对数据的操作
  • MemoryCache:内存缓存,实现了Cache接口
  • DiskCache:硬盘缓存,同样实现了Cache接口
  • Share:这个类就是我们使用的!他主要是提供了 put 和 get两种方法!其实就是对 MemoryCache 和 DiskCache 两个类的操作!

使用

在 Application中初始化:

@Override
public void onCreate() {
    super.onCreate();
    File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "sample");
    if (!file.exists()) {
        file.mkdirs();
    }
    Share.init("CACHE", 10 * 1024, file.toString());
}

之后,你就可以任意的使用它了!

    //设置字符串
    Share.putString("str", "你好啊");
    //设置int
    Share.putInt("int", 1);
    //设置boolean
    Share.putBoolean("boolean", true);
    //设置double
    Share.putDouble("double", 2.1d);
    //设置long
    Share.putLong("long", 20000);
    //设置flot
    Share.putFloat("float", 2.2f);
    //设置类
    Share.putObject("obj", people);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dd);
    //设置bitmap
    Share.putBitmap("bitmap", bitmap);
    //设置集合
    Share.putObject("list", items);

    //得到字符串
    String str=Share.getString("str");
    //得到double
    double dd=Share.getDouble("double", 0.0d);
    //得到int
    int value=Share.getInt("int", 0);
    //得到float
    float ff=Share.getFloat("float", 0.0f);
    //得到bitmap
    Bitmap map=Share.getBitmap("bitmap");
    //得到集合
    List<String> copy= (List<String>) Share.getObject("list");
    .....

使用就是如此简单!

近期要完成的新功能

1.对泛型的支持.


##项目地址

希望这个项目对大家有用。也希望多 star .同时也能多多提出修改意见!不管是对项目本身还是代码!!!!

share's People

Watchers

Manoj Behera avatar

Forkers

mkbehera2

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.