Adding to utils make-weak-hash-table for use in caches.
authorNathan Bird <nathan@acceleration.net>
Mon, 4 Jul 2011 19:40:44 +0000 (15:40 -0400)
committerNathan Bird <nathan@acceleration.net>
Mon, 4 Jul 2011 20:37:58 +0000 (16:37 -0400)
Currently used for
 * *output-hash*: caching generated SQL strings
 * oodml records cache

sql/expressions.lisp
sql/oodml.lisp
sql/utils.lisp

index bee6faffafa22dfc543255d3892890a2ce4736cd..4a6eb6384e6642469f320ce9f4efaa757582cdf3 100644 (file)
 
 
 (defvar *output-hash*
-  #+sbcl
-  (make-hash-table :test #'equal :synchronized T :weakness :key-and-value)
-  #-sbcl
-  (make-hash-table :test #'equal )
-  "For caching generated SQL strings.")
+      (make-weak-hash-table :test #'equal)
+  "For caching generated SQL strings, set to NIL to disable."
+  )
 
 (defmethod output-sql :around ((sql t) database)
   (if (null *output-hash*)
index 6397fa88dd82a73838fb5f900220243bf5a8715f..d38d3b94ded9ad1b3fb03c3944173bc0540115e5 100644 (file)
@@ -1344,11 +1344,8 @@ as elements of a list."
 (defun (setf records-cache-results) (results targets qualifiers database)
   (unless (record-caches database)
     (setf (record-caches database)
-          (make-hash-table :test 'equal
-                           #+allegro   :values    #+allegro :weak
-                           #+clisp     :weak      #+clisp :value
-                           #+lispworks :weak-kind #+lispworks :value)))
-  (setf (gethash (compute-records-cache-key targets qualifiers)
+          (make-weak-hash-table :test 'equal)))
+  (setf (gethash (compute-records-cache-key (copy-list targets) qualifiers)
                  (record-caches database)) results)
   results)
 
index 60fedc3f971d4a8fd6b9b2d39471dfcb5350064d..6e4230d84d94486d8448cc9c84f9c89839bd47e4 100644 (file)
@@ -405,3 +405,22 @@ removed. keys are searched with #'MEMBER"
           unless (member k keys-to-remove)
             collect k and collect v
           while rest)))
+
+(defmacro make-weak-hash-table (&rest args)
+  "Creates a weak hash table for use in a cache."
+  `(progn
+
+    ;;NB: These are generally used for caches that may not have an alternate
+    ;;clearing mechanism. If you are on an implementation that doesn't support
+    ;;weak hash tables then you're memory may accumulate.
+
+    #-(or sbcl allegro clisp lispworks)
+    (warn "UNSAFE! use of weak hash on implementation without support. (see clsql/sql/utils.lisp to add)")
+
+    (make-hash-table
+      #+allegro   :values    #+allegro :weak
+      #+clisp     :weak      #+clisp :value
+      #+lispworks :weak-kind #+lispworks :value
+      #+sbcl :weakness #+sbcl :value
+      ,@args)
+    ))