Advertisement
albibacsi

cache

Jul 19th, 2018
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. By#!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. CACHE_LEN = 5
  5. CACHE = {}
  6. INDEX = []
  7.  
  8.  
  9. def expensive_function(val=0):
  10.     # Inspired by Amazon prime day, today even
  11.     # the expensive function is a bit cheaper
  12.     return val + 1
  13.  
  14.  
  15. def show_cache_and_lru_status():
  16.     print "cache:", CACHE
  17.     print "index:", INDEX, "\n"
  18.  
  19.  
  20. def get_item_index_pos(item=0):
  21.     for i in range(0, len(CACHE)):
  22.         if INDEX[i] == item:
  23.             return i
  24.  
  25.     return -1
  26.  
  27.  
  28. def add_item_to_cache(item=0):
  29.     # Drop last used item from the cache
  30.     if len(CACHE) >= CACHE_LEN:
  31.         last_used_item = INDEX[-1]
  32.         print "cache is full, dropping last item, key:", last_used_item
  33.         del CACHE[last_used_item]
  34.         INDEX.pop()
  35.  
  36.     INDEX.insert(0, item)
  37.     CACHE[item] = expensive_function(item)
  38.  
  39.     print "adding key:", item, "with value:", CACHE[item]
  40.     show_cache_and_lru_status()
  41.  
  42.  
  43. def get_item_value_from_cache(item=0):
  44.     if item not in CACHE:
  45.         raise Exception("No such item: " + str(item))
  46.     else:
  47.         item_pos_in_index = get_item_index_pos(item)
  48.         value = CACHE[item]
  49.  
  50.         # Fix the order of items in the index
  51.  
  52.         if item_pos_in_index > 0:
  53.             # Swap place with the 0th item in the index list, ie.
  54.             # if the 3rd cache entry was accessed, then the new order
  55.             # would be:
  56.             #
  57.             # 1        3
  58.             # 2        2
  59.             # 3   ->   1
  60.             # 4        4
  61.             # 5        5
  62.             #
  63.             # This won't keep the exact order of when the items in the
  64.             # cache were accessed but it requires 1 swap only.
  65.             #
  66.             # Worst case scenario: with a full cache if the last used item
  67.             # is accessed, and a new item arrives, then the 2nd most recently
  68.             # used item (which just swapped place with the last item in the
  69.             # index) is dropped from the cache.
  70.  
  71.             tmp = INDEX[0]
  72.             INDEX[0] = INDEX[item_pos_in_index]
  73.             INDEX[item_pos_in_index] = tmp
  74.  
  75.             # If the expensive function is really expensive, then it's worth to
  76.             # keep the proper order in the index. With this approach the index
  77.             # of the accessed item rises to the top (=0th position) like a
  78.             # bubble at the cost of O(N) performance penalty.
  79.             #
  80.             # for i in reversed(range(0, item_pos_in_index)):
  81.             #    tmp = INDEX[i+1]
  82.             #    INDEX[i+1] = INDEX[i]
  83.             #    INDEX[i] = tmp
  84.  
  85.         return value
  86.  
  87.  
  88. def main():
  89.     # Fill the cache
  90.     for i in range(0, 2*CACHE_LEN):
  91.         add_item_to_cache((i+1)*10)
  92.  
  93.     # Retrieve a few items to test how the order changes
  94.     for item in (70, 90, 80):
  95.         value = get_item_value_from_cache(item)
  96.         print "After retrieving", item, "(value:", value, ") from cache"
  97.         show_cache_and_lru_status()
  98.  
  99.  
  100. if __name__ == "__main__":
  101.     main()
  102.  
  103.  
  104. ************************************
  105. Output:
  106.  
  107. adding key: 10 with value: 11
  108. cache: {10: 11}
  109. index: [10]
  110.  
  111. adding key: 20 with value: 21
  112. cache: {10: 11, 20: 21}
  113. index: [20, 10]
  114.  
  115. adding key: 30 with value: 31
  116. cache: {10: 11, 20: 21, 30: 31}
  117. index: [30, 20, 10]
  118.  
  119. adding key: 40 with value: 41
  120. cache: {40: 41, 10: 11, 20: 21, 30: 31}
  121. index: [40, 30, 20, 10]
  122.  
  123. adding key: 50 with value: 51
  124. cache: {40: 41, 10: 11, 20: 21, 50: 51, 30: 31}
  125. index: [50, 40, 30, 20, 10]
  126.  
  127. cache is full, dropping last item, key: 10
  128. adding key: 60 with value: 61
  129. cache: {40: 41, 50: 51, 20: 21, 60: 61, 30: 31}
  130. index: [60, 50, 40, 30, 20]
  131.  
  132. cache is full, dropping last item, key: 20
  133. adding key: 70 with value: 71
  134. cache: {70: 71, 40: 41, 50: 51, 60: 61, 30: 31}
  135. index: [70, 60, 50, 40, 30]
  136.  
  137. cache is full, dropping last item, key: 30
  138. adding key: 80 with value: 81
  139. cache: {70: 71, 40: 41, 80: 81, 50: 51, 60: 61}
  140. index: [80, 70, 60, 50, 40]
  141.  
  142. cache is full, dropping last item, key: 40
  143. adding key: 90 with value: 91
  144. cache: {70: 71, 80: 81, 50: 51, 90: 91, 60: 61}
  145. index: [90, 80, 70, 60, 50]
  146.  
  147. cache is full, dropping last item, key: 50
  148. adding key: 100 with value: 101
  149. cache: {100: 101, 70: 71, 80: 81, 90: 91, 60: 61}
  150. index: [100, 90, 80, 70, 60]
  151.  
  152. After retrieving 70 (value: 71 ) from cache
  153. cache: {100: 101, 70: 71, 80: 81, 90: 91, 60: 61}
  154. index: [70, 90, 80, 100, 60]
  155.  
  156. After retrieving 90 (value: 91 ) from cache
  157. cache: {100: 101, 70: 71, 80: 81, 90: 91, 60: 61}
  158. index: [90, 70, 80, 100, 60]
  159.  
  160. After retrieving 80 (value: 81 ) from cache
  161. cache: {100: 101, 70: 71, 80: 81, 90: 91, 60: 61}
  162. index: [80, 70, 90, 100, 60]
  163.  
  164.  
  165. The index order using the 2nd approach:
  166.  
  167. After retrieving 70 (value: 71 ) from cache
  168. cache: {100: 101, 70: 71, 80: 81, 90: 91, 60: 61}
  169. index: [70, 100, 90, 80, 60]
  170.  
  171. After retrieving 90 (value: 91 ) from cache
  172. cache: {100: 101, 70: 71, 80: 81, 90: 91, 60: 61}
  173. index: [90, 70, 100, 80, 60]
  174.  
  175. After retrieving 80 (value: 81 ) from cache
  176. cache: {100: 101, 70: 71, 80: 81, 90: 91, 60: 61}
  177. index: [80, 90, 70, 100, 60]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement