@@ -29,7 +29,7 @@ Enable the Gradle plugin for the module where you want to use this library.
2929// build.gradle.kts
3030plugins {
3131 kotlin(/* ... */ )
32- id(" com.moriatsushi.cacheable" ) version " 0.0.2 "
32+ id(" com.moriatsushi.cacheable" ) version " 0.0.3 "
3333}
3434```
3535
@@ -38,11 +38,11 @@ You also need to add the `cacheable-core` dependency to the module.
3838``` kotlin
3939// build.gradle.kts
4040dependencies {
41- implementation(" com.moriatsushi.cacheable:cacheable-core:0.0.2 " )
41+ implementation(" com.moriatsushi.cacheable:cacheable-core:0.0.3 " )
4242}
4343```
4444
45- ## Usage
45+ ## Basic Usage
4646
4747You can use ` @Cacheable ` annotation to cache the result of specified functions.
4848When you call a function with this annotation, it will return the cached value if the function is
@@ -69,26 +69,32 @@ fun getSomething(key: String): Something =
6969If you use the cacheable function within a class, the cache is only shared within an instance of the
7070class.
7171
72- In addition, if you specify ` maxCount ` parameter, the number of cached values will be limited to the
73- specified value.
74- If the number of cached values exceeds the specified value, the cache with the oldest access time
75- will be removed.
72+ You can also use ` @Cacheable ` annotation for a suspend function.
7673
7774``` kotlin
78- class SomeClass {
79- @Cacheable(maxCount = 10 )
80- fun getSomething (key : String ): Something {
81- // ...
82- }
75+ class Repository (private val api : Api ) {
76+ @Cacheable
77+ suspend fun getUser (id : String ): User =
78+ api.getUser(id)
8379}
8480```
8581
86- You can also use ` @Cacheable ` annotation for a suspend function.
82+ ## APIs
83+ There are 2 parameters you can specify to ` @Cacheable ` annotation.
84+
85+ * ` maxCount ` : The maximum number of cached values. If the number of cached values exceeds this
86+ value, the cache with the oldest access time will be removed. If you don't specify this parameter,
87+ the number of cached values will be unlimited.
88+ * ` lock ` : When this is ` true ` , the function is guaranteed to be called only once even if the
89+ function is called multiple times with the same arguments at the same time. Otherwise, the
90+ function may be called multiple times with the same arguments at the same time. The default value
91+ is false.
8792
8893``` kotlin
89- class Repository (private val api : Api ) {
90- @Cacheable(maxCount = 10 )
91- suspend fun getUser (id : String ): User =
92- api.getUser(id)
94+ class SomeClass {
95+ @Cacheable(maxCount = 10 , lock = true )
96+ fun getSomething (key : String ): Something {
97+ // ...
98+ }
9399}
94100```
0 commit comments