Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save NikitaKozlov/d3f66e524d4fb922db3d9747d644a4fc to your computer and use it in GitHub Desktop.

Select an option

Save NikitaKozlov/d3f66e524d4fb922db3d9747d644a4fc to your computer and use it in GitHub Desktop.

Revisions

  1. NikitaKozlov revised this gist Oct 2, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PaginationForFrequentlyChangingCollections.kt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    class ItemRepository(val backendApi: BackendApi, val cache: ItemCache) {

    fun getItemIds(pageNumber: Int) = backendApi.getItemIds(pageNumber)
    fun getOrderedItemIds(pageNumber: Int) = backendApi.getItemIds(pageNumber)

    fun getItemsByIds(itemIds: List<ItemId>): List<Item> {
    val cachedItems = getCachedItems(itemIds)
  2. NikitaKozlov revised this gist Sep 7, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions PaginationForFrequentlyChangingCollections.kt
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ class ItemRepository(val backendApi: BackendApi, val cache: ItemCache) {
    }

    private fun getCachingTimestampFromHeaders(headers: Headers): Timestamp? {
    return null
    //Extraction of the timestamp is out of scope for this gist
    return headers.getCachingTimestamp()
    }

    }
  3. NikitaKozlov revised this gist Sep 7, 2017. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion PaginationForFrequentlyChangingCollections.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    class ItemRepository(val backendApi: BackendApi, val cache: ItemCache) {

    fun getItemIds(pageNumber: Int) = backendApi.getItemIds(pageNumber)

    fun getItemsByIds(itemIds: List<ItemId>): List<Item> {
    @@ -33,4 +35,10 @@
    cachedItems[it]!!
    } else newlyRequestedItems[it]!!
    }
    }
    }

    private fun getCachingTimestampFromHeaders(headers: Headers): Timestamp? {
    return null
    }

    }
  4. NikitaKozlov created this gist Sep 7, 2017.
    36 changes: 36 additions & 0 deletions PaginationForFrequentlyChangingCollections.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    fun getItemIds(pageNumber: Int) = backendApi.getItemIds(pageNumber)

    fun getItemsByIds(itemIds: List<ItemId>): List<Item> {
    val cachedItems = getCachedItems(itemIds)
    val notCachedItemIds = getItemIdsNotRepresentedInCache(itemIds)
    val newlyRequestedItems = requestItemsById(notCachedItemIds)
    return merge(itemIds, cachedItems, newlyRequestedItems)
    }

    private fun getCachedItems(itemIds: List<ItemId>) =
    itemIds.filter { cache.contains(it) }
    .associate { Pair(it, cache.get(it)) }

    private fun getItemIdsNotRepresentedInCache(itemIds: List<ItemId>) =
    itemIds.filterNot { cache.contains(it) }

    private fun requestItemsById(itemIds: List<ItemId>): Map<ItemId, Item> {
    val response: Response<List<Item>> = backendApi.getItems(itemIds)
    val items = response.body()
    val responseValidUntil = getCachingTimestampFromHeaders(response.headers())
    putItemsInCache(items, responseValidUntil)
    return items.associateBy { it.id }
    }

    private fun putItemsInCache(items: List<Item>, validUntil: Timestamp?) {
    items.forEach { cache.put(it, validUntil) }
    }

    private fun merge(itemIds: List<ItemId>, cachedItems: Map<ItemId, Item>,
    newlyRequestedItems: Map<ItemId, Item>): List<Item> {
    return itemIds.map { it ->
    if (cachedItems.containsKey(it)) {
    cachedItems[it]!!
    } else newlyRequestedItems[it]!!
    }
    }