Giter Club home page Giter Club logo

base-diskcache's Introduction

base-diskcache

DiskLruCache属于目前最好的Disk Cache库了,但是由于其的存取API,并不是特别好用。

ASimpleCache 提供的API属于比较好用的了。

于是萌生想法,对于其公开的API进行扩展,对外除了原有的存取方式以外,提供类似ASimpleCache那样比较简单的API用于存储,而内部的核心实现,依然是DiskLruCache原本的。

方法

put(String key, Bitmap bitmap)

put(String key, byte[] value)

put(String key, String value)

put(String key, JSONObject jsonObject)

put(String key, JSONArray jsonArray)

put(String key, Serializable value)

put(String key, Drawable value)

editor(String key).newOutputStream(0);//原有的方式

String getAsString(String key);

JSONObject getAsJson(String key)

JSONArray getAsJSONArray(String key)

<T> T getAsSerializable(String key)

Bitmap getAsBitmap(String key)

byte[] getAsBytes(String key)

Drawable getAsDrawable(String key)

InputStream get(String key);//原有的用法

简单测试

public class CacheTest extends AndroidTestCase
{
    DiskLruCacheHelper helper;
    private static final String TAG = "CacheTest";

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        Log.e(TAG, "setUp");
        helper = new DiskLruCacheHelper(getContext());
    }


    public void testString() throws IOException
    {
        helper.put("testString", "张鸿洋");
        assertEquals("张鸿洋", helper.getAsString("testString"));
    }

    public void testGetStringWithoutVal() throws IOException
    {
        assertEquals(null, helper.getAsString("zhy------zzzzz"));
    }

    public void testJson()
    {
        try
        {
            JSONObject jsonObject = new JSONObject("{\"name\":\"zhy\"}");
            helper.put("testJson", jsonObject);
            assertEquals(jsonObject.toString(), helper.getAsJson("testJson").toString());
        } catch (JSONException e)
        {
            e.printStackTrace();
        }
    }

    public void testSerializable()
    {
        User u = new User();
        u.name = "张鸿洋";
        helper.put("testSerializable", u);
        User u2 = helper.getAsSerializable("testSerializable");
        assertEquals(u.name, u2.name);
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    public void testBitmap()
    {
        Bitmap bm = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher);
        helper.put("testBitmap", bm);
        Bitmap bm2 = helper.getAsBitmap("testBitmap");

        assertEquals(bm.getByteCount(), bm2.getByteCount());
    }

    public void testDrawable()
    {
        Drawable d = getContext().getResources().getDrawable(R.mipmap.ic_launcher);
        helper.put("testDrawable", d);
        Drawable d2 = helper.getAsDrawable("testDrawable");
        assertNotNull(d2);
    }


    private static class User implements Serializable
    {
        String name;
    }

    @Override
    protected void tearDown() throws Exception
    {
        super.tearDown();
        Log.e(TAG, "tearDown");
        helper.close();
    }


}

关于我

base-diskcache's People

Contributors

binkery avatar hongyangandroid avatar notice501 avatar

Watchers

 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.