Skip to content

@CacheKey annotation

crypticminds edited this page Feb 5, 2020 · 3 revisions

@CacheKey annotation is used along with @Freeze or @Refrigerate to define which method parameter or parameters will form the key while keeping the output of the method in the cache.

Usage

@Refrigerate
fun someMethodToDownloadText(@CacheKey val argument1 : String,
                             @CacheKey val argument2 : String,
                             val argument3 : String) : String {
// method logic
}
@Freeze
Class CacheClass {
  fun someMethodToDownloadText(@CacheKey val argument1 : String,
                               @CacheKey val argument2 : String,
                               val argument3 : String) : String {
  // method logic
  }
}

ColdStorage will cache the output of the function where the key of the cache will be

   argument1.hashCode().toString() + argument2.hashCode().toString()

It is important to note the any object declared as a cache key should have a hashCode implementation

In the above example, argument1 and argument2 will determine the output of the method whereas argument3 has no effect whatsoever on the method's output and hence it is not defined as a key.

If no cache key is defined for a method, then all the parameters will together form the key of the cache for that method.