Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the cl-cache-tables Reference Manual, version 0.0.1, generated automatically by Declt version 4.0 beta 2 "William Riker" on Wed Jun 15 03:32:41 2022 GMT+0.
Next: Systems, Previous: The cl-cache-tables Reference Manual, Up: The cl-cache-tables Reference Manual [Contents][Index]
cl-cache-tables is a small, portable, dependency-free library that facilitates caching of data such as lisp data structures.
It uses the native hash-tables of your lisp implementation under the hood, but implements an api that handles caching stuff such as key expiration, with little aditional overhead.
It works at least on sbcl, ecl, ccl, abcl and clisp.
This section assumes you use quicklisp. If you don't, you should! Download and learn about it here.
Once you have quicklisp loaded, simply do:
(ql:quickload :cl-cache-tables)
And it's all up and running. To run the tests do:
(ql:quickload :cl-cache-tables-tests)
Please report if any tests fail in your Common Lisp implementation.
> (ql:quickload :cl-cache-tables)
(:CL-CACHE-TABLES)
> (use-package :cache)
T
> (defvar *cache* (make-cache-table :test #'equal))
*cache*
> (progn
(cache-table-put "key" "value" *cache* :expire 1)
(print (cache-table-get "key" *cache*))
(sleep 1)
(print (cache-table-get "key" *cache*)))
"value"
nil
make-cache-table returns a newly created cache-table. The argument list is the same as in the native constuctor make-hash-table.
(defvar *cache* (make-cache-table :test #'equal))
cache-table-put populates the cache-table with a new key and value. The expire parameter is the time in seconds that the key will be present, where 0 means that it does not expire. Expire defaults to 0. cache-table-put returns the value that was set.
(cache-table-put "some-persistent-key" "persistent" *cache* :expire 0) ;; "persistent"
(cache-table-put "some-temporary-key" "temporary" *cache* :expire 10) ;; "temporary"
cache-table-get retrieves a value from key in cache-table. Returns two values, similarly to gethash: The first one is the value stored at key, and the second one is a generalized boolean indicating if the key exists (this allows us to distinguish the case where we store nil at a cache-table key).
(cache-table-get "some-persistent-key" *cache*)
"persistent"
T
Retrieves key from cache table, similarly to cache-table-get. When the key is not present (or has expired), fun is called with the key argument, to compute/retrieve it from another source. cache-table is then populated with the result and an expiration of "expire" seconds. Returns the value of key. Notice that when function returns nil, that value becomes cached as any other value, so if you want to signal that some value couldn't be found, your function should signal an error."
(defvar *cache* (make-hash-table :test #'equal)) ;; cache-table
(cache-table-get-or-fill "my-key" *cache* #'(lambda (key)
(retrieve-from-mongo key)) :expire 10)
"some-value" ;; retrieved from mongodb
(cache-table-get-or-fill "my-key" *cache* #'(lambda (key)
(retrieve-from-mongo key)) :expire 10)
"some-value" ;; retrieved from cache-table if less than 10 seconds elapsed, else retrieved from mongo.
cache-table-del deletes a key from the cache-table. Returns t if the key existed, nil otherwise.
(cache-table-del "some-non-existing-key" *cache*)
NIL
Similarly to maphash, receives a function of two arguments (key value), and applies it to every key-value pair in cache-table. Always returns nil.
(mapcache #'(lambda (key value) (print (cons key value))) *cache*)
("come-persistent-key" . "persistent")
("some-temporary-key" . "temporary")
NIL
cache-table-ttl returns the amount of time in seconds that a key still has before expiring. If the key is persistent, this function returns -1. If it does not exist, it returns nil.
(cache-table-ttl "some-temporary-key" *cache*)
8
(cache-table-ttl "some-persistent-key" *cache*)
-1
(cache-table-ttl "some-non-existing-key" *cache*)
NIL
cache-table-persist turns a temporary key to a persistent one, by removing the expiration. Returns T if key exists, NIL otherwise.
(cache-table-persist "some-temporary-key" *cache*)
T
(cache-table-ttl "a-non-existing-key" *cache*)
NIL
Returns the number of key-value pairs in a cache-table.
(cache-table-count *cache*)
345
clrcache removes all entries from cache-table and returns that empty cache-table.
(clrcache *cache*) ;; cash-table
(cache-table-count *cache*) ;; 0
copy-cache-table creates a new cache equal to cache-table and returns it.
(copy-cache-table *cache*) ;; new cache-table equal to cache-table
cache-table-p is a predicate that checks if cache-table if indeed a cache-table. Returns T if is, NIL otherwise.
(cache-table-p *cache*)
T
(cache-table-p 3)
NIL
If you have any suggestions, bug reports, etc, please fill in an issue describing it. If you have the time and want to contribute, that is even better! Submit some tests too, let's try and keep coverage up.
MIT
Next: Files, Previous: Introduction, Up: The cl-cache-tables Reference Manual [Contents][Index]
The main system appears first, followed by any subsystem dependency.
A wrapper around native hash-tables to facilitate
in-process caching of common lisp data structures.
Diogo Franco
MIT
0.0.1
Next: Packages, Previous: Systems, Up: The cl-cache-tables Reference Manual [Contents][Index]
Files are sorted by type and then listed depth-first from the systems components trees.
Next: cl-cache-tables/package.lisp, Previous: Lisp, Up: Lisp [Contents][Index]
cl-cache-tables (system).
Next: cl-cache-tables/cl-cache-tables.lisp, Previous: cl-cache-tables/cl-cache-tables.asd, Up: Lisp [Contents][Index]
cl-cache-tables (system).
Previous: cl-cache-tables/package.lisp, Up: Lisp [Contents][Index]
package.lisp (file).
cl-cache-tables (system).
Next: Definitions, Previous: Files, Up: The cl-cache-tables Reference Manual [Contents][Index]
Packages are listed by definition order.
common-lisp.
Next: Indexes, Previous: Packages, Up: The cl-cache-tables Reference Manual [Contents][Index]
Definitions are sorted by export status, category, package, and then by lexicographic order.
Next: Internals, Previous: Definitions, Up: Definitions [Contents][Index]
Previous: Public Interface, Up: Public Interface [Contents][Index]
Returns the number of elements in cache-table. A cleared or new cache-table returns 0.
Deletes key from cache-table. Returns t if the key existed, nil otherwise.
Returns the value at key in cache-table. Also returns a second value that is T if the key exists, and nil if it doesn’t or has expired.
Retrieves key from cache table, similarly to cache-table-get.
When the key is not present, function is called with the key argument,
to compute/retrieve it from another source. cache-table is then populated
with the result and expiration of "expire" seconds. Returns the value
of key. Notice that when function returns nil, that value becomes cached as
any other value, so if you want to signal that some value couldn’t be found,
your function should throw an error
Removes the expiration from a key. Returns nil if key did not exist, and nil otherwise.
Sets key and value in cache-table. Expire is in seconds and defaults to 0, which means that this key will never expire. Returns value.
Returns the time to live (in seconds) of the key in cache-table.
If the key is persistent, returns -1. If it does not exist, returns nil.
Removes all entries from cache-table and returns that empty cache-table.
Creates a new cache-table equal to cache-table and returns it.
Calls fun for every existing and non-expired key value pair in cache-table. Similarly to maphash, returns nil.
Previous: Public Interface, Up: Definitions [Contents][Index]
Next: Structures, Previous: Internals, Up: Internals [Contents][Index]
hash.
size.
test.
Helper function that copies a hash table.
Previous: Ordinary functions, Up: Internals [Contents][Index]
Previous: Definitions, Up: The cl-cache-tables Reference Manual [Contents][Index]
Jump to: | (
C F M |
---|
Jump to: | (
C F M |
---|
Next: Data types, Previous: Functions, Up: Indexes [Contents][Index]
Jump to: | H R S T |
---|
Jump to: | H R S T |
---|
Jump to: | C F P S |
---|
Jump to: | C F P S |
---|