Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn retrieve
- "Retrieve value with key from cache."
- [cache key]
- ;; search in the cache for the key, and write the returned value in 'value'
- ;; dosync needed, because cache tree needs to be updated if the element is found in the tree
- ;; so we need to make sure no other updates happen between find with splaying and updating
- (let [value (dosync
- (let [newTree (st/find (:tree @cache) key)]
- (if (= newTree nil)
- nil
- ;;else
- (do
- (ref-set cache {:store (:store @cache) :maxSize (:maxSize @cache) :size (:size @cache) :tree newTree })
- (:value newTree)
- )
- )
- )
- )
- ]
- (if (= value nil)
- ;; if the value == nil, search in store for the key and write the returned value in storeValue
- (let [ storeValue (store/retrieve (:store @cache) key) ]
- (if (= storeValue nil)
- ;; if storeValue == nil, just return it
- storeValue
- ;; else check if maxSize == 0
- (if (= (:maxSize @cache) 0)
- ;;if maxSize == 0 just return the StoreValue
- storeValue
- ;; else update cache and return the storeValue
- (do
- (dosync
- (commute cache updatecache key storeValue)
- )
- ;; return storeValue
- storeValue
- )
- )
- )
- )
- ;; else return the value
- value
- )
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement