Module cachesqlite
An SQLite-based cache implementation, with an interface similar to Cache.
Example:
local CacheSQLite = require("cachesqlite") local cache = CacheSQLite:new{ size = 1024 * 1024 * 10, -- 10 MB -- Set to :memory: for an in-memory database. -- In that case, set auto_close to false. db_path = "/path/to/cache.db", } cache:insert("key", {value = "data"}) local data = cache:check("key")
Functions
CacheSQLite:openDB () | Opens the SQLite database. |
CacheSQLite:closeDB ([explicit=false]) | Closes the SQLite database. |
CacheSQLite:isConnected () | Retrieves the connected state of the database. |
CacheSQLite:insert (key, object) | Inserts an object into the cache. |
CacheSQLite:check (key) | Retrieves an object if it is in the cache and updates its access time. |
CacheSQLite:get (key) | Retrieves an object if it is in the cache without updating its access time. |
CacheSQLite:remove (key) | Removes an object from the cache. |
CacheSQLite:willAccept (size) | Queries whether the cache will accept an object of a given size in bytes. |
CacheSQLite:clear () | Clears the entire cache. |
Fields
size | Max storage space, in bytes. |
db_path | Database file path. |
codec | Compression codec from Persist. |
auto_close | Whether to automatically close the DB connection after each operation. |
Functions
- CacheSQLite:openDB ()
- Opens the SQLite database. This is normally done internally, but can be called manually if needed.
- CacheSQLite:closeDB ([explicit=false])
-
Closes the SQLite database.
This is normally done internally, but can be called manually if needed.
Parameters:
- explicit boolean When auto_close is false, this must be set to true to close the DB. (default false)
- CacheSQLite:isConnected ()
-
Retrieves the connected state of the database.
This is normally done internally, but can be called manually if needed.
Returns:
-
boolean
- CacheSQLite:insert (key, object)
-
Inserts an object into the cache.
Parameters:
- key string
- object any
Returns:
-
boolean success, number size
- CacheSQLite:check (key)
-
Retrieves an object if it is in the cache and updates its access time.
Parameters:
- key string
Returns:
-
any
- CacheSQLite:get (key)
-
Retrieves an object if it is in the cache without updating its access time.
Parameters:
- key string
Returns:
-
any
- CacheSQLite:remove (key)
-
Removes an object from the cache.
Parameters:
- key
- CacheSQLite:willAccept (size)
-
Queries whether the cache will accept an object of a given size in bytes.
Parameters:
- size We only allow a single object to fill 50% of the cache
- CacheSQLite:clear ()
- Clears the entire cache.