Very slim wrapper around exqlite to expose a key/value API with namespacing. Useful for local caching
- Add to children in
application.ex:
children = [
...
{Sqlcache, []},
...
]- (Optional) - Configure root_dir for Sqlite DB file in
config.exs
config :sqlcache, rootdir: "/tmp/sqlcache"- Start using
: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 provides a macro for easy function result caching, with namespace support and cache deletion helpers.
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
defcachedwraps your function to cache its result by arguments. - Use
del/2to remove a cached value for specific arguments. - Namespace is configurable per module for cache separation.
The package can be installed by adding sqlcache to your list of dependencies in mix.exs:
def deps do
[
{:sqlcache, "~> 1.1"}
]
endThe docs can be found at https://hexdocs.pm/sqlcache.