Skip to content

mtumilowicz/java12-fundamentals-cache-implementations-workshop

Repository files navigation

Build Status License: GPL v3

java12-fundamentals-cache-implementations-workshop

preface

  • goals of this workshop

    • understand concept of LRU cache
    • understand concept of LFU cache
    • implement LRU and LFU cache
    • see how guards are useful in list implementations
  • workshop: lfu.workshop, lru.workshop

    • during implementation use classes from list
  • answers: lfu.answers, lru.answers

least recently used (LRU)

  • discards the least recently used items first
  • operations
    • get(key) - get the value if the key exists in the cache, otherwise null
    • put(key, value) - set or insert the value if the key is not already present
      • when the cache reached its capacity, it should invalidate the least recently used item before inserting a new item
    • both in O(1)
  • solutions
    • LinkedHashMap<Integer, Integer> + overridden removeEldestEntry method
      • remark: LinkedHashMap has two iteration order: access-order or insertion-order
    • map + double linked list

least frequently used

  • is nothing but removing least frequently used item from the cache to put the new data into the cache
  • is sometimes combined with a Least Recently Used algorithm and called LRFU
  • operations
    • get(key) - get the value if the key exists in the cache, otherwise null
    • put(key, value) - set or insert the value if the key is not already present
      • when the cache reached its capacity, it should invalidate the least recently used item before inserting a new item
    • both in O(1)
  • may seem like an intuitive approach to memory management it is not without faults
    • consider an item in memory which is referenced repeatedly for a short period of time and is not accessed again for an extended period of time
    • an explicit LFU system is fairly uncommon; instead, there are hybrids that utilize LFU concepts
  • solution
    • cache map, frequency map, frequency list