Skip to content
This repository was archived by the owner on Dec 29, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CL-CACHE
CLACHE
========

CL-CACHE is a general caching library for Common Lisp.
CLACHE is a general caching library for Common Lisp.

Overview
--------

CL-CACHE provides a general caching facility for Common Lisp. The API
CLACHE provides a general caching facility for Common Lisp. The API
is similar with standard hash-table interface. Let me show you an
overview of API.

Expand All @@ -17,24 +17,29 @@ overview of API.

As you can see, it is easy to use. Here is an example:

;; Create a store
(defparamater *store* (progn
(ensure-directories-exist #p"cache/")
(make-instance 'file-store :directory #p"cache/")))

;; Store cache
(setcache 1 "foo")
(setcache 1 "foo" *store*)
;;=> 1

;; Get cache
(getcache 1)
(getcache 1 *store*)
;;=> 1, T

;; Get non-exited cache
(getcache 42)
(getcache 42 *store*)
;;=> NIL, NIL

;; Remove cache
(remcache 1)
(remcache 1 *store*)
;;=> T

;; Clear all cache
(clrcache)
(clrcache *store*)

API
---
Expand Down Expand Up @@ -74,7 +79,7 @@ storages via API.

### Function: `getcache`

getcache key &optional storage
getcache key storage

Retrieve a cache value from `storage` indicated by `key` and return
values of the cache value and a boolean whether the cache exists in
Expand All @@ -84,29 +89,29 @@ exist. For example, `(getcache "not-existed-cache")` will return `nil`,

### Function: `setcache`

setcache key value &optional expire storage
setcache key value storage &optional expire

Store a cache `value` into `storage` with `key` and `expire`. `expire`
is an expiration time in seconds. If `expire` is `nil`, the cache will
never be expired. The return value is `value` that has been stored.

### Function: `(setf getcache)`

(setf getcache) value key &optional expire storage
(setf getcache) value key storage &optional expire

Same as `setcache`.

### Function: `remcache`

remcache key &optional storage
remcache key storage

Remove a cache from `storage` indicated by `key`. If the cache has
been successfully removed, this function returns `t`, otherwise
returns `nil`.

### Function: `clrcache`

clrcache &optional storage
clrcache storage

Remove all caches from `storage`. The return value is undefined.

Expand Down
8 changes: 4 additions & 4 deletions clache-test.asd
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(defpackage cl-cache-test-asd
(defpackage clache-test-asd
(:use :cl :asdf))
(in-package :cl-cache-test-asd)
(in-package :clache-test-asd)

(defsystem cl-cache-test
:depends-on (:cl-cache
(defsystem clache-test
:depends-on (:clache
:cl-test-more)
:components ((:module "tests"
:serial t
Expand Down
3 changes: 2 additions & 1 deletion clache.asd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
(in-package :clache-asd)

(defsystem :clache
:version "0.2"
:version (:read-from-file "version.lisp-expr")
:author "Tomohiro Matsuyama"
:maintainer "Olexiy Zamkoviy"
:license "LLGPL"
:depends-on (:alexandria
:trivial-garbage
Expand Down
22 changes: 11 additions & 11 deletions src/api.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,35 @@ values of the cache value and a boolean whether the cache exists in
STORE. The cache value will be NIL if such the cache doesn't
exist. For example, (getcache \"not-existed-cache\") will return NIL,
NIL."
@type (store store)
;@type (store store)
(load-cache key store))

@export
(defun setcache (key value store &key expire)
"Store a cache VALUE into STORE with KEY and EXPIRE. EXPIRE is an
expiration time in seconds. If EXPIRE is NIL, the cache will never be
expired. The return value is VALUE that has been stored."
@type (store store)
@type (expire expire)
;@type (store store)
;@type (expire expire)
(store-cache key value store expire))

@export
(defun (setf getcache) (value key store &key expire)
@type (store store)
@type (expire expire)
(store-cache key value expire store))
;@type (store store)
;@type (expire expire)
(store-cache key value store expire))

@export
(defun remcache (key store)
"Remove a cache from STORE indicated by KEY. If the cache has been
successfully removed, this function returns T, otherwise returns NIL."
@type (store store)
;@type (store store)
(delete-cache key store))

@export
(defun clrcache (store)
"Remove all caches from STORE. The return value is undefined."
@type (store store)
;@type (store store)
(clear-cache store))

@export
Expand All @@ -96,16 +96,16 @@ Example:
(if ,exists-p
,value
(let ((,value (progn ,@body)))
(setcache ,key ,store ,value :expire ,expire)
(setcache ,key ,value ,store :expire ,expire)
,value))))))

@export
(defmacro with-inline-cache ((key &key expire (test #'equal) weakness) &body body)
(defmacro with-inline-cache ((key &key expire (test 'equal) weakness) &body body)
"Same as WITH-CACHE, except that an inline memory store will be used
as a cache store. TEST is a function to test hash table keys of the
memory store. WEAKNESS specifies the hash table is weak-hash-table or
not."
(let* ((hash-table-form `(trivial-garbage:make-weak-hash-table :test ,test :weakness ,weakness))
(let* ((hash-table-form `(trivial-garbage:make-weak-hash-table :test (quote ,test) :weakness ,weakness))
(store-form `(make-instance 'memory-store :hash-table ,hash-table-form)))
`(with-cache (,key :store (load-time-value ,store-form) :expire ,expire)
,@body)))
Expand Down
2 changes: 1 addition & 1 deletion src/stores/file.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TODO
(values value t)))
(values nil nil))))

(defmethod store-cache (key value expire (store file-store))
(defmethod store-cache (key value (store file-store) expire)
(when expire
(setf expire (+ (get-universal-time) expire)))
(cl-store:store (cons expire value)
Expand Down
3 changes: 2 additions & 1 deletion src/stores/memory.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ TODO
@export
(defclass memory-store (store)
((hash-table :initform (make-hash-table :test #'equal)
:initarg :hash-table
:reader hash-table-of)))

(defmethod load-cache (key (store memory-store))
Expand All @@ -26,7 +27,7 @@ TODO
(values value t)))
(values nil nil))))

(defmethod store-cache (key value expire (store memory-store))
(defmethod store-cache (key value (store memory-store) expire)
(when expire
(setf expire (+ (get-universal-time) expire)))
(setf (gethash key (hash-table-of store))
Expand Down
Loading