Skip to content

Commit

Permalink
purge and exists? methods for cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
kul committed Nov 13, 2014
1 parent 6872d8c commit 0c1bbb3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pool

Clojure wrapper for Apache Commons Pool.
For pooling and caching objects which are costly to create.

##Install

Expand Down
15 changes: 14 additions & 1 deletion src/pool/cache.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Other optional kwarg are
:destroy double arity function which take key and object."
[make-fn & {:keys [destroy] :or {destroy ignore}}]
(let [cache {:cache (atom {}) :make make-fn}]
(let [cache {:cache (atom {}) :make make-fn :destroy destroy}]
(shutdown-hook
(doseq [[key object] @(:cache cache)]
(destroy key object)))
Expand All @@ -32,3 +32,16 @@
(if-let [object (@cache* key)]
object
((swap! cache* assoc key (make-fn key)) key))))))

(defn purge
[cache key]
(let [cache* (:cache cache)
destory-fn (:destroy cache)]
; lock?
(when-let [object (@cache* key)]
(destory-fn key object)
(swap! cache* dissoc key))))

(defn exists?
[cache key]
((-> cache :cache deref) key))

0 comments on commit 0c1bbb3

Please sign in to comment.