The cl-cache-tables Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

The cl-cache-tables Reference Manual

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.

Table of Contents


1 Introduction

cl-cache-tables

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.

How do i use it?

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.

Example

> (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

API

(make-cache-table &key (test #'eql) (size 1024) (rehash-size 1.5) (rehash-threshold 0.75))

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 key value cache-table &key (expire 0))

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 key cache-table)

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

(cache-table-get-or-fill key cache-table fun &key (expire 0))

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 key cache-table)

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

(mapcache function cache-table)

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 key cache-table)

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 key cache-table)

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

(cache-table-count cache-table)

Returns the number of key-value pairs in a cache-table.

(cache-table-count *cache*)
345

(clrcache cache-table)

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 cache-table)

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 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

Contributing

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.

License

MIT


2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 cl-cache-tables

A wrapper around native hash-tables to facilitate
in-process caching of common lisp data structures.

Author

Diogo Franco

License

MIT

Version

0.0.1

Source

cl-cache-tables.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp   [Contents][Index]

3.1.1 cl-cache-tables/cl-cache-tables.asd

Source

cl-cache-tables.asd.

Parent Component

cl-cache-tables (system).

ASDF Systems

cl-cache-tables.


3.1.2 cl-cache-tables/package.lisp

Source

cl-cache-tables.asd.

Parent Component

cl-cache-tables (system).

Packages

cache.


3.1.3 cl-cache-tables/cl-cache-tables.lisp

Dependency

package.lisp (file).

Source

cl-cache-tables.asd.

Parent Component

cl-cache-tables (system).

Public Interface
Internals

4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 cache

Source

package.lisp.

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Public Interface


5.1.1 Ordinary functions

Function: cache-table-count (cache-table)

Returns the number of elements in cache-table. A cleared or new cache-table returns 0.

Package

cache.

Source

cl-cache-tables.lisp.

Function: cache-table-del (key cache-table)

Deletes key from cache-table. Returns t if the key existed, nil otherwise.

Package

cache.

Source

cl-cache-tables.lisp.

Function: cache-table-get (key cache-table)

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.

Package

cache.

Source

cl-cache-tables.lisp.

Function: cache-table-get-or-fill (key cache-table fun &key expire)

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

Package

cache.

Source

cl-cache-tables.lisp.

Function: cache-table-p (object)
Package

cache.

Source

cl-cache-tables.lisp.

Function: cache-table-persist (key cache-table)

Removes the expiration from a key. Returns nil if key did not exist, and nil otherwise.

Package

cache.

Source

cl-cache-tables.lisp.

Function: cache-table-put (key value cache-table &key expire)

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.

Package

cache.

Source

cl-cache-tables.lisp.

Function: cache-table-ttl (key cache-table)

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.

Package

cache.

Source

cl-cache-tables.lisp.

Function: clrcache (cache-table)

Removes all entries from cache-table and returns that empty cache-table.

Package

cache.

Source

cl-cache-tables.lisp.

Function: copy-cache-table (cache-table)

Creates a new cache-table equal to cache-table and returns it.

Package

cache.

Source

cl-cache-tables.lisp.

Function: make-cache-table (&key test rehash-size rehash-threshold size)
Package

cache.

Source

cl-cache-tables.lisp.

Function: mapcache (function cache-table)

Calls fun for every existing and non-expired key value pair in cache-table. Similarly to maphash, returns nil.

Package

cache.

Source

cl-cache-tables.lisp.


5.2 Internals


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.1 Ordinary functions

Reader: cache-table-hash (instance)
Writer: (setf cache-table-hash) (instance)
Package

cache.

Source

cl-cache-tables.lisp.

Target Slot

hash.

Reader: cache-table-rehash-size (instance)
Writer: (setf cache-table-rehash-size) (instance)
Package

cache.

Source

cl-cache-tables.lisp.

Target Slot

rehash-size.

Reader: cache-table-rehash-threshold (instance)
Writer: (setf cache-table-rehash-threshold) (instance)
Package

cache.

Source

cl-cache-tables.lisp.

Target Slot

rehash-threshold.

Reader: cache-table-size (instance)
Writer: (setf cache-table-size) (instance)
Package

cache.

Source

cl-cache-tables.lisp.

Target Slot

size.

Reader: cache-table-test (instance)
Writer: (setf cache-table-test) (instance)
Package

cache.

Source

cl-cache-tables.lisp.

Target Slot

test.

Function: copy-hash-table (hash-table)

Helper function that copies a hash table.

Package

cache.

Source

cl-cache-tables.lisp.


5.2.2 Structures

Structure: cache-table
Package

cache.

Source

cl-cache-tables.lisp.

Direct superclasses

structure-object.

Direct slots
Slot: test
Readers

cache-table-test.

Writers

(setf cache-table-test).

Slot: size
Readers

cache-table-size.

Writers

(setf cache-table-size).

Slot: rehash-size
Readers

cache-table-rehash-size.

Writers

(setf cache-table-rehash-size).

Slot: rehash-threshold
Readers

cache-table-rehash-threshold.

Writers

(setf cache-table-rehash-threshold).

Slot: hash
Readers

cache-table-hash.

Writers

(setf cache-table-hash).


Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts


Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   (  
C   F   M  
Index Entry  Section

(
(setf cache-table-hash): Private ordinary functions
(setf cache-table-rehash-size): Private ordinary functions
(setf cache-table-rehash-threshold): Private ordinary functions
(setf cache-table-size): Private ordinary functions
(setf cache-table-test): Private ordinary functions

C
cache-table-count: Public ordinary functions
cache-table-del: Public ordinary functions
cache-table-get: Public ordinary functions
cache-table-get-or-fill: Public ordinary functions
cache-table-hash: Private ordinary functions
cache-table-p: Public ordinary functions
cache-table-persist: Public ordinary functions
cache-table-put: Public ordinary functions
cache-table-rehash-size: Private ordinary functions
cache-table-rehash-threshold: Private ordinary functions
cache-table-size: Private ordinary functions
cache-table-test: Private ordinary functions
cache-table-ttl: Public ordinary functions
clrcache: Public ordinary functions
copy-cache-table: Public ordinary functions
copy-hash-table: Private ordinary functions

F
Function, (setf cache-table-hash): Private ordinary functions
Function, (setf cache-table-rehash-size): Private ordinary functions
Function, (setf cache-table-rehash-threshold): Private ordinary functions
Function, (setf cache-table-size): Private ordinary functions
Function, (setf cache-table-test): Private ordinary functions
Function, cache-table-count: Public ordinary functions
Function, cache-table-del: Public ordinary functions
Function, cache-table-get: Public ordinary functions
Function, cache-table-get-or-fill: Public ordinary functions
Function, cache-table-hash: Private ordinary functions
Function, cache-table-p: Public ordinary functions
Function, cache-table-persist: Public ordinary functions
Function, cache-table-put: Public ordinary functions
Function, cache-table-rehash-size: Private ordinary functions
Function, cache-table-rehash-threshold: Private ordinary functions
Function, cache-table-size: Private ordinary functions
Function, cache-table-test: Private ordinary functions
Function, cache-table-ttl: Public ordinary functions
Function, clrcache: Public ordinary functions
Function, copy-cache-table: Public ordinary functions
Function, copy-hash-table: Private ordinary functions
Function, make-cache-table: Public ordinary functions
Function, mapcache: Public ordinary functions

M
make-cache-table: Public ordinary functions
mapcache: Public ordinary functions

Jump to:   (  
C   F   M