https://github.com/mindreframer/sqlcache
SQLite based cache with namespacing for Elixir
https://github.com/mindreframer/sqlcache
caching elixir sqlite
Last synced: 11 days ago
JSON representation
SQLite based cache with namespacing for Elixir
- Host: GitHub
- URL: https://github.com/mindreframer/sqlcache
- Owner: mindreframer
- Created: 2023-02-27T21:58:11.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-07-01T15:02:59.000Z (11 months ago)
- Last Synced: 2025-07-01T15:50:51.274Z (11 months ago)
- Topics: caching, elixir, sqlite
- Language: Elixir
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Sqlcache
Very slim wrapper around `exqlite` to expose a key/value API with namespacing. Useful for local caching
## Usage
1. Add to children in `application.ex`:
```elixir
children = [
...
{Sqlcache, []},
...
]
```
2. (Optional) - Configure root_dir for Sqlite DB file in `config.exs`
```elixir
config :sqlcache, rootdir: "/tmp/sqlcache"
```
3. Start using
```elixir
:ok == Sqlcache.clear_kind("test")
:ok == Sqlcache.put("test", "a", 1)
{:ok, 1} == Sqlcache.get("test", "a")
:ok == Sqlcache.put("test", "a", %{a: 1, b: 2})
{:ok, %{a: 1, b: 2}} == Sqlcache.get("test", "a")
:ok == Sqlcache.clear_kind("test")
{:error, nil} == Sqlcache.get("test", "a")
```
## Sqlcache.Wrapper: Function Caching Macro
`Sqlcache.Wrapper` provides a macro for easy function result caching, with namespace support and cache deletion helpers.
### Example
```elixir
defmodule MyApp.UsersCached do
use Sqlcache.Wrapper, namespace: "user"
# This function's result will be cached by argument
defcached get_user(id) do
MyApp.Users.get_user(id)
end
# Remove a specific cached value
def remove_from_cache(id) do
del(:get_user, [id])
end
end
# Usage:
MyApp.UsersCached.get_user(123) # Calls and caches
MyApp.UsersCached.get_user(123) # Returns cached result
MyApp.UsersCached.remove_from_cache(123) # Removes from cache
```
- The macro `defcached` wraps your function to cache its result by arguments.
- Use `del/2` to remove a cached value for specific arguments.
- Namespace is configurable per module for cache separation.
## Installation
The package can be installed by adding `sqlcache` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:sqlcache, "~> 1.1"}
]
end
```
The docs can be found at .