Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#### 使用Gradle构建时添加一下依赖即可:
```
compile 'com.dvsnier:cacheLib:0.0.4'
compile 'com.dvsnier:cacheLib:0.0.5'
```

#### 使用前配置
Expand All @@ -24,7 +24,7 @@ compile 'com.dvsnier:cacheLib:0.0.4'
@Override
public void onCreate() {
super.onCreate();
initializedCache();
CacheManager.getInstance().initialize(this);
...
}

Expand All @@ -33,18 +33,33 @@ compile 'com.dvsnier:cacheLib:0.0.4'
super.onTerminate();
CacheManager.getInstance().close();
}

private void initializedCache() {
File cache = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cache = getExternalCacheDir();
} else {
cache = getCacheDir();
}
int cacheMaxSizeOfDisk = 1024 * 1024 * 1024;
CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheDirectory(cache).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
}

```
#### 扩展
##### 第一种
```
// 在application的onCreate中初始化
@Override
public void onCreate() {
super.onCreate();
// 默认磁盘配置512M 缓存空间
CacheManager.getInstance().initialize(this);
...
}
```
##### 第二种
```
// 在application的onCreate中初始化
@Override
public void onCreate() {
super.onCreate();
// 自定义磁盘1G 缓存空间
int cacheMaxSizeOfDisk = 1024 * 1024 * 1024; // 1G
CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
// CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
...
}

public static int getAppVersionCode(Context context) {
int versionCode = 1;
PackageManager pm = context.getPackageManager();
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dvsnier.demo">
package="com.dvsnier.demo">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name=".DvsnierApplication"
Expand All @@ -13,9 +16,9 @@

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/com/dvsnier/demo/Bean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.dvsnier.demo;

import java.io.Serializable;

/**
* Created by dovsnier on 2018/6/12.
*/
public class Bean implements Serializable {

private static final long serialVersionUID = 8219150308909916068L;
private String name;
private String version;

public Bean() {
}

public Bean(String name, String version) {
this.name = name;
this.version = version;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

@Override
public String toString() {
return "Bean{" +
"name='" + name + '\'' +
", version='" + version + '\'' +
'}';
}
}
19 changes: 3 additions & 16 deletions app/src/main/java/com/dvsnier/demo/DvsnierApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;

import com.dvsnier.cache.CacheManager;
import com.dvsnier.cache.ICacheConfig;

import java.io.File;

/**
* Created by lizw on 2017/6/23.
Expand All @@ -20,7 +16,9 @@ public class DvsnierApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initializedCache();
CacheManager.getInstance().initialize(this);
// int cacheMaxSizeOfDisk = 1024 * 1024 * 1024; // 1G
// CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
}

@Override
Expand All @@ -29,17 +27,6 @@ public void onTerminate() {
CacheManager.getInstance().close();
}

private void initializedCache() {
File cache = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cache = getExternalCacheDir();
} else {
cache = getCacheDir();
}
int cacheMaxSizeOfDisk = 1024 * 1024 * 1024;
CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheDirectory(cache).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
}

public static int getAppVersionCode(Context context) {
int versionCode = 1;
PackageManager pm = context.getPackageManager();
Expand Down
35 changes: 28 additions & 7 deletions app/src/main/java/com/dvsnier/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,53 @@

public class MainActivity extends Activity {

private final String key0 = "key_0";
private final String key1 = "key_1";
private final String key2 = "key_2";
private TextView test;
private TextView content;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (TextView) findViewById(R.id.test);
content = (TextView) findViewById(R.id.content);
obtainContent();
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String key = String.valueOf(System.currentTimeMillis());
CacheManager.getInstance().putString(key, view.toString()).put(this.toString(), "测试数据: " + System.currentTimeMillis()).commit();
Toast.makeText(MainActivity.this, "测试完成,准备关闭...", Toast.LENGTH_SHORT).show();
CacheManager.getInstance().put(key0, "测试数据: " + System.currentTimeMillis())
.putString(key1, "测试字符串: " + view.toString())
.putObject(key2, new Bean("cache", "0.0.5"))
.commit();
obtainContent();
Toast.makeText(MainActivity.this, "测试完成,60s 后准备关闭...", Toast.LENGTH_SHORT).show();
view.postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 1000);
}, 60 * 1000);
}
});
}

@Override
protected void onPause() {
super.onPause();
// CacheManager.getInstance().onFlush(); // the deprecated
protected void onResume() {
super.onResume();
obtainContent();
}

protected void obtainContent() {
content.postDelayed(new Runnable() {
@Override
public void run() {
Object o0 = CacheManager.getInstance().get(key0);
String o1 = CacheManager.getInstance().getString(key1);
Object o2 = CacheManager.getInstance().getObject(key2);
content.setText(String.format("1. value:\n\t\t\t\t%1$s\n\n2. 字符串:\n\t\t\t\t%2$s\n\n3. 对象:\n\t\t\t\t%3$s\n\n", o0, o1, o2));
}
}, 1000);
}
}
23 changes: 18 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:orientation="vertical"
tools:context="com.dvsnier.demo.MainActivity">

<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|top"
android:padding="25dp"
android:textColor="@color/colorPrimary"
tools:ignore="RtlHardcoded" />

<TextView
android:id="@+id/test"
android:layout_width="99dp"
android:layout_height="99dp"
android:layout_centerInParent="true"
android:background="@color/colorAccent"
android:gravity="center"
android:text="测试"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
android:textStyle="bold"
tools:ignore="HardcodedText" />

</RelativeLayout>
1 change: 1 addition & 0 deletions bintrayUpload.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gradlew clean build publishToMavenLocal bintrayUpload -PdryRun=false > bintrayUpload.log
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ buildscript {

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "com.github.dcendents:android-maven-gradle-plugin:${android_maven_gradle_plugin}"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:${gradle_bintray_plugin}"
classpath "com.novoda:bintray-release:${bintray_release_version}"
}
}

allprojects {
repositories {
jcenter()
mavenCentral()
google()
}

tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}

Expand Down
Loading