├── screenshoot1.png ├── screenshoot2.png ├── screenshoot3.png ├── screenshoot4.png ├── XDroidCacheExample.apk ├── XDroidCache ├── lint.xml ├── src │ └── com │ │ └── xdroid │ │ └── cache │ │ ├── interfaces │ │ ├── TimeUnit.java │ │ └── ICache.java │ │ ├── utils │ │ ├── ImageHelper.java │ │ └── CacheHelper.java │ │ ├── disk │ │ ├── Util.java │ │ ├── StrictLineReader.java │ │ └── DiskLruCache.java │ │ ├── SecondLevelCacheKit.java │ │ ├── manage │ │ ├── MemoryCacheManager.java │ │ └── DiskCacheManager.java │ │ └── memory │ │ └── LruCache.java ├── AndroidManifest.xml └── proguard-project.txt ├── XDroidCacheExample ├── ic_launcher-web.png ├── libs │ └── android-support-v4.jar ├── res │ ├── drawable-xhdpi │ │ ├── beauty.png │ │ └── ic_launcher.png │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── values │ │ ├── colos.xml │ │ ├── strings.xml │ │ └── styles.xml │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ └── layout │ │ ├── activity_cache_demo.xml │ │ ├── fragment_image.xml │ │ ├── fragment_object.xml │ │ ├── activity_main.xml │ │ └── fragment_charset.xml ├── proguard-project.txt ├── src │ └── com │ │ └── xdroid │ │ └── cache │ │ └── example │ │ ├── bean │ │ └── PersonBean.java │ │ ├── widget │ │ └── SquareButton.java │ │ ├── MainActivity.java │ │ ├── CacheDemoActivity.java │ │ └── fragment │ │ ├── ObjectFragment.java │ │ ├── ImageFragment.java │ │ └── CharsetFragment.java └── AndroidManifest.xml ├── .gitignore └── README.md /screenshoot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/screenshoot1.png -------------------------------------------------------------------------------- /screenshoot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/screenshoot2.png -------------------------------------------------------------------------------- /screenshoot3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/screenshoot3.png -------------------------------------------------------------------------------- /screenshoot4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/screenshoot4.png -------------------------------------------------------------------------------- /XDroidCacheExample.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/XDroidCacheExample.apk -------------------------------------------------------------------------------- /XDroidCache/lint.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /XDroidCacheExample/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/XDroidCacheExample/ic_launcher-web.png -------------------------------------------------------------------------------- /XDroidCacheExample/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/XDroidCacheExample/libs/android-support-v4.jar -------------------------------------------------------------------------------- /XDroidCacheExample/res/drawable-xhdpi/beauty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/XDroidCacheExample/res/drawable-xhdpi/beauty.png -------------------------------------------------------------------------------- /XDroidCacheExample/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/XDroidCacheExample/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /XDroidCacheExample/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/XDroidCacheExample/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /XDroidCacheExample/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/XDroidCacheExample/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /XDroidCacheExample/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robinxdroid/XDroidCache/HEAD/XDroidCacheExample/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /XDroidCacheExample/res/values/colos.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #E86767 4 | 5 | -------------------------------------------------------------------------------- /XDroidCacheExample/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | XDroidCacheExample 5 | {"name":"Robin","email":"735506404@robinx.net"} 6 | 7 | 8 | -------------------------------------------------------------------------------- /XDroidCache/src/com/xdroid/cache/interfaces/TimeUnit.java: -------------------------------------------------------------------------------- 1 | package com.xdroid.cache.interfaces; 2 | 3 | /** 4 | * The cache's time unit 5 | * @author Robin 6 | * @since 2015-11-17 18:45:09 7 | * 8 | */ 9 | public interface TimeUnit { 10 | 11 | public int YEAR = 0x01,MONTH = 0x02,WEEK = 0x03,DAY = 0x04,HOUR = 0x05,MINUTES = 0x06,SECOND = 0x07; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /XDroidCache/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /XDroidCacheExample/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /XDroidCacheExample/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /XDroidCacheExample/res/layout/activity_cache_demo.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /XDroidCacheExample/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | #*.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | /XDroidCache/bin/* 29 | /XDroidCache/gen/* 30 | /XDroidCache/.classpath 31 | /XDroidCache/.project 32 | /XDroidCache/project.properties 33 | /XDroidCache/.settings/ 34 | /XDroidCacheExample/bin/* 35 | /XDroidCacheExample/gen/* 36 | /XDroidCacheExample/.classpath 37 | /XDroidCacheExample/.project 38 | /XDroidCacheExample/project.properties 39 | /XDroidCacheExample/.settings/ 40 | -------------------------------------------------------------------------------- /XDroidCache/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /XDroidCacheExample/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /XDroidCacheExample/src/com/xdroid/cache/example/bean/PersonBean.java: -------------------------------------------------------------------------------- 1 | package com.xdroid.cache.example.bean; 2 | 3 | import java.io.Serializable; 4 | 5 | public class PersonBean implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private String name; 9 | private String email; 10 | 11 | public PersonBean(String name, String email) { 12 | super(); 13 | this.name = name; 14 | this.email = email; 15 | } 16 | 17 | public PersonBean() { 18 | super(); 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getEmail() { 30 | return email; 31 | } 32 | 33 | public void setEmail(String email) { 34 | this.email = email; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "PersonBean [name=" + name + ", email=" + email + "]"; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /XDroidCacheExample/src/com/xdroid/cache/example/widget/SquareButton.java: -------------------------------------------------------------------------------- 1 | package com.xdroid.cache.example.widget; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.widget.TextView; 6 | 7 | public class SquareButton extends TextView { 8 | public SquareButton(Context context, AttributeSet attrs, int defStyle) { 9 | super(context, attrs, defStyle); 10 | } 11 | 12 | public SquareButton(Context context, AttributeSet attrs) { 13 | super(context, attrs); 14 | } 15 | 16 | public SquareButton(Context context) { 17 | super(context); 18 | } 19 | 20 | @Override 21 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 22 | 23 | setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec)); 24 | 25 | int childWidthSize = getMeasuredWidth(); 26 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY); 27 | heightMeasureSpec = widthMeasureSpec; 28 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 29 | } 30 | } -------------------------------------------------------------------------------- /XDroidCacheExample/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 12 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /XDroidCache/src/com/xdroid/cache/utils/ImageHelper.java: -------------------------------------------------------------------------------- 1 | package com.xdroid.cache.utils; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.graphics.Canvas; 8 | import android.graphics.PixelFormat; 9 | import android.graphics.drawable.BitmapDrawable; 10 | import android.graphics.drawable.Drawable; 11 | 12 | /** 13 | * Image helper 14 | * @author Robin 15 | * @since 2015-11-23 19:36:35 16 | * 17 | */ 18 | public class ImageHelper { 19 | public static byte[] bitmap2Bytes(Bitmap bm) { 20 | if (bm == null) { 21 | return null; 22 | } 23 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 24 | bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 25 | return baos.toByteArray(); 26 | } 27 | 28 | public static Bitmap bytes2Bitmap(byte[] bytes) { 29 | return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 30 | } 31 | 32 | public static Bitmap drawable2Bitmap(Drawable drawable) { 33 | if (drawable == null) { 34 | return null; 35 | } 36 | int w = drawable.getIntrinsicWidth(); 37 | int h = drawable.getIntrinsicHeight(); 38 | Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 39 | : Bitmap.Config.RGB_565; 40 | Bitmap bitmap = Bitmap.createBitmap(w, h, config); 41 | Canvas canvas = new Canvas(bitmap); 42 | drawable.setBounds(0, 0, w, h); 43 | drawable.draw(canvas); 44 | return bitmap; 45 | } 46 | 47 | @SuppressWarnings("deprecation") 48 | public static Drawable bitmap2Drawable(Bitmap bm) { 49 | if (bm == null) { 50 | return null; 51 | } 52 | BitmapDrawable bd = new BitmapDrawable(bm); 53 | bd.setTargetDensity(bm.getDensity()); 54 | return new BitmapDrawable(bm); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /XDroidCacheExample/src/com/xdroid/cache/example/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.xdroid.cache.example; 2 | 3 | import com.xdroid.cache.example.CacheDemoActivity.DemoType; 4 | 5 | import android.app.Activity; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import android.view.View; 9 | import android.view.View.OnClickListener; 10 | 11 | public class MainActivity extends Activity implements OnClickListener { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_main); 17 | 18 | init(); 19 | } 20 | 21 | private void init() { 22 | int[] ids = new int[] { R.id.btn_string, R.id.btn_jso, R.id.btn_jsa, R.id.btn_byte, R.id.btn_bitmap, 23 | R.id.btn_drawable, R.id.btn_serialize }; 24 | for (int i = 0; i < ids.length; i++) { 25 | findViewById(ids[i]).setOnClickListener(this); 26 | } 27 | 28 | } 29 | 30 | @Override 31 | public void onClick(View v) { 32 | Intent intent = new Intent(this, CacheDemoActivity.class); 33 | switch (v.getId()) { 34 | case R.id.btn_string: 35 | intent.putExtra("demo_type", DemoType.CHARSET); 36 | break; 37 | case R.id.btn_jso: 38 | intent.putExtra("demo_type", DemoType.CHARSET); 39 | break; 40 | case R.id.btn_jsa: 41 | intent.putExtra("demo_type", DemoType.CHARSET); 42 | break; 43 | case R.id.btn_byte: 44 | intent.putExtra("demo_type", DemoType.CHARSET); 45 | break; 46 | case R.id.btn_bitmap: 47 | intent.putExtra("demo_type", DemoType.IMAGE); 48 | break; 49 | case R.id.btn_drawable: 50 | intent.putExtra("demo_type", DemoType.IMAGE); 51 | break; 52 | case R.id.btn_serialize: 53 | intent.putExtra("demo_type", DemoType.OBJECT); 54 | break; 55 | } 56 | startActivity(intent); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XDroidCache 2 | 二级缓存框架,包括内存缓存于磁盘缓存,两者均使用Lru(近期最少使用算法),轻松为你的应用接入缓存机制,简单的场景比如你的网络请求,很方便的为你的网络请求模块添加缓存功能。 3 | 存储数据,先存入Memory cache,再存入Diskcache,取出数据时,先检查Memory中是否存在缓存,存在直接返回数据,不存在从Disk查找。当缓存过期的时候会进行删除,返回null 4 | 5 | #Provide 6 | 1.MemoryCache(内存缓存),使用Android自带LruCache 7 | 2.DiskCache(磁盘缓存),使用JakeWharton的DiskLruCache 8 | 3.SecondLevelCache(二级缓存),一级MemoryCache,二级DiskCache 9 | 4.支持缓存时间设置,最小时间单位至秒,最大至年 10 | 5.支持永久缓存 11 | 6.缓存多种数据类型,基本的包括 12 | String 13 | byte[] 14 | JSONObject 15 | JSONArray 16 | Bitmap 17 | Drawable 18 | Serialize(任何序列化的对象) 19 | #Here is the sample 20 | [Download demo.apk](https://github.com/robinxdroid/XDroidCache/blob/master/XDroidCacheExample.apk?raw=true) 21 | #Screenshot 22 | ![](https://github.com/robinxdroid/XDroidCache/blob/master/screenshoot4.png?raw=true) 23 | ![](https://github.com/robinxdroid/XDroidCache/blob/master/screenshoot1.png?raw=true) 24 | ![](https://github.com/robinxdroid/XDroidCache/blob/master/screenshoot2.png?raw=true) 25 | ![](https://github.com/robinxdroid/XDroidCache/blob/master/screenshoot3.png?raw=true) 26 | 27 | # Usage 28 | 以String数据存取为例 29 | ```java 30 | //key:key_stirng 31 | //value:"测试数据" 32 | //缓存时间:10 33 | //时间单位:秒 34 | SecondLevelCacheKit.getInstance(this).put("key_string", "测试数据", 10, TimeUnit.SECOND); 35 | ``` 36 | ```java 37 | //key:key_stirng 38 | //value:"测试数据" 39 | //未设置缓存时间,则为永久缓存 40 | SecondLevelCacheKit.getInstance(this).put("key_string", "测试数据"); 41 | ``` 42 | ```java 43 | //取缓存 44 | String result = SecondLevelCacheKit.getInstance(this).getAsString("key_string"); 45 | ``` 46 | 其他详细请见Demo 47 | #Thanks 48 | [DiskLruCache](https://github.com/JakeWharton/DiskLruCache)
49 | [ASimpleCache](https://github.com/yangfuhai/ASimpleCache) 50 | #About me 51 | Email:735506404@robinx.net
52 | Blog:[www.robinx.net](http://www.robinx.net) 53 | 54 | -------------------------------------------------------------------------------- /XDroidCacheExample/src/com/xdroid/cache/example/CacheDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.xdroid.cache.example; 2 | 3 | import com.xdroid.cache.example.fragment.CharsetFragment; 4 | import com.xdroid.cache.example.fragment.ImageFragment; 5 | import com.xdroid.cache.example.fragment.ObjectFragment; 6 | 7 | import android.os.Bundle; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.app.FragmentActivity; 10 | import android.support.v4.app.FragmentManager; 11 | import android.support.v4.app.FragmentTransaction; 12 | 13 | /** 14 | * Demo演示FragmentActivity 15 | * @author Robin 16 | * @since 2015-11-24 16:19:19 17 | * 18 | */ 19 | public class CacheDemoActivity extends FragmentActivity { 20 | 21 | public interface DemoType { 22 | public int CHARSET = 0x01, IMAGE = 0x02, OBJECT = 0x03; 23 | } 24 | 25 | @Override 26 | protected void onCreate(Bundle arg0) { 27 | super.onCreate(arg0); 28 | setContentView(R.layout.activity_cache_demo); 29 | 30 | init(); 31 | } 32 | 33 | private void init() { 34 | //Fragment[] fragments = new Fragment[]{new CharsetFragment(),new ImageFragment(),new ObjectFragment()}; 35 | 36 | switch (getIntent().getIntExtra("demo_type", 0)) { 37 | case DemoType.CHARSET: 38 | switchFragment(CharsetFragment.class); 39 | break; 40 | 41 | case DemoType.IMAGE: 42 | switchFragment(ImageFragment.class); 43 | break; 44 | case DemoType.OBJECT: 45 | switchFragment(ObjectFragment.class); 46 | break; 47 | } 48 | } 49 | 50 | /** 51 | * 切换Fragment 52 | * @param cls 53 | */ 54 | public void switchFragment(Class cls){ 55 | int containerId = R.id.fragment_container; 56 | if (cls == null) { 57 | return; 58 | } 59 | try { 60 | String fragmentTag = cls.toString(); 61 | FragmentManager fm = getSupportFragmentManager(); 62 | Fragment fragment = (Fragment) fm.findFragmentByTag(fragmentTag); 63 | if (fragment == null) { 64 | fragment = (Fragment) cls.newInstance(); 65 | } 66 | 67 | FragmentTransaction ft = fm.beginTransaction(); 68 | if (fragment.isAdded()) { 69 | ft.show(fragment); 70 | } else { 71 | ft.add(containerId, fragment, fragmentTag); 72 | } 73 | 74 | //ft.addToBackStack(fragmentTag); 75 | ft.commitAllowingStateLoss(); 76 | } catch (InstantiationException e) { 77 | e.printStackTrace(); 78 | } catch (IllegalAccessException e) { 79 | e.printStackTrace(); 80 | } 81 | } 82 | 83 | 84 | 85 | } 86 | -------------------------------------------------------------------------------- /XDroidCache/src/com/xdroid/cache/disk/Util.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xdroid.cache.disk; 18 | 19 | import java.io.Closeable; 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.io.Reader; 23 | import java.io.StringWriter; 24 | import java.nio.charset.Charset; 25 | 26 | /** Junk drawer of utility methods. */ 27 | final class Util { 28 | static final Charset US_ASCII = Charset.forName("US-ASCII"); 29 | static final Charset UTF_8 = Charset.forName("UTF-8"); 30 | 31 | private Util() { 32 | } 33 | 34 | static String readFully(Reader reader) throws IOException { 35 | try { 36 | StringWriter writer = new StringWriter(); 37 | char[] buffer = new char[1024]; 38 | int count; 39 | while ((count = reader.read(buffer)) != -1) { 40 | writer.write(buffer, 0, count); 41 | } 42 | return writer.toString(); 43 | } finally { 44 | reader.close(); 45 | } 46 | } 47 | 48 | /** 49 | * Deletes the contents of {@code dir}. Throws an IOException if any file 50 | * could not be deleted, or if {@code dir} is not a readable directory. 51 | */ 52 | static void deleteContents(File dir) throws IOException { 53 | File[] files = dir.listFiles(); 54 | if (files == null) { 55 | throw new IOException("not a readable directory: " + dir); 56 | } 57 | for (File file : files) { 58 | if (file.isDirectory()) { 59 | deleteContents(file); 60 | } 61 | if (!file.delete()) { 62 | throw new IOException("failed to delete file: " + file); 63 | } 64 | } 65 | } 66 | 67 | static void closeQuietly(/*Auto*/Closeable closeable) { 68 | if (closeable != null) { 69 | try { 70 | closeable.close(); 71 | } catch (RuntimeException rethrown) { 72 | throw rethrown; 73 | } catch (Exception ignored) { 74 | } 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /XDroidCacheExample/src/com/xdroid/cache/example/fragment/ObjectFragment.java: -------------------------------------------------------------------------------- 1 | package com.xdroid.cache.example.fragment; 2 | 3 | import com.xdroid.cache.SecondLevelCacheKit; 4 | import com.xdroid.cache.example.R; 5 | import com.xdroid.cache.example.bean.PersonBean; 6 | import com.xdroid.cache.interfaces.TimeUnit; 7 | 8 | import android.os.Bundle; 9 | import android.support.v4.app.Fragment; 10 | import android.text.TextUtils; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.View.OnClickListener; 14 | import android.view.ViewGroup; 15 | import android.widget.EditText; 16 | import android.widget.TextView; 17 | import android.widget.Toast; 18 | 19 | public class ObjectFragment extends Fragment { 20 | 21 | private EditText mNameEditText; 22 | private EditText mEmailEditText; 23 | private EditText mCacheTimeEditText; 24 | private TextView mTipsTextView; 25 | 26 | @Override 27 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 28 | View view = inflater.inflate(R.layout.fragment_object, container, false); 29 | init(view); 30 | return view; 31 | 32 | } 33 | 34 | private void init(View view) { 35 | initView(view); 36 | } 37 | 38 | private void initView(View view) { 39 | mNameEditText = (EditText) view.findViewById(R.id.et_name); 40 | mEmailEditText = (EditText) view.findViewById(R.id.et_email); 41 | mCacheTimeEditText = (EditText) view.findViewById(R.id.et_cache_time); 42 | mTipsTextView = (TextView) view.findViewById(R.id.tv_tips); 43 | 44 | initSerializeCache(view); 45 | } 46 | 47 | /** 48 | * Serialize 缓存 49 | * 50 | * @param view 51 | */ 52 | private void initSerializeCache(View view) { 53 | view.findViewById(R.id.btn_serialize_save).setOnClickListener(new OnClickListener() { 54 | 55 | @Override 56 | public void onClick(View v) { 57 | String name = mNameEditText.getText().toString().trim(); 58 | String email = mEmailEditText.getText().toString().trim(); 59 | 60 | PersonBean cacheValue = new PersonBean(name, email); 61 | 62 | int cacheTime = 0; 63 | // 如果缓存时间为空,那么不设置缓存时间(永久缓存) 64 | if (!TextUtils.isEmpty(mCacheTimeEditText.getText().toString().trim())) { 65 | cacheTime = Integer.parseInt(mCacheTimeEditText.getText().toString().trim()); 66 | // 存储key为”key_serialize“ 67 | SecondLevelCacheKit.getInstance(getActivity()).put("key_serialize", cacheValue, cacheTime, 68 | TimeUnit.SECOND); 69 | Toast.makeText(getActivity(), "缓存成功,缓存时间:" + cacheTime + "秒", Toast.LENGTH_SHORT).show(); 70 | } else { 71 | SecondLevelCacheKit.getInstance(getActivity()).put("key_serialize", cacheValue); 72 | Toast.makeText(getActivity(), "缓存成功,永久缓存", Toast.LENGTH_SHORT).show(); 73 | } 74 | 75 | } 76 | }); 77 | view.findViewById(R.id.btn_serialize_read).setOnClickListener(new OnClickListener() { 78 | 79 | @Override 80 | public void onClick(View v) { 81 | PersonBean result = SecondLevelCacheKit.getInstance(getActivity()).getAsSerializable("key_serialize"); 82 | if (result== null) { 83 | mTipsTextView.setText("未查找到缓存数据"); 84 | } else { 85 | mTipsTextView.setText("Key:key_serialize Value:" + result); 86 | } 87 | } 88 | }); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /XDroidCache/src/com/xdroid/cache/interfaces/ICache.java: -------------------------------------------------------------------------------- 1 | package com.xdroid.cache.interfaces; 2 | 3 | import java.io.Serializable; 4 | 5 | import org.json.JSONArray; 6 | import org.json.JSONObject; 7 | 8 | import android.graphics.Bitmap; 9 | import android.graphics.drawable.Drawable; 10 | 11 | /** 12 | * Cache methods 13 | * 14 | * @author Robin 15 | * @since 2015-10-13 13:43:24 16 | * 17 | */ 18 | public interface ICache { 19 | 20 | /* 21 | * ======================================================================== 22 | * String 23 | * ======================================================================== 24 | */ 25 | 26 | public void put(String key, String value); 27 | 28 | public void put(String key, String value, int cacheTime, int timeUnit); 29 | 30 | public String getAsString(String key); 31 | 32 | /* 33 | * ======================================================================== 34 | * JSONObject 35 | * ======================================================================== 36 | */ 37 | 38 | public void put(String key, JSONObject jsonObject); 39 | 40 | public void put(String key, JSONObject jsonObject, int cacheTime, int timeUnit); 41 | 42 | public JSONObject getAsJSONObject(String key); 43 | 44 | /* 45 | * ======================================================================== 46 | * JSONArray 47 | * ======================================================================== 48 | */ 49 | 50 | public void put(String key, JSONArray jsonArray); 51 | 52 | public void put(String key, JSONArray jsonArray, int cacheTime, int timeUnit); 53 | 54 | public JSONArray getAsJSONArray(String key); 55 | 56 | /* 57 | * ======================================================================== 58 | * Byte 59 | * ======================================================================== 60 | */ 61 | 62 | public void put(String key, byte[] value); 63 | 64 | public void put(String key, byte[] value, int cacheTime, int timeUnit); 65 | 66 | public byte[] getAsBytes(String key); 67 | 68 | /* 69 | * ======================================================================== 70 | * Serializable 71 | * ======================================================================== 72 | */ 73 | 74 | public void put(String key, Serializable value); 75 | 76 | public void put(String key, Serializable value, int cacheTime, int timeUnit); 77 | 78 | public T getAsSerializable(String key); 79 | 80 | /* 81 | * ======================================================================== 82 | * Bitmap 83 | * ======================================================================== 84 | */ 85 | 86 | public void put(String key, Bitmap bitmap); 87 | 88 | public void put(String key, Bitmap bitmap, int cacheTime, int timeUnit); 89 | 90 | public Bitmap getAsBitmap(String key); 91 | 92 | /* 93 | * ======================================================================== 94 | * Drawable 95 | * ======================================================================== 96 | */ 97 | 98 | public void put(String key, Drawable value); 99 | 100 | public void put(String key, Drawable value, int cacheTime, int timeUnit); 101 | 102 | public Drawable getAsDrawable(String key); 103 | 104 | /* 105 | * ======================================================================== 106 | * Other methods 107 | * ======================================================================== 108 | */ 109 | 110 | public boolean remove(String key); 111 | 112 | } 113 | -------------------------------------------------------------------------------- /XDroidCacheExample/res/layout/fragment_image.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 13 | 14 | 19 | 20 | 28 | 29 | 35 | 36 | 37 | 42 | 43 |