Skip to content

Commit a8499d0

Browse files
authored
Merge pull request #1 from TBCBank/change/add_more_response_values_to_cache
Add more response values to cache model
2 parents b6aa9ee + 371b982 commit a8499d0

5 files changed

Lines changed: 91 additions & 88 deletions

File tree

app/src/main/java/ge/tbcbank/retrocache/TestApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import retrofit2.http.Path
66

77
interface TestApi {
88

9-
@Cache(tag = "mtag", cacheTimeMillis = 60_000)
9+
@Cache(tag = "mtag")
1010
@GET("search" + "/{query}/{page}")
1111
suspend fun getBookWithPage(
1212
@Path("page") page: String,

retrocache/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = "ge.tbcbank.retrocache"
11-
version "1.0"
11+
version "1.0.1"
1212

1313
dependencies {
1414
val okhttp = "4.11.0"
@@ -34,7 +34,7 @@ afterEvaluate {
3434
from(components["java"])
3535
groupId = "com.github.TBCBank"
3636
artifactId = "RetroCache"
37-
version = "1.0"
37+
version = "1.0.1"
3838
}
3939
}
4040
}

retrocache/src/main/java/ge/tbcbank/retrocache/RetroCacheInterceptor.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ge.tbcbank.retrocache
22

3+
import okhttp3.Headers
34
import okhttp3.Interceptor
4-
import okhttp3.Protocol
55
import okhttp3.RequestBody
66
import okhttp3.Response
77
import okhttp3.ResponseBody.Companion.toResponseBody
@@ -29,11 +29,12 @@ class RetroCacheInterceptor(
2929
val cachedData = retroCacheManager[key].takeIf { fromCache }
3030
if (cachedData != null) {
3131
Response.Builder()
32-
.code(200)
33-
.request(request)
3432
.message("From RetroCache")
33+
.request(request)
34+
.code(cachedData.responseCode)
3535
.body(cachedData.responseJson?.toResponseBody())
36-
.protocol(Protocol.HTTP_1_1)
36+
.protocol(cachedData.responseProtocol)
37+
.headers(Headers.headersOf(*cachedData.responseHeaders))
3738
.build()
3839
} else {
3940
val response = chain.proceed(request)
@@ -42,6 +43,9 @@ class RetroCacheInterceptor(
4243
responseJson = response
4344
.peekBody(retroCacheManager.maxObjectSizeBytes.toLong())
4445
.string(),
46+
responseCode = response.code,
47+
responseProtocol = response.protocol,
48+
responseHeaders = response.headers.toArray(),
4549
tag = cacheAnnotation.tag.ifEmpty { null },
4650
expirationTime = getCacheTime(cacheAnnotation, cachePolicy),
4751
)
@@ -56,6 +60,15 @@ class RetroCacheInterceptor(
5660
}
5761
}
5862

63+
private fun Headers.toArray(): Array<String> {
64+
val headerList = mutableListOf<String>()
65+
forEach {
66+
headerList.add(it.first)
67+
headerList.add(it.second)
68+
}
69+
return headerList.toTypedArray()
70+
}
71+
5972
private fun getCacheTime(cacheAnnotation: Cache, cachePolicy: CachePolicy?): Long? {
6073
if (cachePolicy is CachePolicy.CachedWithTime) {
6174
return System.currentTimeMillis() + cachePolicy.timeInMillis
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
package ge.tbcbank.retrocache
22

3+
import okhttp3.Protocol
34
import java.io.Serializable
45

56
data class RetroCacheValue(
67
val responseJson: String?,
8+
val responseCode: Int,
9+
val responseProtocol: Protocol,
10+
val responseHeaders: Array<String>,
711
val tag: String? = null,
812
val expirationTime: Long? = null,
9-
) : Serializable
13+
) : Serializable {
14+
15+
override fun equals(other: Any?): Boolean {
16+
if (this === other) return true
17+
if (javaClass != other?.javaClass) return false
18+
19+
other as RetroCacheValue
20+
21+
if (responseJson != other.responseJson) return false
22+
if (responseCode != other.responseCode) return false
23+
if (responseProtocol != other.responseProtocol) return false
24+
if (!responseHeaders.contentEquals(other.responseHeaders)) return false
25+
if (tag != other.tag) return false
26+
return expirationTime == other.expirationTime
27+
}
28+
29+
override fun hashCode(): Int {
30+
var result = responseJson?.hashCode() ?: 0
31+
result = 31 * result + responseCode
32+
result = 31 * result + responseProtocol.hashCode()
33+
result = 31 * result + responseHeaders.contentHashCode()
34+
result = 31 * result + (tag?.hashCode() ?: 0)
35+
result = 31 * result + (expirationTime?.hashCode() ?: 0)
36+
return result
37+
}
38+
39+
40+
}
Lines changed: 39 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ge.tbcbank.retrocache
22

3+
import okhttp3.Protocol
34
import org.junit.Assert.assertEquals
45
import org.junit.Assert.assertNotNull
56
import org.junit.Assert.assertNull
@@ -12,7 +13,7 @@ class RetroCacheManagerTest {
1213
@Test
1314
fun `get returns cached object when present`() {
1415
val cacheManager = RetroCacheManager.Builder().build()
15-
val model = RetroCacheValue("test", "123")
16+
val model = getRetroCacheModel()
1617
cacheManager["key"] = model
1718

1819
val cachedModel = cacheManager["key"]
@@ -23,12 +24,14 @@ class RetroCacheManagerTest {
2324
@Test
2425
fun `get returns null when object expired`() {
2526
val cacheManager = RetroCacheManager.Builder().build()
26-
val model =
27-
RetroCacheValue(
28-
"test",
29-
"123",
30-
expirationTime = System.currentTimeMillis() - 1000
31-
)
27+
val model = RetroCacheValue(
28+
responseJson = "test",
29+
responseCode = 200,
30+
responseProtocol = Protocol.HTTP_1_1,
31+
responseHeaders = arrayOf(),
32+
tag = "123",
33+
expirationTime = System.currentTimeMillis() - 1000
34+
)
3235
cacheManager["key"] = model
3336

3437
val cachedModel = cacheManager["key"]
@@ -39,8 +42,8 @@ class RetroCacheManagerTest {
3942
@Test
4043
fun `set adds object to cache`() {
4144
val cacheManager = RetroCacheManager.Builder().build()
42-
val model = RetroCacheValue("test", "123")
4345

46+
val model = getRetroCacheModel()
4447
cacheManager["key"] = model
4548

4649
val cachedModel = cacheManager["key"]
@@ -50,7 +53,7 @@ class RetroCacheManagerTest {
5053
@Test
5154
fun `remove removes object from cache`() {
5255
val cacheManager = RetroCacheManager.Builder().build()
53-
val model = RetroCacheValue("test", "123")
56+
val model = getRetroCacheModel()
5457
cacheManager["key"] = model
5558

5659
cacheManager.remove("key")
@@ -59,11 +62,19 @@ class RetroCacheManagerTest {
5962
assertNull(cachedModel)
6063
}
6164

65+
private fun getRetroCacheModel(responseJson: String = "test", tag: String = "123") = RetroCacheValue(
66+
responseJson = responseJson,
67+
responseCode = 200,
68+
responseProtocol = Protocol.HTTP_1_1,
69+
responseHeaders = arrayOf(),
70+
tag = tag
71+
)
72+
6273
@Test
6374
fun `clearAll removes all objects from cache`() {
6475
val cacheManager = RetroCacheManager.Builder().build()
65-
cacheManager["key1"] = RetroCacheValue("test1", "123")
66-
cacheManager["key2"] = RetroCacheValue("test2", "456")
76+
cacheManager["key1"] = getRetroCacheModel("test1", "123")
77+
cacheManager["key2"] = getRetroCacheModel("test2", "456")
6778

6879
cacheManager.clearAll()
6980

@@ -74,9 +85,9 @@ class RetroCacheManagerTest {
7485
@Test
7586
fun `clearAllByTag removes objects with specified tag`() {
7687
val cacheManager = RetroCacheManager.Builder().build()
77-
cacheManager["key1"] = RetroCacheValue("test1", tag = "tag1")
78-
cacheManager["key2"] = RetroCacheValue("test2", tag = "tag1")
79-
cacheManager["key3"] = RetroCacheValue("test3", tag = "tag2")
88+
cacheManager["key1"] = getRetroCacheModel("test1", tag = "tag1")
89+
cacheManager["key2"] = getRetroCacheModel("test2", tag = "tag1")
90+
cacheManager["key3"] = getRetroCacheModel("test3", tag = "tag2")
8091

8192
cacheManager.clearAllByTag("tag1")
8293

@@ -108,9 +119,9 @@ class RetroCacheManagerTest {
108119
@Test
109120
fun `last accessed object is moved to top of cache`() {
110121
val cacheManager = RetroCacheManager.Builder().build()
111-
cacheManager["key1"] = RetroCacheValue("test1")
112-
cacheManager["key2"] = RetroCacheValue("test2")
113-
cacheManager["key3"] = RetroCacheValue("test3")
122+
cacheManager["key1"] = getRetroCacheModel("test1")
123+
cacheManager["key2"] = getRetroCacheModel("test2")
124+
cacheManager["key3"] = getRetroCacheModel("test3")
114125

115126
var first: RetroCacheValue? = null
116127
var last: RetroCacheValue? = null
@@ -139,12 +150,12 @@ class RetroCacheManagerTest {
139150
@Test
140151
fun `old cached responses are removed if maxMemory limit has reached`() {
141152
val cacheManager = RetroCacheManager.Builder()
142-
.maxMemorySize(100 * 1024) // 100 kb max memory size
143-
.maxObjectSize(1 * 1024) // 1 kb max memory size
153+
.maxMemorySize(100 * sizeOfObject(getRetroCacheModel(get1KbString()))) // fits 100 object
154+
.maxObjectSize(1 * sizeOfObject(getRetroCacheModel(get1KbString())))
144155
.build()
145156

146157
(0..110).forEach {// add 10 items more than limit
147-
cacheManager[it.toString()] = RetroCacheValue(oneKbString)
158+
cacheManager[it.toString()] = getRetroCacheModel(get1KbString())
148159
}
149160

150161
(0..110).forEach {
@@ -163,9 +174,8 @@ class RetroCacheManagerTest {
163174
.maxObjectSize(1 * 1024) // 1 kb max memory size
164175
.build()
165176

166-
val invalidSizedString = oneKbString + "12345"
167-
cacheManager["key"] = RetroCacheValue(invalidSizedString)
168-
println(sizeOfObject(RetroCacheValue(invalidSizedString)))
177+
val invalidSizedString = get1KbString() + "12345"
178+
cacheManager["key"] = getRetroCacheModel(invalidSizedString)
169179
assertNull(cacheManager["key"])
170180
}
171181

@@ -180,62 +190,11 @@ class RetroCacheManagerTest {
180190
}
181191

182192

183-
companion object {
184-
private val oneKbString = """
185-
{
186-
"id": 123,
187-
"name": "John Doe",
188-
"email": "johndoe@example.com",
189-
"age": 30,
190-
"address": {
191-
"street": "123 Main St",
192-
"city": "Anytown",
193-
"country": "USA"
194-
}
195-
}
196-
{
197-
"id": 123,
198-
"name": "John Doe",
199-
"email": "johndoe@example.com",
200-
"age": 30,
201-
"address": {
202-
"street": "123 Main St",
203-
"city": "Anytown",
204-
"country": "USA"
205-
}
206-
}
207-
{
208-
"id": 123,
209-
"name": "John Doe",
210-
"email": "johndoe@example.com",
211-
"age": 30,
212-
"address": {
213-
"street": "123 Main St",
214-
"city": "Anytown",
215-
"country": "USA"
216-
}
217-
}
218-
{
219-
"id": 123,
220-
"name": "John Doe",
221-
"email": "johndoe@example.com",
222-
"age": 30,
223-
"address": {
224-
"street": "123 Main St",
225-
"city": "Anytown",
226-
"country": "USA"
227-
}
228-
}
229-
{
230-
"id": 123,
231-
"name": "John Doe",
232-
"email": "johndoe@example.com",
233-
"age": 30,
234-
"address": {
235-
"street": "123 Main St",
236-
"city": "Anytown",
237-
}1111111
238-
}
239-
""".trimIndent()
193+
private fun get1KbString(): String {
194+
val stringBuilder = StringBuilder()
195+
repeat(1024) {
196+
stringBuilder.append('a')
197+
}
198+
return stringBuilder.toString()
240199
}
241200
}

0 commit comments

Comments
 (0)