Skip to content

Commit 2ce3cb1

Browse files
committed
fix:添加可以设置网络缓存的时间
1 parent 78f4b3f commit 2ce3cb1

8 files changed

Lines changed: 55 additions & 17 deletions

File tree

.idea/sonarlint/issuestore/0/9/09fc55d7b0e62b7c5cfc119d00304e17034b5a62

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/issuestore/5/9/59ca5d84a1197004f62a9cb9f53a7df61157ac7a

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/issuestore/e/f/ef64fd164d2083a14e4ffe5a00abb31194c0a8ef

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FlowHttp/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
//ext.GROUP = "com.lute.network"
88
//ext.POM_ARTIFACT_ID = "NetCore-Flow"
9-
//ext.VERSION_NAME = "1.6.4"
9+
//ext.VERSION_NAME = "1.6.5"
1010
////引用gradle_upload.gradle
1111
//apply from: "${project.rootDir}/maven_upload.gradle"
1212

@@ -19,7 +19,7 @@ afterEvaluate {
1919
// 这里头是artifacts的配置信息,不填会采用默认的
2020
groupId = 'com.github.buhuiming'
2121
artifactId = 'NetCore-Flow'
22-
version = '1.6.4'
22+
version = '1.6.5'
2323

2424
from components.release
2525
artifact androidSourcesJar //打包源码,去除这行打的包将看不到源码

FlowHttp/src/main/java/com/bhm/network/core/HttpConfig.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class HttpConfig(builder: Builder) {
3838
internal var successCode: Int = OK_CODE
3939
internal var parseDataKey: Boolean = false
4040
internal var httpLogEvent: HttpLogEvent? = null
41+
internal var cacheDuration: Long = 5L
42+
internal var cacheDurationNoNet: Long = 60 * 60 * 24 * 3L
4143

4244
/**
4345
* 设置请求loading页面
@@ -170,6 +172,16 @@ class HttpConfig(builder: Builder) {
170172
this.httpLogEvent = httpLogEvent
171173
}
172174

175+
/**
176+
* 设置缓存时间
177+
* @param cacheDuration 有网络时缓存时间,单位秒
178+
* @param cacheDurationNoNet 没有网络时缓存时间,单位秒
179+
*/
180+
fun setCacheDuration(cacheDuration: Long, cacheDurationNoNet: Long) = apply {
181+
this.cacheDuration = cacheDuration
182+
this.cacheDurationNoNet = cacheDurationNoNet
183+
}
184+
173185
fun build(): HttpConfig {
174186
return HttpConfig(this)
175187
}
@@ -250,6 +262,14 @@ class HttpConfig(builder: Builder) {
250262
var httpLogEvent: HttpLogEvent? = null
251263
private set
252264

265+
@JvmStatic
266+
var cacheDuration: Long = 5L //缓存时间,单位秒,有网络时缓存时间,默认5秒
267+
private set
268+
269+
@JvmStatic
270+
var cacheDurationNoNet: Long = 60 * 60 * 24 * 3L //缓存时间,单位秒,没有网络时缓存时间,默认3天
271+
private set
272+
253273
@JvmStatic
254274
fun create() = Builder()
255275

@@ -288,5 +308,7 @@ class HttpConfig(builder: Builder) {
288308
successCode = builder.successCode
289309
parseDataKey = builder.parseDataKey
290310
httpLogEvent = builder.httpLogEvent
311+
cacheDuration = builder.cacheDuration
312+
cacheDurationNoNet = builder.cacheDurationNoNet
291313
}
292314
}

FlowHttp/src/main/java/com/bhm/network/core/HttpOptions.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class HttpOptions(private val builder: Builder) {
7878
get() = builder.noProxy
7979
val httpLogEvent: HttpLogEvent?
8080
get() = builder.httpLogEvent
81+
val cacheDuration: Long
82+
get() = builder.cacheDuration
83+
val cacheDurationNoNet: Long
84+
get() = builder.cacheDurationNoNet
8185

8286
class Builder(val activity: FragmentActivity) {
8387
internal var isShowDialog = HttpConfig.isShowDialog
@@ -105,6 +109,8 @@ class HttpOptions(private val builder: Builder) {
105109
internal var jobKey = System.currentTimeMillis().toString()
106110
internal var noProxy = true
107111
internal var httpLogEvent = HttpConfig.httpLogEvent
112+
internal var cacheDuration: Long = HttpConfig.cacheDuration
113+
internal var cacheDurationNoNet: Long = HttpConfig.cacheDurationNoNet
108114

109115
init {
110116
activity.lifecycle.addObserver(object : DefaultLifecycleObserver {
@@ -261,6 +267,16 @@ class HttpOptions(private val builder: Builder) {
261267
this.httpLogEvent = httpLogEvent
262268
}
263269

270+
/**
271+
* 设置缓存时间
272+
* @param cacheDuration 有网络时缓存时间,单位秒
273+
* @param cacheDurationNoNet 没有网络时缓存时间,单位秒
274+
*/
275+
fun setCacheDuration(cacheDuration: Long, cacheDurationNoNet: Long) = apply {
276+
this.cacheDuration = cacheDuration
277+
this.cacheDurationNoNet = cacheDurationNoNet
278+
}
279+
264280
fun build(): HttpOptions {
265281
val options = HttpOptions(this)
266282
JobManager.get().setHttpOptions(options)

FlowHttp/src/main/java/com/bhm/network/core/interceptor/CacheInterceptor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CacheInterceptor {
2424
var request = chain.request()
2525
if (isNetworkConnected(builder.activity)) {
2626
// 有网络时, 缓存5s,根据实际情况设置
27-
val maxAge = 5
27+
val maxAge = builder.cacheDuration
2828
request = request.newBuilder()
2929
.removeHeader(USER_AGENT)
3030
.removeHeader(ACCEPT_ENCODING)
@@ -40,7 +40,7 @@ class CacheInterceptor {
4040
.build()
4141
} else {
4242
// 无网络时,缓存为3天
43-
val maxStale = 60 * 60 * 24 * 3
43+
val maxStale = builder.cacheDurationNoNet
4444
request = request.newBuilder()
4545
.cacheControl(CacheControl.FORCE_CACHE)
4646
.removeHeader(USER_AGENT)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111

1212
dependencies {
13-
implementation 'com.github.buhuiming:NetCore-Flow:1.6.4'
13+
implementation 'com.github.buhuiming:NetCore-Flow:1.6.5'
1414
}
1515

1616
#### 1、Application配置默认的全局配置项(可选)

0 commit comments

Comments
 (0)