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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ captures/
.idea/libraries
.DS_Store
release/
mock/

# Keystore files
*.jks
Expand Down
115 changes: 108 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
## cacheKit
一个简单易用的cache 库,此版本的cache 支持多实例模式.
一个简单易用的cache 库,此版本的cache 支持`多实例模式`和`本地缓存过期模式`.

### 特征
- 简单
- 实用
- 方便
- 多实例
- 缓存过期

#### 使用Gradle构建时添加一下依赖即可:
```
implementation 'com.dvsnier:cache:0.1.0'
implementation 'com.dvsnier:cache:0.2.0'
```

#### 使用前配置
Expand All @@ -32,12 +33,12 @@ implementation 'com.dvsnier:cache:0.1.0'
@Override
public void onTerminate() {
super.onTerminate();
CacheManager.getInstance().close();
CacheManager.getInstance().onDestroy();
}

```
#### 扩展
##### 第一种
##### 第一种,默认配置
```
// 在application的onCreate中初始化
@Override
Expand All @@ -48,7 +49,7 @@ implementation 'com.dvsnier:cache:0.1.0'
...
}
```
##### 第二种
##### 第二种,自定义配置
```
// 在application的onCreate中初始化
@Override
Expand All @@ -74,7 +75,7 @@ implementation 'com.dvsnier:cache:0.1.0'
}
```

#### 第三种
#### 第三种,多实例配置
```
// 在application的onCreate中初始化
@Override
Expand Down Expand Up @@ -106,6 +107,47 @@ implementation 'com.dvsnier:cache:0.1.0'
return versionCode;
}
```
#### 第四种,本地缓存过期模式配置
```
// 在application的onCreate中初始化
@Override
public void onCreate() {
super.onCreate();
// 自定义磁盘1G 缓存空间
int cacheMaxSizeOfDisk = Double.valueOf(CacheStorage.INSTANCE().getFormatted(1, AbstractStorage.SCU.G)).intValue(); // 1G < Integer.MAX_VALUE ~ 2G
// the configure cache alias, here in the downloads directory
CacheManager.getInstance().initialize(IType.TYPE_DOWNLOADS, new ICacheConfig.Builder(this)
.setContext(this)
.setAppVersion(getAppVersionCode(this))
.setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk)
.setUniqueName(IType.TYPE_DOWNLOADS)
.setCacheGenre(new CacheGenre.SCHEDULED()) // the Scheduled Mode, otherwise is default
// .setDebug(true)
// .setLevel(Level.VERBOSE)
.create());

CacheManager.getInstance().setOnTransactionSessionChangeListener(IType.TYPE_DOWNLOADS, new OnTransactionSessionChangeListener() {
@Override
public void onTransactionSessionChange(@NonNull String alias, @NonNull String key, @Nullable Object value) {
Log.i(Debug.TAG(), String.format("the current cache engine(%s), key(%s) - value(%s)", alias, key, value));
}
});
...
}

public static int getAppVersionCode(Context context) {
int versionCode = 1;
PackageManager pm = context.getPackageManager();
try {
PackageInfo packInfo = pm.getPackageInfo(context.getPackageName(), 0);
versionCode = packInfo.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
```


#### 使用
#### 1. 默认方式
Expand All @@ -122,12 +164,71 @@ implementation 'com.dvsnier:cache:0.1.0'
.commit(IType.TYPE_DOWNLOADS);
```

#### 3. 本地缓存过期方式
```
CacheManager.getInstance().put(IType.TYPE_DOWNLOADS, getKey(), getValue())
.put(IType.TYPE_DOWNLOADS, key1, getValue(), 30, TimeUnit.SECONDS)
.put(IType.TYPE_DOWNLOADS, key2, getValue(), 1, TimeUnit.MINUTES)
.put(IType.TYPE_DOWNLOADS, String.valueOf(System.currentTimeMillis()), getValue(), 3, TimeUnit.SECONDS)
.commit(IType.TYPE_DOWNLOADS);
```

#### 4. 获取数据
```
Object o0 = CacheManager.getInstance().get(key0);
String o1 = CacheManager.getInstance().getString(key1);
Object o2 = CacheManager.getInstance().getObject(key2);

Object O3 = CacheManager.getInstance().get(IType.TYPE_DOWNLOADS, key1);
Object O4 = CacheManager.getInstance().get(IType.TYPE_DOWNLOADS, key2);
```

#### 5. 方法签名

说明
- ```Y```:支持; ```*```: 原则上支持;

| | 默认 | 多实例 | 缓存过期 | 说明 | 备注 | |
| ---------------------------- | ---- | ------ | -------- | ---- | ---- | --- |
| | | | | | | |
| put(K,V) | Y | * | | | | |
| putString(K,V) | Y | * | | | | |
| putInputStream(K,V) | Y | * | | | | |
| putObject(K,V) | Y | * | | | | |
| | | | | | | |
| get(K) | Y | * | | | | |
| getString(K) | Y | * | | | | |
| getInputStream(K) | Y | * | | | | |
| getObject(K) | Y | * | | | | |
| | | | | | | |
| put(Type,K,V) | | Y | | | | |
| putString(Type,K,V) | | Y | | | | |
| putInputStream(Type,K,V) | | Y | | | | |
| putObject(Type,K,V) | | Y | | | | |
| | | | | | | |
| get(Type,K) | | Y | | | | |
| getString(Type,K) | | Y | | | | |
| getInputStream(Type,K) | | Y | | | | |
| getObject(Type,K) | | Y | | | | |
| | | | | | | |
| put(Type,K,V,D,U) | | Y | Y | | | |
| putString(Type,K,V,D,U) | | Y | Y | | | |
| putInputStream(Type,K,V,D,U) | | Y | Y | | | |
| putObject(Type,K,V,D,U) | | Y | Y | | | |
| | | | | | | |
| get(Type,K) | | Y | Y | | | |
| getString(Type,K) | | Y | Y | | | |
| getInputStream(Type,K) | | Y | Y | | | |
| getObject(Type,K) | | Y | Y | | | |


#### FAQ
1. 默认缓存文件保存目录为: `/mnt/sdcard/Android/data/package_your_name/cache/`
2. 配置缓存(IType.TYPE_DOWNLOADS)文件保存目录为: `/mnt/sdcard/Android/data/package_your_name/cache/downloads`,
如果配置不符合规范,则提供的默认配置为: `/mnt/sdcard/Android/data/package_your_name/cache/local`

> ```0.0.6``` 版本的Cache SDK 和```0.1.0``` 版本的Cache SDK 不兼容,如需简单使用缓存请使用```0.0.6```;
> - ```0.0.6``` 版本的Cache SDK 和```0.1.0``` 版本的Cache SDK 不兼容,如需简单使用缓存请使用```0.0.6```;
> - 推荐```0.2.0```,本地缓存过期和持久化自由切换(冷切换),兼容```0.1.0``` 版本,不兼容```0.0.6``` 版本;
----
### 关于作者
* Email: <dovsnier@qq.com>
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
}

implementation project(':cacheLib')
// implementation 'com.dvsnier:cache:0.1.0'

testImplementation "junit:junit:${rootProject.ext.junit_version}"
androidTestImplementation "com.android.support.test:runner:${rootProject.ext.runner_version}"
Expand Down

This file was deleted.

10 changes: 7 additions & 3 deletions app/src/main/java/com/dvsnier/demo/DvsnierApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import android.content.pm.PackageManager;

import com.dvsnier.cache.CacheManager;
import com.dvsnier.cache.base.CacheGenre;
import com.dvsnier.cache.config.ICacheConfig;
import com.dvsnier.cache.config.IType;
import com.dvsnier.cache.infrastructure.LogStorage;
import com.dvsnier.crash.Crash;

/**
Expand All @@ -19,6 +21,7 @@ public class DvsnierApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LogStorage.INSTANCE().log(this);
Crash.initialize(this);

// 方式一
Expand All @@ -32,28 +35,29 @@ public void onCreate() {
.setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk)
// .setUniqueName(IType.TYPE_DEFAULT)
.setUniqueName(IType.TYPE_DOWNLOADS)
.setCacheGenre(new CacheGenre.SCHEDULED())
.setDebug(true)
// .setLevel(Level.VERBOSE)
.create());

// CacheManager.getInstance().setOnTransactionSessionChangeListener(null, new OnTransactionSessionChangeListener() {
// @Override
// public void onTransactionSessionChange(@NonNull String alias, @NonNull String key, @Nullable Object value) {
// Log.i(Debug.TAG(), String.format("the current cache engine(%s), key(%s) - value(%s)", alias, key, value));
// Log.i(Debug.TAG(), String.format("==> the current cache engine(%s), key(%s) - value(%s)", alias, key, value));
// }
// });
// CacheManager.getInstance().setOnTransactionSessionChangeListener(IType.TYPE_DOWNLOADS, new OnTransactionSessionChangeListener() {
// @Override
// public void onTransactionSessionChange(@NonNull String alias, @NonNull String key, @Nullable Object value) {
// Log.i(Debug.TAG(), String.format("the current cache engine(%s), key(%s) - value(%s)", alias, key, value));
// Log.i(Debug.TAG(), String.format("==> the current cache engine(%s), key(%s) - value(%s)", alias, key, value));
// }
// });
}

@Override
public void onTerminate() {
super.onTerminate();
CacheManager.getInstance().close();
CacheManager.getInstance().onDestroy();
}

public static int getAppVersionCode(Context context) {
Expand Down
Loading