CLACHE is a general caching library for Common Lisp.
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.
getcache
- Get cache from storagesetcache
- Store cache into storageremcache
- Remove cache from storageclrcache
- Clear all cache in storage
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" *store*)
;;=> 1
;; Get cache
(getcache 1 *store*)
;;=> 1, T
;; Get non-exited cache
(getcache 42 *store*)
;;=> NIL, NIL
;; Remove cache
(remcache 1 *store*)
;;=> T
;; Clear all cache
(clrcache *store*)
A cache is a triple of a key, a value, and an expiration time.
Any object can be used as a cache key if the object can be converted
into a string properly by using cache-key-to-string
.
Same as cache keys, any object can be used as a cache value. However, a type of cache values can be limited by storages. So you have to be careful what storage are you using.
An expiration time describes how long caches live in seconds. If an
expiration time is nil
, such caches will never be expired:
persistent cache.
If a cache is stored in a storage and has not yet been expired or a persitent cache, we express the cache exists in the storage.
Storage is an abstract layer of maintaining caches. You can access storages via API.
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
storage
. The cache value will be nil
if such the cache doesn't
exist. For example, (getcache "not-existed-cache")
will return nil
,
nil
.
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.
(setf getcache) value key storage &optional expire
Same as setcache
.
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
.
clrcache storage
Remove all caches from storage
. The return value is undefined.
- Allegro CL v8.2
- SBCL v1.0.47
- CMU CL v20b
- Clozure CL v1.6
- ECL v11.1.1
- GNU CLISP v2.48
Copyright (C) 2011 Tomohiro Matsuyama <[email protected]>