Advertisement
Guest User

Untitled

a guest
Aug 5th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. (defn retrieve
  2. "Retrieve value with key from cache."
  3. [cache key]
  4. ;; search in the cache for the key, and write the returned value in 'value'
  5. ;; dosync needed, because cache tree needs to be updated if the element is found in the tree
  6. ;; so we need to make sure no other updates happen between find with splaying and updating
  7. (let [value (dosync
  8. (let [newTree (st/find (:tree @cache) key)]
  9. (if (= newTree nil)
  10. nil
  11. ;;else
  12. (do
  13. (ref-set cache {:store (:store @cache) :maxSize (:maxSize @cache) :size (:size @cache) :tree newTree })
  14. (:value newTree)
  15. )
  16. )
  17. )
  18. )
  19. ]
  20.  
  21. (if (= value nil)
  22. ;; if the value == nil, search in store for the key and write the returned value in storeValue
  23. (let [ storeValue (store/retrieve (:store @cache) key) ]
  24.  
  25. (if (= storeValue nil)
  26. ;; if storeValue == nil, just return it
  27. storeValue
  28. ;; else check if maxSize == 0
  29. (if (= (:maxSize @cache) 0)
  30. ;;if maxSize == 0 just return the StoreValue
  31. storeValue
  32. ;; else update cache and return the storeValue
  33. (do
  34. (dosync
  35. (commute cache updatecache key storeValue)
  36. )
  37. ;; return storeValue
  38. storeValue
  39. )
  40. )
  41. )
  42. )
  43. ;; else return the value
  44. value
  45.  
  46. )
  47. )
  48. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement