Skip to content

Commit 858de73

Browse files
author
crypticminds
committed
[Cache key annotation] : Added annotation to declare a method parameters as cache keys
1 parent f6cdbb7 commit 858de73

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

app/src/main/java/com/arcane/coldstorage/cache/CacheWithRefrigerate.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.arcane.coldstorage.cache
22

3+
import com.arcane.coldstorageannotation.CacheKey
34
import com.arcane.coldstorageannotation.Refrigerate
45

56
class CacheWithRefrigerate {
@@ -10,11 +11,20 @@ class CacheWithRefrigerate {
1011
return "B"
1112
}
1213

14+
@Refrigerate(operation = "serviceB")
15+
fun makeCallToSomeService(@CacheKey key: String,
16+
@CacheKey abcd: String,
17+
efgh : String,
18+
ignorethis : String): String {
19+
return "C"
20+
21+
}
22+
1323
/**
1424
* This will throw an error since the function is private
1525
@Refrigerate(operation = "failure")
1626
private fun failureFunction(): String {
17-
return "C"
27+
return "C"
1828
}
1929
2030
@@ -23,5 +33,5 @@ class CacheWithRefrigerate {
2333
fun failureFunction2() {
2434
2535
}
26-
**/
36+
**/
2737
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.arcane.coldstorageannotation
2+
3+
/**
4+
* Annotation to mark a method parameter as the key of the cache.
5+
* This can be used to mark multiple parameters and all of them together
6+
* will form the key for the cache for that particular method.
7+
*
8+
* If no key is specified then all the parameters will together form
9+
* the key of the cache.
10+
*
11+
* @author Anurag
12+
*/
13+
@Retention(AnnotationRetention.SOURCE)
14+
@Target(AnnotationTarget.VALUE_PARAMETER)
15+
annotation class CacheKey

coldstorageannotation/src/main/java/com/arcane/coldstorageannotation/Refrigerate.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package com.arcane.coldstorageannotation
2222
@Target(AnnotationTarget.FUNCTION)
2323
annotation class Refrigerate(
2424
val timeToLive: Long = -1,
25+
@Deprecated("Use the annotation @CacheKey")
2526
val keys: Array<String> = [],
2627
val operation: String
2728
)

coldstoragecompiler/src/main/java/com/arcane/coldstorage_compiler/helper/CodeGenerationHelper.kt

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.arcane.coldstorage_compiler.helper
22

3+
import com.arcane.coldstorageannotation.CacheKey
34
import com.arcane.coldstorageannotation.Freeze
45
import com.arcane.coldstorageannotation.Refrigerate
56
import com.squareup.kotlinpoet.*
67
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
8+
import java.util.*
79
import java.util.stream.Collectors
810
import javax.lang.model.element.*
911
import javax.lang.model.type.ExecutableType
@@ -256,23 +258,42 @@ class CodeGenerationHelper {
256258
}
257259

258260
//TODO check if variable name is incorrect
259-
val keys: MutableList<String> =
260-
if (refrigerate.keys.isEmpty()) {
261-
parameters.stream().map { parameter ->
261+
val keys: MutableList<String> = refrigerate.keys.toMutableList()
262+
263+
264+
265+
var annotatedKeys = parameters
266+
.stream()
267+
.map { parameter ->
268+
if (parameter.getAnnotation(CacheKey::class.java) == null) {
269+
null
270+
} else {
262271
parameter.simpleName.toString()
263-
}.collect(Collectors.toList())
264-
} else {
265-
refrigerate.keys.toMutableList()
272+
}
266273
}
274+
.filter(Objects::nonNull)
275+
.collect(Collectors.toList())
276+
277+
278+
annotatedKeys.addAll(keys)
279+
280+
if(annotatedKeys.isEmpty()) {
281+
annotatedKeys = parameters.stream().map { parameter ->
282+
parameter.simpleName.toString()
283+
}.collect(Collectors.toList())
284+
}
285+
286+
val allKeys = annotatedKeys.stream().distinct().collect(Collectors.toList())
287+
267288

268289
var calculateKeyCodeBlock = "val $KEY_VARIABLE = "
269290

270-
if (keys.isNullOrEmpty()) {
291+
if (allKeys.isNullOrEmpty()) {
271292
calculateKeyCodeBlock += " \"${refrigerate.operation}\""
272293
} else {
273-
keys.forEach { key ->
294+
allKeys.forEach { key ->
274295
calculateKeyCodeBlock += "Integer.toString($key.hashCode())"
275-
if (keys.indexOf(key) != keys.size - 1) {
296+
if (allKeys.indexOf(key) != allKeys.size - 1) {
276297
calculateKeyCodeBlock += " + "
277298
}
278299
}

0 commit comments

Comments
 (0)