Ignore:
Timestamp:
11/17/11 12:52:41 (9 years ago)
Author:
Alaric Snell-Pym
Message:

ugarit: Better use of sqlite, which will hopefully improve performance. 1.0.1 release.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/4/ugarit/trunk/backend-cache.scm

    r25501 r25521  
    99(define (backend-cache cachepath be)
    1010   (define *db* (open-database cachepath))
     11   (change-file-mode cachepath (bitwise-ior perm/irusr perm/iwusr))
    1112   (when (null? (schema *db*))
    1213         (for-each (lambda (statement)
    1314                     (exec (sql *db* statement)))
    1415                   cache-sql-schema))
     16   (exec (sql *db* "BEGIN;"))
     17
     18   (define cache-set-query (sql *db* "INSERT OR REPLACE INTO cache (key, type) VALUES (?,?)"))
     19   (define cache-get-query (sql *db* "SELECT type FROM cache WHERE key = ?"))
     20   (define cache-delete-query (sql *db* "DELETE FROM cache WHERE key = ?"))
     21
     22   (define commit-interval 1000)
     23   (define *updates-since-last-commit* 0)
     24   (define (flush!)
     25     (exec (sql *db* "COMMIT;"))
     26     (exec (sql *db* "BEGIN;"))
     27     (set! *updates-since-last-commit* 0))
     28   (define (maybe-flush!)
     29     (set! *updates-since-last-commit*
     30           (+ *updates-since-last-commit* 1))
     31     (when (> *updates-since-last-commit* commit-interval)
     32           (flush!)))
    1533
    1634   (define (cache-set! key type)
    1735      (when type
    18           (exec (sql *db* "INSERT OR REPLACE INTO cache (key, type) VALUES (?,?)") key (symbol->string type)))
     36            (begin
     37              (exec cache-set-query key (symbol->string type))
     38              (maybe-flush!)))
    1939      type)
    2040
    2141   (define (cache-get key)
    2242      (let ((result
    23              (query fetch (sql *db* "SELECT type FROM cache WHERE key = ?") key)))
     43             (query fetch cache-get-query key)))
    2444        (if (pair? result)
    2545            (string->symbol (car result))
     
    2747
    2848   (define (cache-delete! key)
    29      (exec (sql *db* "DELETE FROM cache WHERE key = ?") key))
     49     (exec cache-delete-query key)
     50     (maybe-flush!))
    3051
    3152   (make-storage
     
    5475               result)))
    5576      (lambda (tag key) ; set-tag!
    56          ((storage-set-tag! be) tag key))
     77        ((storage-set-tag! be) tag key)
     78        (flush!))
    5779      (lambda (tag) ; tag
    5880         ((storage-tag be) tag))
     
    6082         ((storage-all-tags be)))
    6183      (lambda (tag) ; remove-tag!
    62          ((storage-remove-tag! be) tag))
     84         ((storage-remove-tag! be) tag)
     85         (flush!))
    6386      (lambda (tag) ; lock-tag!
    64          ((storage-lock-tag! be) tag))
     87         ((storage-lock-tag! be) tag)
     88         (flush!))
    6589      (lambda (tag) ; tag-locked?
    6690         ((storage-tag-locked? be) tag))
    6791      (lambda (tag) ; unlock-tag!
    68          ((storage-unlock-tag! be) tag))
     92         ((storage-unlock-tag! be) tag)
     93         (flush!))
    6994      (lambda () ; close!
    7095         ((begin
     96            (exec (sql *db* "COMMIT;"))
    7197            (close-database *db*)
    7298            (storage-close! be))))))
Note: See TracChangeset for help on using the changeset viewer.